Debian Codegraph @

cc/cxxcom/builtins.c

raw symbols SCIP
1 /* $Id: builtins.c,v 1.9 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 #ifndef MIN
30 #define MIN(a,b) (((a)<(b))?(a):(b))
31 #endif
32 #ifndef MAX
33 #define MAX(a,b) (((a)>(b))?(a):(b))
34 #endif
35
36 #ifndef NO_C_BUILTINS
37
38 /*
39 * replace an alloca function with direct allocation on stack.
40 * return a destination temp node.
41 */
42 static NODE *
43 builtin_alloca(const struct bitable *bt, NODE *a)
44 {
45 NODE *t, *u;
46
47 #ifdef notyet
48 if (xnobuiltins)
49 return NULL;
50 #endif
51
52 t = tempnode(0, VOID|PTR, 0, 0);
53 u = tempnode(regno(t), VOID|PTR, 0, 0);
54 spalloc(t, a, SZCHAR);
55 return u;
56 }
57
58 /*
59 * Determine if a value is known to be constant at compile-time and
60 * hence that PCC can perform constant-folding on expressions involving
61 * that value.
62 */
63 static NODE *
64 builtin_constant_p(const struct bitable *bt, NODE *a)
65 {
66 void putjops(NODE *p, void *arg);
67 NODE *f;
68 int isconst;
69
70 walkf(a, putjops, 0);
71 for (f = a; f->n_op == COMOP; f = f->n_right)
72 ;
73 isconst = nncon(f);
74 tfree(a);
75 return bcon(isconst);
76 }
77
78 /*
79 * Hint to the compiler whether this expression will evaluate true or false.
80 * Just ignored for now.
81 */
82 static NODE *
83 builtin_expect(const struct bitable *bt, NODE *a)
84 {
85 NODE *f;
86
87 if (a && a->n_op == CM) {
88 tfree(a->n_right);
89 f = a->n_left;
90 nfree(a);
91 a = f;
92 }
93
94 return a;
95 }
96
97 /*
98 * Take integer absolute value.
99 * Simply does: ((((x)>>(8*sizeof(x)-1))^(x))-((x)>>(8*sizeof(x)-1)))
100 */
101 static NODE *
102 builtin_abs(const struct bitable *bt, NODE *a)
103 {
104 NODE *p, *q, *r, *t, *t2, *t3;
105 int tmp1, tmp2, shift;
106
107 if (a->n_type != INT)
108 a = cast(a, INT, 0);
109
110 if (a->n_op == ICON) {
111 if (getlval(a) < 0)
112 setlval(a, -getlval(a));
113 p = a;
114 } else {
115 t = tempnode(0, a->n_type, a->n_df, a->n_ap);
116 tmp1 = regno(t);
117 p = buildtree(ASSIGN, t, a);
118
119 t = tempnode(tmp1, a->n_type, a->n_df, a->n_ap);
120 shift = (int)tsize(a->n_type, a->n_df, a->n_ap) - 1;
121 q = buildtree(RS, t, bcon(shift));
122
123 t2 = tempnode(0, a->n_type, a->n_df, a->n_ap);
124 tmp2 = regno(t2);
125 q = buildtree(ASSIGN, t2, q);
126
127 t = tempnode(tmp1, a->n_type, a->n_df, a->n_ap);
128 t2 = tempnode(tmp2, a->n_type, a->n_df, a->n_ap);
129 t3 = tempnode(tmp2, a->n_type, a->n_df, a->n_ap);
130 r = buildtree(MINUS, buildtree(ER, t, t2), t3);
131
132 p = buildtree(COMOP, p, buildtree(COMOP, q, r));
133 }
134
135 return p;
136 }
137
138 #define cmop(x,y) buildtree(COMOP, x, y)
139 #define lblnod(l) nlabel(l)
140
141 #ifndef TARGET_CXZ
142 /*
143 * Find number of beginning 0's in a word of type t.
144 * t should be deunsigned.
145 */
146 static NODE *
147 builtin_cxz(NODE *a, TWORD t, int isclz)
148 {
149 NODE *t101, *t102;
150 NODE *rn, *p;
151 int l15, l16, l17;
152 int sz;
153
154 t = ctype(t);
155 sz = (int)tsize(t, 0, 0);
156
157 t101 = tempnode(0, INT, 0, 0);
158 t102 = tempnode(0, t, 0, 0);
159 l15 = getlab();
160 l16 = getlab();
161 l17 = getlab();
162 rn = buildtree(ASSIGN, ccopy(t102), a);
163 rn = cmop(rn, buildtree(ASSIGN, ccopy(t101), bcon(0)));
164 rn = cmop(rn, lblnod(l16));
165
166 p = buildtree(CBRANCH, buildtree(GE, ccopy(t101), bcon(sz)), bcon(l15));
167 rn = cmop(rn, p);
168 if (isclz) {
169 p = buildtree(CBRANCH,
170 buildtree(GE, ccopy(t102), bcon(0)), bcon(l17));
171 } else {
172 p = buildtree(CBRANCH,
173 buildtree(EQ, buildtree(AND, ccopy(t102), bcon(1)),
174 bcon(0)), bcon(l17));
175 }
176 rn = cmop(rn, p);
177
178 rn = cmop(rn, block(GOTO, bcon(l15), NIL, INT, 0, 0));
179
180 rn = cmop(rn, lblnod(l17));
181 rn = cmop(rn, buildtree(isclz ? LSEQ : RSEQ , t102, bcon(1)));
182
183 rn = cmop(rn, buildtree(INCR, ccopy(t101), bcon(1)));
184
185 rn = cmop(rn, block(GOTO, bcon(l16), NIL, INT, 0, 0));
186 rn = cmop(rn, lblnod(l15));
187 return cmop(rn, t101);
188 }
189
190 static NODE *
191 builtin_clz(const struct bitable *bt, NODE *a)
192 {
193 return builtin_cxz(a, INT, 1);
194 }
195
196 static NODE *
197 builtin_clzl(const struct bitable *bt, NODE *a)
198 {
199 return builtin_cxz(a, LONG, 1);
200 }
201
202 static NODE *
203 builtin_clzll(const struct bitable *bt, NODE *a)
204 {
205 return builtin_cxz(a, LONGLONG, 1);
206 }
207
208 static NODE *
209 builtin_ctz(const struct bitable *bt, NODE *a)
210 {
211 return builtin_cxz(a, INT, 0);
212 }
213
214 static NODE *
215 builtin_ctzl(const struct bitable *bt, NODE *a)
216 {
217 return builtin_cxz(a, LONG, 0);
218 }
219
220 static NODE *
221 builtin_ctzll(const struct bitable *bt, NODE *a)
222 {
223 return builtin_cxz(a, LONGLONG, 0);
224 }
225 #endif
226
227 #ifndef TARGET_FFS
228 /*
229 * Find number of beginning 0's in a word of type t.
230 * t should be deunsigned.
231 */
232 static NODE *
233 builtin_ff(NODE *a, TWORD t)
234 {
235 NODE *t101, *t102;
236 NODE *rn, *p;
237 int l15, l16, l17;
238 int sz;
239
240 t = ctype(t);
241 sz = (int)tsize(t, 0, 0)+1;
242
243 t101 = tempnode(0, INT, 0, 0);
244 t102 = tempnode(0, t, 0, 0);
245 l15 = getlab();
246 l16 = getlab();
247 l17 = getlab();
248 rn = buildtree(ASSIGN, ccopy(t101), bcon(0));
249 rn = cmop(rn, buildtree(ASSIGN, ccopy(t102), a));
250
251 p = buildtree(CBRANCH, buildtree(EQ, ccopy(t102), bcon(0)), bcon(l15));
252 rn = cmop(rn, p);
253
254 rn = cmop(rn, buildtree(INCR, ccopy(t101), bcon(1)));
255
256 rn = cmop(rn, lblnod(l16));
257
258 p = buildtree(CBRANCH, buildtree(GE, ccopy(t101), bcon(sz)), bcon(l15));
259 rn = cmop(rn, p);
260
261 p = buildtree(CBRANCH,
262 buildtree(EQ, buildtree(AND, ccopy(t102), bcon(1)),
263 bcon(0)), bcon(l17));
264 rn = cmop(rn, p);
265
266 rn = cmop(rn, block(GOTO, bcon(l15), NIL, INT, 0, 0));
267
268 rn = cmop(rn, lblnod(l17));
269 rn = cmop(rn, buildtree(RSEQ, t102, bcon(1)));
270
271 rn = cmop(rn, buildtree(INCR, ccopy(t101), bcon(1)));
272
273 rn = cmop(rn, block(GOTO, bcon(l16), NIL, INT, 0, 0));
274 rn = cmop(rn, lblnod(l15));
275 return cmop(rn, t101);
276 }
277
278 static NODE *
279 builtin_ffs(const struct bitable *bt, NODE *a)
280 {
281 return builtin_ff(a, INT);
282 }
283
284 static NODE *
285 builtin_ffsl(const struct bitable *bt, NODE *a)
286 {
287 return builtin_ff(a, LONG);
288 }
289
290 static NODE *
291 builtin_ffsll(const struct bitable *bt, NODE *a)
292 {
293 return builtin_ff(a, LONGLONG);
294 }
295 #endif
296
297 /*
298 * Get size of object, if possible.
299 * Currently does nothing,
300 */
301 static NODE *
302 builtin_object_size(const struct bitable *bt, NODE *a)
303 {
304 CONSZ v = icons(a->n_right);
305 NODE *f;
306
307 if (v < 0 || v > 3)
308 uerror("arg2 must be between 0 and 3");
309
310 f = buildtree(COMOP, a->n_left, xbcon(v < 2 ? -1 : 0, NULL, bt->rt));
311 nfree(a);
312 return f;
313 }
314
315 #ifndef TARGET_STDARGS
316 static NODE *
317 builtin_stdarg_start(const struct bitable *bt, NODE *a)
318 {
319 NODE *p, *q;
320 int sz;
321
322 /* must first deal with argument size; use int size */
323 p = a->n_right;
324 if (p->n_type < INT) {
325 sz = (int)(SZINT/tsize(p->n_type, p->n_df, p->n_ap));
326 } else
327 sz = 1;
328
329 /* do the real job */
330 p = buildtree(ADDROF, p, NIL); /* address of last arg */
331 #ifdef STACK_DOWN
332 p = optim(buildtree(PLUS, p, bcon(sz))); /* add one to it (next arg) */
333 #else
334 p = optim(buildtree(MINUS, p, bcon(sz))); /* add one to it (next arg) */
335 #endif
336 q = block(NAME, NIL, NIL, PTR+VOID, 0, 0); /* create cast node */
337 q = buildtree(CAST, q, p); /* cast to void * (for assignment) */
338 p = q->n_right;
339 nfree(q->n_left);
340 nfree(q);
341 p = buildtree(ASSIGN, a->n_left, p); /* assign to ap */
342 nfree(a);
343 return p;
344 }
345
346 static NODE *
347 builtin_va_arg(const struct bitable *bt, NODE *a)
348 {
349 NODE *p, *q, *r, *rv;
350 int sz, nodnum;
351
352 /* create a copy to a temp node of current ap */
353 p = ccopy(a->n_left);
354 q = tempnode(0, p->n_type, p->n_df, p->n_ap);
355 nodnum = regno(q);
356 rv = buildtree(ASSIGN, q, p);
357
358 r = a->n_right;
359 sz = (int)tsize(r->n_type, r->n_df, r->n_ap)/SZCHAR;
360 /* add one to ap */
361 #ifdef STACK_DOWN
362 rv = buildtree(COMOP, rv , buildtree(PLUSEQ, a->n_left, bcon(sz)));
363 #else
364 ecomp(buildtree(MINUSEQ, a->n_left, bcon(sz)));
365 #endif
366
367 nfree(a->n_right);
368 nfree(a);
369 r = tempnode(nodnum, INCREF(r->n_type), r->n_df, r->n_ap);
370 return buildtree(COMOP, rv, buildtree(UMUL, r, NIL));
371
372 }
373
374 static NODE *
375 builtin_va_end(const struct bitable *bt, NODE *a)
376 {
377 tfree(a);
378 return bcon(0); /* nothing */
379 }
380
381 static NODE *
382 builtin_va_copy(const struct bitable *bt, NODE *a)
383 {
384 NODE *f;
385
386 f = buildtree(ASSIGN, a->n_left, a->n_right);
387 nfree(a);
388 return f;
389 }
390 #endif /* TARGET_STDARGS */
391
392 /*
393 * For unimplemented "builtin" functions, try to invoke the
394 * non-builtin name
395 */
396 static NODE *
397 binhelp(NODE *a, TWORD rt, char *n)
398 {
399 NODE *f = block(NAME, NIL, NIL, INT, 0, 0);
400
401 f->n_sp = lookup(addname(n), SNORMAL);
402 if (f->n_sp->sclass == SNULL) {
403 f->n_sp->sclass = EXTERN;
404 f->n_sp->stype = INCREF(rt)+(FTN-PTR);
405 }
406 f->n_type = f->n_sp->stype;
407 f = clocal(f);
408 return buildtree(CALL, f, a);
409 }
410
411 static NODE *
412 builtin_unimp(const struct bitable *bt, NODE *a)
413 {
414 return binhelp(a, bt->rt, &bt->name[10]);
415 }
416
417 #if 0
418 static NODE *
419 builtin_unimp_f(NODE *f, NODE *a, TWORD rt)
420 {
421 return binhelp(f, a, rt, f->n_sp->sname);
422 }
423 #endif
424
425 #ifndef TARGET_PREFETCH
426 static NODE *
427 builtin_prefetch(const struct bitable *bt, NODE *a)
428 {
429 tfree(a);
430 return bcon(0);
431 }
432 #endif
433
434 #ifndef TARGET_ISMATH
435 /*
436 * Handle the builtin macros for the math functions is*
437 * To get something that is be somewhat generic assume that
438 * isnan() is a real function and that cast of a NaN type
439 * to double will still be a NaN.
440 */
441 static NODE *
442 mtisnan(NODE *p)
443 {
444
445 return binhelp(cast(ccopy(p), DOUBLE, 0), INT, "isnan");
446 }
447
448 static TWORD
449 mtcheck(NODE *p)
450 {
451 TWORD t1 = p->n_left->n_type, t2 = p->n_right->n_type;
452
453 if ((t1 >= FLOAT && t1 <= LDOUBLE) ||
454 (t2 >= FLOAT && t2 <= LDOUBLE))
455 return MAX(t1, t2);
456 return 0;
457 }
458
459 static NODE *
460 builtin_isunordered(const struct bitable *bt, NODE *a)
461 {
462 NODE *p;
463
464 if (mtcheck(a) == 0)
465 return bcon(0);
466
467 p = buildtree(OROR, mtisnan(a->n_left), mtisnan(a->n_right));
468 tfree(a);
469 return p;
470 }
471 static NODE *
472 builtin_isany(NODE *a, TWORD rt, int cmpt)
473 {
474 NODE *p, *q;
475 TWORD t;
476
477 if ((t = mtcheck(a)) == 0)
478 return bcon(0);
479 p = buildtree(OROR, mtisnan(a->n_left), mtisnan(a->n_right));
480 p = buildtree(NOT, p, NIL);
481 q = buildtree(cmpt, cast(ccopy(a->n_left), t, 0),
482 cast(ccopy(a->n_right), t, 0));
483 p = buildtree(ANDAND, p, q);
484 tfree(a);
485 return p;
486 }
487 static NODE *
488 builtin_isgreater(const struct bitable *bt, NODE *a)
489 {
490 return builtin_isany(a, bt->rt, GT);
491 }
492 static NODE *
493 builtin_isgreaterequal(const struct bitable *bt, NODE *a)
494 {
495 return builtin_isany(a, bt->rt, GE);
496 }
497 static NODE *
498 builtin_isless(const struct bitable *bt, NODE *a)
499 {
500 return builtin_isany(a, bt->rt, LT);
501 }
502 static NODE *
503 builtin_islessequal(const struct bitable *bt, NODE *a)
504 {
505 return builtin_isany(a, bt->rt, LE);
506 }
507 static NODE *
508 builtin_islessgreater(const struct bitable *bt, NODE *a)
509 {
510 NODE *p, *q, *r;
511 TWORD t;
512
513 if ((t = mtcheck(a)) == 0)
514 return bcon(0);
515 p = buildtree(OROR, mtisnan(a->n_left), mtisnan(a->n_right));
516 p = buildtree(NOT, p, NIL);
517 q = buildtree(GT, cast(ccopy(a->n_left), t, 0),
518 cast(ccopy(a->n_right), t, 0));
519 r = buildtree(LT, cast(ccopy(a->n_left), t, 0),
520 cast(ccopy(a->n_right), t, 0));
521 q = buildtree(OROR, q, r);
522 p = buildtree(ANDAND, p, q);
523 tfree(a);
524 return p;
525 }
526 #endif
527
528 /*
529 * Math-specific builtins that expands to constants.
530 * Versins here is for IEEE FP, vax needs its own versions.
531 */
532 #if TARGET_ENDIAN == TARGET_LE
533 static const unsigned char vFLOAT[] = { 0, 0, 0x80, 0x7f };
534 static const unsigned char vDOUBLE[] = { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f };
535 #ifdef LDBL_128
536 static const unsigned char vLDOUBLE[] = { 0,0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0x7f };
537 #else /* LDBL_80 */
538 static const unsigned char vLDOUBLE[] = { 0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0x7f };
539 #endif
540 static const unsigned char nFLOAT[] = { 0, 0, 0xc0, 0x7f };
541 static const unsigned char nDOUBLE[] = { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f };
542 #ifdef LDBL_128
543 static const unsigned char nLDOUBLE[] = { 0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0, 0xc0, 0xff, 0x7f };
544 #else /* LDBL_80 */
545 static const unsigned char nLDOUBLE[] = { 0, 0, 0, 0, 0, 0, 0, 0xc0, 0xff, 0x7f, 0, 0 };
546 #endif
547 #else
548 static const unsigned char vFLOAT[] = { 0x7f, 0x80, 0, 0 };
549 static const unsigned char vDOUBLE[] = { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 };
550 #ifdef LDBL_128
551 static const unsigned char vLDOUBLE[] = { 0x7f, 0xff, 0x80, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0 };
552 #else /* LDBL_80 */
553 static const unsigned char vLDOUBLE[] = { 0x7f, 0xff, 0x80, 0, 0, 0, 0, 0, 0, 0 };
554 #endif
555 static const unsigned char nFLOAT[] = { 0x7f, 0xc0, 0, 0 };
556 static const unsigned char nDOUBLE[] = { 0x7f, 0xf8, 0, 0, 0, 0, 0, 0 };
557 #ifdef LDBL_128
558 static const unsigned char nLDOUBLE[] = { 0x7f, 0xff, 0xc0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0 };
559 #else /* LDBL_80 */
560 static const unsigned char nLDOUBLE[] = { 0x7f, 0xff, 0xc0, 0, 0, 0, 0, 0, 0, 0 };
561 #endif
562 #endif
563
564 #define VALX(typ,TYP) { \
565 typ d; \
566 int x; \
567 NODE *f; \
568 x = MIN(sizeof(n ## TYP), sizeof(d)); \
569 memcpy(&d, v ## TYP, x); \
570 f = block(FCON, NIL, NIL, TYP, NULL, 0); \
571 f->n_dcon = fltallo(); \
572 ((FLT *)f->n_dcon)->fp = d; \
573 return f; \
574 }
575
576 static NODE *
577 builtin_huge_valf(const struct bitable *bt, NODE *a) VALX(float,FLOAT)
578 static NODE *
579 builtin_huge_val(const struct bitable *bt, NODE *a) VALX(double,DOUBLE)
580 static NODE *
581 builtin_huge_vall(const struct bitable *bt, NODE *a) VALX(long double,LDOUBLE)
582
583 #define builtin_inff builtin_huge_valf
584 #define builtin_inf builtin_huge_val
585 #define builtin_infl builtin_huge_vall
586
587 /*
588 * Return NANs, if reasonable.
589 */
590 static NODE *
591 builtin_nanx(const struct bitable *bt, NODE *a)
592 {
593 if (a == NULL || a->n_op == CM) {
594 uerror("%s bad argument", bt->name);
595 a = bcon(0);
596 } else if (a->n_op == STRING && *a->n_name == '\0') {
597 a->n_op = FCON;
598 a->n_type = bt->rt;
599 a->n_dcon = fltallo();
600 memcpy(&FCAST(a->n_dcon)->fp, nLDOUBLE, sizeof(long double));
601 } else
602 a = binhelp(eve(a), bt->rt, &bt->name[10]);
603 return a;
604 }
605
606 /*
607 * Target defines, to implement target versions of the generic builtins
608 */
609 #ifndef TARGET_MEMCMP
610 #define builtin_memcmp builtin_unimp
611 #endif
612 #ifndef TARGET_MEMCPY
613 #define builtin_memcpy builtin_unimp
614 #endif
615 #ifndef TARGET_MEMPCPY
616 #define builtin_mempcpy builtin_unimp
617 #endif
618 #ifndef TARGET_MEMSET
619 #define builtin_memset builtin_unimp
620 #endif
621
622 /* Reasonable type of size_t */
623 #ifndef SIZET
624 #if SZINT == SZSHORT
625 #define SIZET UNSIGNED
626 #elif SZLONG > SZINT
627 #define SIZET ULONG
628 #else
629 #define SIZET UNSIGNED
630 #endif
631 #endif
632
633 static TWORD memcpyt[] = { VOID|PTR, VOID|PTR, SIZET, INT };
634 static TWORD memsett[] = { VOID|PTR, INT, SIZET, INT };
635 static TWORD allocat[] = { SIZET };
636 static TWORD expectt[] = { LONG, LONG };
637 static TWORD strcmpt[] = { CHAR|PTR, CHAR|PTR };
638 static TWORD strcpyt[] = { CHAR|PTR, CHAR|PTR, INT };
639 static TWORD strncpyt[] = { CHAR|PTR, CHAR|PTR, SIZET, INT };
640 static TWORD strchrt[] = { CHAR|PTR, INT };
641 static TWORD strcspnt[] = { CHAR|PTR, CHAR|PTR };
642 static TWORD strspnt[] = { CHAR|PTR, CHAR|PTR };
643 static TWORD strpbrkt[] = { CHAR|PTR, CHAR|PTR };
644 static TWORD nant[] = { CHAR|PTR };
645 static TWORD bitt[] = { UNSIGNED };
646 static TWORD bitlt[] = { ULONG };
647 static TWORD bitllt[] = { ULONGLONG };
648 static TWORD abst[] = { INT };
649
650 static const struct bitable bitable[] = {
651 { "__builtin___memcpy_chk", builtin_unimp, 0, 4, memcpyt, VOID|PTR },
652 { "__builtin___mempcpy_chk", builtin_unimp, 0, 4, memcpyt, VOID|PTR },
653 { "__builtin___memmove_chk", builtin_unimp, 0, 4, memcpyt, VOID|PTR },
654 { "__builtin___memset_chk", builtin_unimp, 0, 4, memsett, VOID|PTR },
655
656 { "__builtin___strcat_chk", builtin_unimp, 0, 3, strcpyt, CHAR|PTR },
657 { "__builtin___strcpy_chk", builtin_unimp, 0, 3, strcpyt, CHAR|PTR },
658 { "__builtin___strncat_chk", builtin_unimp, 0, 4, strncpyt,CHAR|PTR },
659 { "__builtin___strncpy_chk", builtin_unimp, 0, 4, strncpyt,CHAR|PTR },
660
661 { "__builtin___printf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
662 { "__builtin___fprintf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
663 { "__builtin___sprintf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
664 { "__builtin___snprintf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
665 { "__builtin___vprintf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
666 { "__builtin___vfprintf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
667 { "__builtin___vsprintf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
668 { "__builtin___vsnprintf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
669
670 { "__builtin_alloca", builtin_alloca, 0, 1, allocat, VOID|PTR },
671 { "__builtin_abs", builtin_abs, 0, 1, abst, INT },
672 { "__builtin_clz", builtin_clz, 0, 1, bitt, INT },
673 { "__builtin_clzl", builtin_clzl, 0, 1, bitlt, INT },
674 { "__builtin_clzll", builtin_clzll, 0, 1, bitllt, INT },
675 { "__builtin_ctz", builtin_ctz, 0, 1, bitt, INT },
676 { "__builtin_ctzl", builtin_ctzl, 0, 1, bitlt, INT },
677 { "__builtin_ctzll", builtin_ctzll, 0, 1, bitllt, INT },
678 { "__builtin_ffs", builtin_ffs, 0, 1, bitt, INT },
679 { "__builtin_ffsl", builtin_ffsl, 0, 1, bitlt, INT },
680 { "__builtin_ffsll", builtin_ffsll, 0, 1, bitllt, INT },
681 { "__builtin_popcount", builtin_unimp, 0, 1, bitt, UNSIGNED },
682 { "__builtin_popcountl", builtin_unimp, 0, 1, bitlt, ULONG },
683 { "__builtin_popcountll", builtin_unimp, 0, 1, bitllt, ULONGLONG },
684
685 { "__builtin_constant_p", builtin_constant_p, 0, 1, 0, INT },
686 { "__builtin_expect", builtin_expect, 0, 2, expectt, LONG },
687 { "__builtin_memcmp", builtin_memcmp, 0, 3, memcpyt, INT },
688 { "__builtin_memcpy", builtin_memcpy, 0, 3, memcpyt, VOID|PTR },
689 { "__builtin_mempcpy", builtin_mempcpy, 0, 3, memcpyt, VOID|PTR },
690 { "__builtin_memset", builtin_memset, 0, 3, memsett, VOID|PTR },
691 { "__builtin_huge_valf", builtin_huge_valf, 0, 0, 0, FLOAT },
692 { "__builtin_huge_val", builtin_huge_val, 0, 0, 0, DOUBLE },
693 { "__builtin_huge_vall", builtin_huge_vall, 0, 0, 0, LDOUBLE },
694 { "__builtin_inff", builtin_inff, 0, 0, 0, FLOAT },
695 { "__builtin_inf", builtin_inf, 0, 0, 0, DOUBLE },
696 { "__builtin_infl", builtin_infl, 0, 0, 0, LDOUBLE },
697 { "__builtin_isgreater", builtin_isgreater, 0, 2, NULL, INT },
698 { "__builtin_isgreaterequal", builtin_isgreaterequal, 0, 2, NULL, INT },
699 { "__builtin_isless", builtin_isless, 0, 2, NULL, INT },
700 { "__builtin_islessequal", builtin_islessequal, 0, 2, NULL, INT },
701 { "__builtin_islessgreater", builtin_islessgreater, 0, 2, NULL, INT },
702 { "__builtin_isunordered", builtin_isunordered, 0, 2, NULL, INT },
703 { "__builtin_nanf", builtin_nanx, BTNOEVE, 1, nant, FLOAT },
704 { "__builtin_nan", builtin_nanx, BTNOEVE, 1, nant, DOUBLE },
705 { "__builtin_nanl", builtin_nanx, BTNOEVE, 1, nant, LDOUBLE },
706 { "__builtin_object_size", builtin_object_size, 0, 2, memsett, SIZET },
707 { "__builtin_prefetch", builtin_prefetch, 0, 1, memsett, VOID },
708 { "__builtin_strcmp", builtin_unimp, 0, 2, strcmpt, INT },
709 { "__builtin_strcpy", builtin_unimp, 0, 2, strcpyt, CHAR|PTR },
710 { "__builtin_stpcpy", builtin_unimp, 0, 2, strcpyt, CHAR|PTR },
711 { "__builtin_strchr", builtin_unimp, 0, 2, strchrt, CHAR|PTR },
712 { "__builtin_strlen", builtin_unimp, 0, 1, strcmpt, SIZET },
713 { "__builtin_strrchr", builtin_unimp, 0, 2, strchrt, CHAR|PTR },
714 { "__builtin_strncpy", builtin_unimp, 0, 3, strncpyt, CHAR|PTR },
715 { "__builtin_strncat", builtin_unimp, 0, 3, strncpyt, CHAR|PTR },
716 { "__builtin_strcspn", builtin_unimp, 0, 2, strcspnt, SIZET },
717 { "__builtin_strspn", builtin_unimp, 0, 2, strspnt, SIZET },
718 { "__builtin_strstr", builtin_unimp, 0, 2, strcmpt, CHAR|PTR },
719 { "__builtin_strpbrk", builtin_unimp, 0, 2, strpbrkt, CHAR|PTR },
720 #ifndef TARGET_STDARGS
721 { "__builtin_stdarg_start", builtin_stdarg_start, 0, 2, 0, VOID },
722 { "__builtin_va_start", builtin_stdarg_start, 0, 2, 0, VOID },
723 { "__builtin_va_arg", builtin_va_arg, BTNORVAL|BTNOPROTO, 2, 0, 0 },
724 { "__builtin_va_end", builtin_va_end, 0, 1, 0, VOID },
725 { "__builtin_va_copy", builtin_va_copy, 0, 2, 0, VOID },
726 #endif
727 #ifdef TARGET_BUILTINS
728 TARGET_BUILTINS
729 #endif
730 };
731
732 /*
733 * Check and cast arguments for builtins.
734 */
735 static int
736 acnt(NODE *a, int narg, TWORD *tp)
737 {
738 NODE *q;
739 TWORD t;
740
741 if (a == NIL)
742 return narg;
743 for (; a->n_op == CM; a = a->n_left, narg--) {
744 if (tp == NULL)
745 continue;
746 q = a->n_right;
747 t = ctype(tp[narg-1]);
748 if (q->n_type == t)
749 continue;
750 a->n_right = ccast(q, t, 0, NULL, 0);
751 }
752
753 /* Last arg is ugly to deal with */
754 if (narg == 1 && tp != NULL && a->n_type != tp[0]) {
755 q = talloc();
756 *q = *a;
757 q = ccast(q, ctype(tp[0]), 0, NULL, 0);
758 *a = *q;
759 nfree(q);
760 }
761 return narg != 1;
762 }
763
764 NODE *
765 builtin_check(struct symtab *sp, NODE *a)
766 {
767 const struct bitable *bt;
768
769 if (sp->soffset < 0 ||
770 sp->soffset >= (int)(sizeof(bitable)/sizeof(bitable[0])))
771 cerror("builtin_check");
772
773 bt = &bitable[sp->soffset];
774 if ((bt->flags & BTNOEVE) == 0)
775 a = eve(a);
776 if (((bt->flags & BTNOPROTO) == 0) && acnt(a, bt->narg, bt->tp)) {
777 uerror("wrong argument count to %s", bt->name);
778 return bcon(0);
779 }
780 return (*bt->fun)(bt, a);
781 }
782
783 /*
784 * Put all builtin functions into the global symbol table.
785 */
786 void
787 builtin_init()
788 {
789 const struct bitable *bt;
790 NODE *p = block(TYPE, 0, 0, 0, 0, 0);
791 struct symtab *sp;
792 int i;
793
794 for (i = 0; i < (int)(sizeof(bitable)/sizeof(bitable[0])); i++) {
795 bt = &bitable[i];
796 sp = lookup(addname(bt->name), 0);
797 if (bt->rt == 0 && (bt->flags & BTNORVAL) == 0)
798 cerror("function '%s' has no return type", bt->name);
799 p->n_type = INCREF(bt->rt) + (FTN-PTR);
800 p->n_sp = sp;
801 defid(p, EXTDEF);
802 sp->soffset = i;
803 sp->sflags |= SBUILTIN;
804 }
805 nfree(p);
806 }
807 #endif