]> Cypherpunks repositories - gostls13.git/commitdiff
Implement unsafe.Alignof.
authorIan Lance Taylor <iant@golang.org>
Tue, 10 Feb 2009 19:46:26 +0000 (11:46 -0800)
committerIan Lance Taylor <iant@golang.org>
Tue, 10 Feb 2009 19:46:26 +0000 (11:46 -0800)
R=ken
DELTA=20  (19 added, 0 deleted, 1 changed)
OCL=24719
CL=24771

src/cmd/gc/dcl.c
src/cmd/gc/sysimport.c
src/cmd/gc/unsafe.go

index d5d3a9bf4df19f625a5bba8da87d7e7fa4ad0011..35d1a8e62b64e7c2e8c9a17486eca52fca921f40 100644 (file)
@@ -1495,6 +1495,7 @@ unsafenmagic(Node *l, Node *r)
 {
        Node *n;
        Sym *s;
+       Type *t;
        long v;
        Val val;
 
@@ -1519,7 +1520,23 @@ unsafenmagic(Node *l, Node *r)
                if(r->op != ODOT && r->op != ODOTPTR)
                        goto no;
                walktype(r, Erv);
-               v = n->xoffset;
+               v = r->xoffset;
+               goto yes;
+       }
+       if(strcmp(s->name, "Alignof") == 0) {
+               walktype(r, Erv);
+               if (r->type == T)
+                       goto no;
+               // make struct { byte; T; }
+               t = typ(TSTRUCT);
+               t->type = typ(TFIELD);
+               t->type->type = types[TUINT8];
+               t->type->down = typ(TFIELD);
+               t->type->down->type = r->type;
+               // compute struct widths
+               dowidth(t);
+               // the offset of T is its required alignment
+               v = t->type->down->width;
                goto yes;
        }
 
index 4d682d675d0e2a053bfc8e57b5a8762fcdbddf5f..ccc38343d7ed5dedd1f84f6e8ef6b292ffd7dd86 100644 (file)
@@ -67,5 +67,6 @@ char *unsafeimport =
        "type unsafe.Pointer *any\n"
        "func unsafe.Offsetof (? any) (? int)\n"
        "func unsafe.Sizeof (? any) (? int)\n"
+       "func unsafe.Alignof (? any) (? int)\n"
        "\n"
        "$$\n";
index 47703f6e0ffb1d65cec8340fdf8a2fee054b92d5..d1dcee02a83fbd6376aeb14333eed4b800936a4d 100644 (file)
@@ -8,3 +8,4 @@ package PACKAGE
 type   Pointer *any;
 func   Offsetof(any) int;
 func   Sizeof(any) int;
+func   Alignof(any) int;