align.c revision 1
1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1985-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* Phong Vo <kpv@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * Glenn Fowler
1N/A * AT&T Bell Laboratories
1N/A *
1N/A * generate align features
1N/A *
1N/A * NOTE: two's complement binary integral representation assumed
1N/A */
1N/A
1N/A#include "FEATURE/common"
1N/A
1N/A#include <setjmp.h>
1N/A
1N/Aunion _u_
1N/A{
1N/A long u1;
1N/A char* u2;
1N/A double u3;
1N/A char u4[1024];
1N/A intmax_t u5;
1N/A uintmax_t u6;
1N/A _ast_fltmax_t u7;
1N/A void* u8;
1N/A char* (*u9)();
1N/A jmp_buf u10;
1N/A};
1N/A
1N/Astruct _s_
1N/A{
1N/A char s1;
1N/A union _u_ s2;
1N/A};
1N/A
1N/A#define roundof(x,y) (((x)+((y)-1))&~((y)-1))
1N/A
1N/Astatic union _u_ u;
1N/Astatic union _u_ v;
1N/A
1N/Aint
1N/Amain()
1N/A{
1N/A register int i;
1N/A register int j;
1N/A register int k;
1N/A
1N/A int align0;
1N/A int align1;
1N/A int align2;
1N/A unsigned long bit1;
1N/A unsigned long bit2;
1N/A unsigned long bits0;
1N/A unsigned long bits1;
1N/A unsigned long bits2;
1N/A
1N/A u.u2 = u.u4;
1N/A v.u2 = u.u2 + 1;
1N/A bit1 = u.u1 ^ v.u1;
1N/A v.u2 = u.u2 + 2;
1N/A bit2 = u.u1 ^ v.u1;
1N/A align0 = sizeof(struct _s_) - sizeof(union _u_);
1N/A bits0 = 0;
1N/A k = 0;
1N/A for (j = 0; j < align0; j++)
1N/A {
1N/A u.u2 = u.u4 + j;
1N/A bits1 = 0;
1N/A for (i = 0; i < align0; i++)
1N/A {
1N/A v.u2 = u.u2 + i;
1N/A bits1 |= u.u1 ^ v.u1;
1N/A }
1N/A if (!bits0 || bits1 < bits0)
1N/A {
1N/A bits0 = bits1;
1N/A k = j;
1N/A }
1N/A }
1N/A align1 = roundof(align0, 2);
1N/A u.u2 = u.u4 + k;
1N/A for (bits1 = bits0; i < align1; i++)
1N/A {
1N/A v.u2 = u.u2 + i;
1N/A bits1 |= u.u1 ^ v.u1;
1N/A }
1N/A align2 = roundof(align0, 4);
1N/A for (bits2 = bits1; i < align2; i++)
1N/A {
1N/A v.u2 = u.u2 + i;
1N/A bits2 |= u.u1 ^ v.u1;
1N/A }
1N/A printf("typedef unsigned %s ALIGN_INTEGRAL;\n", sizeof(char*) >= sizeof(long) ? "long" : sizeof(char*) >= sizeof(int) ? "int" : "short");
1N/A printf("\n");
1N/A printf("#define ALIGN_CHUNK %d\n", sizeof(char*) >= 4 ? 8192 : 1024);
1N/A printf("#define ALIGN_INTEGRAL %s\n", sizeof(char*) >= sizeof(long) ? "long" : sizeof(char*) >= sizeof(int) ? "int" : "short");
1N/A printf("#define ALIGN_INTEGER(x) ((ALIGN_INTEGRAL)(x))\n");
1N/A printf("#define ALIGN_POINTER(x) ((char*)(x))\n");
1N/A if (bits2 == (align2 - 1)) printf("#define ALIGN_ROUND(x,y) ALIGN_POINTER(ALIGN_INTEGER((x)+(y)-1)&~((y)-1))\n");
1N/A else printf("#define ALIGN_ROUND(x,y) ALIGN_POINTER(ALIGN_INTEGER(ALIGN_ALIGN(x)+(((y)+%d)/%d)-1)&~((((y)+%d)/%d)-1))\n", align0, align0, align0, align0);
1N/A printf("\n");
1N/A if (align0 == align2)
1N/A {
1N/A printf("#define ALIGN_BOUND ALIGN_BOUND2\n");
1N/A printf("#define ALIGN_ALIGN(x) ALIGN_ALIGN2(x)\n");
1N/A printf("#define ALIGN_TRUNC(x) ALIGN_TRUNC2(x)\n");
1N/A }
1N/A else if (align0 == align1)
1N/A {
1N/A printf("#define ALIGN_BOUND ALIGN_BOUND1\n");
1N/A printf("#define ALIGN_ALIGN(x) ALIGN_ALIGN1(x)\n");
1N/A printf("#define ALIGN_TRUNC(x) ALIGN_TRUNC1(x)\n");
1N/A }
1N/A else
1N/A {
1N/A printf("#define ALIGN_BOUND 1\n");
1N/A printf("#define ALIGN_ALIGN(x) ALIGN_POINTER(x)\n");
1N/A printf("#define ALIGN_TRUNC(x) ALIGN_POINTER(x)\n");
1N/A }
1N/A printf("\n");
1N/A printf("#define ALIGN_BIT1 0x%lx\n", bit1);
1N/A if (align1 == align2)
1N/A {
1N/A printf("#define ALIGN_BOUND1 ALIGN_BOUND2\n");
1N/A printf("#define ALIGN_ALIGN1(x) ALIGN_ALIGN2(x)\n");
1N/A printf("#define ALIGN_TRUNC1(x) ALIGN_TRUNC2(x)\n");
1N/A }
1N/A else
1N/A {
1N/A printf("#define ALIGN_BOUND1 %d\n", align1);
1N/A printf("#define ALIGN_ALIGN1(x) ALIGN_TRUNC1((x)+%d)\n", align1 - 1);
1N/A printf("#define ALIGN_TRUNC1(x) ALIGN_POINTER(ALIGN_INTEGER((x)+%d)&0x%lx)\n", align1 - 1, ~(bits0|bits1));
1N/A }
1N/A printf("#define ALIGN_CLRBIT1(x) ALIGN_POINTER(ALIGN_INTEGER(x)&0x%lx)\n", ~bit1);
1N/A printf("#define ALIGN_SETBIT1(x) ALIGN_POINTER(ALIGN_INTEGER(x)|0x%lx)\n", bit1);
1N/A printf("#define ALIGN_TSTBIT1(x) ALIGN_POINTER(ALIGN_INTEGER(x)&0x%lx)\n", bit1);
1N/A printf("\n");
1N/A printf("#define ALIGN_BIT2 0x%lx\n", bit2);
1N/A printf("#define ALIGN_BOUND2 %d\n", align2);
1N/A printf("#define ALIGN_ALIGN2(x) ALIGN_TRUNC2((x)+%d)\n", align2 - 1);
1N/A printf("#define ALIGN_TRUNC2(x) ALIGN_POINTER(ALIGN_INTEGER(x)&0x%lx)\n", ~(bits0|bits1|bits2));
1N/A printf("#define ALIGN_CLRBIT2(x) ALIGN_POINTER(ALIGN_INTEGER(x)&0x%lx)\n", ~bit2);
1N/A printf("#define ALIGN_SETBIT2(x) ALIGN_POINTER(ALIGN_INTEGER(x)|0x%lx)\n", bit2);
1N/A printf("#define ALIGN_TSTBIT2(x) ALIGN_POINTER(ALIGN_INTEGER(x)&0x%lx)\n", bit2);
1N/A printf("\n");
1N/A return 0;
1N/A}