Skip to content

Commit

Permalink
Revert "Revert "fix Issue 23875 23880 (#15172)" (#15196)"
Browse files Browse the repository at this point in the history
This reverts commit 7da9317.
  • Loading branch information
WalterBright committed May 11, 2023
1 parent 2547f10 commit 3508482
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 18 deletions.
62 changes: 44 additions & 18 deletions compiler/src/dmd/cparse.d
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ final class CParser(AST) : Parser!AST
// atomic-type-specifier or type_qualifier
case TOK._Atomic:

case TOK.__attribute__:

Ldeclaration:
{
cparseDeclaration(LVL.local);
Expand Down Expand Up @@ -2710,20 +2712,34 @@ final class CParser(AST) : Parser!AST
*
* Params:
* declarator = declarator kind
* t = base type to start with
* tbase = base type to start with
* pident = set to Identifier if there is one, null if not
* specifier = specifiers in and out
* Returns:
* type declared. If a TypeFunction is returned, this.symbols is the
* symbol table for the parameter-type-list, which will contain any
* declared struct, union or enum tags.
*/
private AST.Type cparseDeclarator(DTR declarator, AST.Type t,
private AST.Type cparseDeclarator(DTR declarator, AST.Type tbase,
out Identifier pident, ref Specifier specifier)
{
//printf("cparseDeclarator(%d, %p)\n", declarator, t);
AST.Types constTypes; // all the Types that will need `const` applied to them

/* Insert tx -> t into
* ts -> ... -> t
* so that
* ts -> ... -> tx -> t
*/
static void insertTx(ref AST.Type ts, AST.Type tx, AST.Type t)
{
AST.Type* pt;
for (pt = &ts; *pt != t; pt = &(cast(AST.TypeNext)*pt).next)
{
}
*pt = tx;
}

AST.Type parseDecl(AST.Type t)
{
AST.Type ts;
Expand Down Expand Up @@ -2789,20 +2805,6 @@ final class CParser(AST) : Parser!AST
// parse DeclaratorSuffixes
while (1)
{
/* Insert tx -> t into
* ts -> ... -> t
* so that
* ts -> ... -> tx -> t
*/
static void insertTx(ref AST.Type ts, AST.Type tx, AST.Type t)
{
AST.Type* pt;
for (pt = &ts; *pt != t; pt = &(cast(AST.TypeNext)*pt).next)
{
}
*pt = tx;
}

switch (token.value)
{
case TOK.leftBracket:
Expand Down Expand Up @@ -2915,7 +2917,17 @@ final class CParser(AST) : Parser!AST
return ts;
}

t = parseDecl(t);
auto t = parseDecl(tbase);

if (specifier.vector_size)
{
auto length = new AST.IntegerExp(token.loc, specifier.vector_size / tbase.size(), AST.Type.tuns32);
auto tsa = new AST.TypeSArray(tbase, length);
AST.Type tv = new AST.TypeVector(tsa);
specifier.vector_size = 0; // used it up

insertTx(t, tv, tbase); // replace tbase with tv
}

/* Because const is transitive, cannot assemble types from
* fragments. Instead, types to be annotated with const are put
Expand Down Expand Up @@ -3553,7 +3565,19 @@ final class CParser(AST) : Parser!AST
{
nextToken();
check(TOK.leftParenthesis);
cparseConstantExp(); // TODO implement
if (token.value == TOK.int32Literal)
{
const n = token.unsvalue;
if (n < 1 || n & (n - 1) || ushort.max < n)
error("__attribute__((vector_size(%lld))) must be an integer positive power of 2 and be <= 32,768", cast(ulong)n);
specifier.vector_size = cast(uint) n;
nextToken();
}
else
{
error("value for vector_size expected, not `%s`", token.toChars());
nextToken();
}
check(TOK.rightParenthesis);
}
else
Expand Down Expand Up @@ -4694,6 +4718,7 @@ final class CParser(AST) : Parser!AST
// atomic-type-specifier
case TOK._Atomic:
case TOK.typeof_:
case TOK.__attribute__:
t = peek(t);
if (t.value != TOK.leftParenthesis ||
!skipParens(t, &t))
Expand Down Expand Up @@ -4959,6 +4984,7 @@ final class CParser(AST) : Parser!AST
bool dllexport; /// dllexport attribute
bool _deprecated; /// deprecated attribute
AST.Expression depMsg; /// deprecated message
uint vector_size; /// positive power of 2 multipe of base type size

SCW scw; /// storage-class specifiers
MOD mod; /// type qualifiers
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dmd/initsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,9 @@ extern(C++) Initializer initializerSemantic(Initializer init, Scope* sc, ref Typ
*/
t = t.toBasetype();

if (auto tv = t.isTypeVector())
t = tv.basetype;

/* If `{ expression }` return the expression initializer
*/
ExpInitializer isBraceExpression()
Expand Down
28 changes: 28 additions & 0 deletions compiler/test/compilable/test23875.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* DISABLED: win32 linux32
*/

// https://issues.dlang.org/show_bug.cgi?id=23875
// https://issues.dlang.org/show_bug.cgi?id=23880

int __attribute__((vector_size(16))) neptune()
{
int __attribute__((vector_size (16))) v = { 4,1,2,3 };
return v;
}

__attribute__((__vector_size__(16))) int pluto(int i)
{
int __attribute__((__vector_size__ (16))) * p1;
int * __attribute__((__vector_size__ (16))) p2;

int __attribute__((__vector_size__ (16))) v1;
__attribute__((__vector_size__ (16))) int v2;

v1 = (__attribute__((__vector_size__ (16))) int) {4,1,2,3};

p1 = p2;
*p1 = v1;
v1 = (__attribute__((__vector_size__ (16))) int) v2;

return i ? v1 : v2;
}
13 changes: 13 additions & 0 deletions compiler/test/fail_compilation/test23875.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* DISABLED: win32 linux32
TEST_OUTPUT:
---
fail_compilation/test23875.c(12): Error: __attribute__((vector_size(10))) must be an integer positive power of 2 and be <= 32,768
fail_compilation/test23875.c(13): Error: value for vector_size expected, not `x`
---
*/

// https://issues.dlang.org/show_bug.cgi?id=23875
// https://issues.dlang.org/show_bug.cgi?id=23880

int __attribute__((vector_size(10))) neptune();
int __attribute__((vector_size(x))) saturn();
1 change: 1 addition & 0 deletions druntime/src/importc.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#define __volatile__ volatile
#define __attribute __attribute__
#define __alignof _Alignof
#define __vector_size__ vector_size

/********************
* Clang nullability extension used by macOS headers.
Expand Down

0 comments on commit 3508482

Please sign in to comment.