Annotation of researchv9/cmd/cfront/szal.c, revision 1.1

1.1     ! root        1: 
        !             2: #include "stdio.h"
        !             3: /*
        !             4:        C program to run on a machine to write a size/align file for
        !             5:        the C++ translator.
        !             6: 
        !             7:        Most output line are on the form:
        !             8:                typeX   sizeof(typeX)   alignment_requirement_for_typeX
        !             9: */
        !            10: 
        !            11: /*
        !            12:        assumes
        !            13: 
        !            14:                that ``double d0; char c0;'' poses the worst alignment condition
        !            15: 
        !            16:                two's complement integer representation
        !            17: 
        !            18:                that a word is defined by a :0 field
        !            19: */
        !            20: 
        !            21: typedef int (*PF)();
        !            22: struct st1 { char a; };
        !            23: struct ss {
        !            24:        double a0; char c0;
        !            25:        char c1;
        !            26:        double a00; char c00;
        !            27:        short s1;
        !            28:        double a2; char c2;
        !            29:        int i1;
        !            30:        double a3; char c3;
        !            31:        long l1;
        !            32:        double a4; char c4;
        !            33:        float f1;
        !            34:        double a6; char c5;
        !            35:        double d1;
        !            36:        double a7; char c6;
        !            37:        char* p1;
        !            38:        double a8; char c7;
        !            39:        struct ss * p2;
        !            40:        double a9; char c8;
        !            41:        struct st1 oo;
        !            42:        double a10; char c9;
        !            43:        PF pf;
        !            44: } oo;
        !            45: struct st5 { char a; int :0; };        /* by definition: a word */
        !            46: struct st2 { char :2; };
        !            47: struct st3 { int :2; };
        !            48: struct st4 { char :2; char :2; };
        !            49: 
        !            50: struct st6 { char v[3]; char : 2; };   /* fits in 4 bytes */
        !            51: struct st7 { char v[3]; int : 2; };    /* might not */
        !            52: struct st8 { char v[3]; char : 2; char : 2; };
        !            53: 
        !            54: struct st9 { char v[7]; char : 2; };   /* fits in 8 bytes */
        !            55: struct st10 { char v[7]; int : 2; };   /* might not */
        !            56: struct st11 { char v[7]; char : 2; char : 2; };
        !            57: 
        !            58: out(s,a1,a2,p) char* s; int a1; int a2; char* p;
        !            59: {
        !            60:        printf("%s\t%d\t%d\t%s\n",s,a1,a2,p?p:"");
        !            61: }
        !            62: 
        !            63: int a123456789 = 1;    /* if this does not compile get a better C compiler */
        !            64: int a123456780 = 2;
        !            65: 
        !            66: 
        !            67: main()
        !            68: {
        !            69:        char largest[50];
        !            70:        char c = 1;
        !            71:        int i1 = 0;
        !            72:        int i2 = 0;
        !            73:        int i = 1;
        !            74: 
        !            75:        if (a123456789 == a123456780)
        !            76:                fprintf(stderr,"Warning: Your C compiler is dangerous.\nIt strips trailing characters off long identifiers without warning.\nGet a new one\n");
        !            77: 
        !            78:        while (c) { c<<=1; c&=~1; i1++; }       /* i1 = #bits in byte */
        !            79: 
        !            80:        if (sizeof(struct st5) == sizeof(char)) /* i2 = #bits in word  */
        !            81:                i2 = i1;
        !            82:        else if (sizeof(struct st5) == sizeof(short)) {
        !            83:                short i = 1;
        !            84:                while (i) { i<<=1; i&=~1; i2++; }
        !            85:        }
        !            86:        else if (sizeof(struct st5) == sizeof(int))
        !            87:                while (i) { i<<=1; i&=~1; i2++; }
        !            88:        else if (sizeof(struct st5) == sizeof(long)) {
        !            89:                long i = 1;
        !            90:                while (i) { i<<=1; i&=~1; i2++; }
        !            91:        }
        !            92:        else {
        !            93:                fprintf(stderr,"Warning: Your C compiler probably handles 0 lengths fields wrong\n");
        !            94:                i = sizeof(int);
        !            95:        }
        !            96: 
        !            97:        out("bit",i1,i2,0);
        !            98:        out("word",sizeof(struct st5),sizeof(struct st5),0);
        !            99:        out("char",sizeof(char),(int)&oo.c1-(int)&oo.c0,0);
        !           100:        out("short",sizeof(short),(int)&oo.s1-(int)&oo.c00,0);
        !           101:        i = ((unsigned)~0)>>1;
        !           102:        sprintf(largest,"%d",i);        /* largest integer */
        !           103:        out("int",sizeof(int),(int)&oo.i1-(int)&oo.c2,largest);
        !           104:        out("long",sizeof(long),(int)&oo.l1-(int)&oo.c3,0);
        !           105:        out("float",sizeof(float),(int)&oo.f1-(int)&oo.c4,0);
        !           106:        out("double",sizeof(double),(int)&oo.d1-(int)&oo.c5,0);
        !           107:        i = 1<<(sizeof(char*)*i1-2); 
        !           108:        if (i<400*1024)
        !           109:                fprintf(stderr,"Pointers to data too small to handle C++\n");
        !           110:        out("bptr",sizeof(char*),(int)&oo.p1-(int)&oo.c6,0);
        !           111:        out("wptr",sizeof(struct ss *),(int)&oo.p2-(int)&oo.c7,0);
        !           112:        i = 1<<(sizeof(PF)*i1-2); 
        !           113:        if (i<250*1024)
        !           114:                fprintf(stderr,"Pointers to functions too small to handle C++\n");
        !           115: /*     out("fptr",sizeof(PF),(int)&oo.pf-(int)&oo.c9,0);
        !           116: */
        !           117:        if (sizeof(PF)!=sizeof(struct ss*))
        !           118:                fprintf(stderr,"Cannot handle sizeof(pointer to function) != sizeof(pointer to struct)\n");
        !           119:        out("struct",sizeof(struct st1),(int)&oo.oo-(int)&oo.c8,0);
        !           120:        switch (sizeof(struct st1)) {
        !           121:        case 1:
        !           122:                i1 = sizeof(struct st2)!=sizeof(struct st3);
        !           123:                i2 = sizeof(struct st2)==sizeof(struct st4);
        !           124:                break;
        !           125:        case 2:
        !           126:                i1 = sizeof(struct st6)!=sizeof(struct st7);
        !           127:                i2 = sizeof(struct st6)==sizeof(struct st8);
        !           128:                break;
        !           129:        case 4:
        !           130:                i1 = sizeof(struct st9)!=sizeof(struct st10);
        !           131:                i2 = sizeof(struct st9)==sizeof(struct st11);
        !           132:                break;
        !           133:        default:
        !           134:                fprintf(stderr,"Cannot figure out if field sizes are sensitive to the type of fields\n");
        !           135:        }
        !           136:        out("struct2",i1 /* sensitive to field type */,i2 /* packs fields */,0);
        !           137: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.