[cfe-commits] [REVIEW] - Altivec vector literal casting problem - need feedback - AST/Sema

John Thompson john.thompson.jtsoftware at gmail.com
Fri Jun 25 16:48:15 PDT 2010


I have the first pass at a fix for bug 6895, which I also reported.
However, because of possible problems in altivec.h that are being looked
into, it's not quite ready for prime time.  However, I'm hoping for some
preliminary feedback to see if I'm on the right track.

First, some background on the problem.

Basically, the following produced an "cannot initialize a vector element of
type '(type)'" error:

vector char v1 = (vector char)((vector int)(1, 2, 3, 4));

This was because the outer paren expression was trying to make a vector
literal out of the inner, instead of making it a cast.  (See the original
AltiVec standard at
http://www.freescale.com/files/32bit/doc/ref_manual/ALTIVECPIM.pdf, section
2.5.2, where it actually discusses this case.)

After straining my brain to figure out where the fix should go (I think it
should be ActOnCastOfParenListExpr), this patch is what I came up with.

Because I needed some way to determine if the initializers were arithmetic
constants or not, I added a function to Expr, isArithmeticConstantExpr,
which calls a static function of the same name, which I hacked up from a
copy of the CheckICE function used by the isIntegerConstantExpression
function.  (Is this plagerism?).  I'm not sure if it's exactly right,
complete, or is overkill, but it seems to work for the few cases I've
tried.  Is it reasonable to put it in Expr, or should it just be a static
function in the one file I use it in?

Here a sample of the AST produced from this:

vector int v1 = (vector int)(1, 2, 3, 4);
vector char v2 = (vector char)((vector int)(1, 2, 3, 4));
From: clang -cc1 -triple=powerpc-apple-darwin8 -faltivec -D__ALTIVEC_H
-ast-dump vecliteralcast1.c >vecliteralcast1.txt 2>&1

(The -D__ALTIVEC_H argument disables the altivec.h header implicitly
included.)

The output:

typedef char *__builtin_va_list;
__vector int v1 = (CompoundLiteralExpr 0x2e7b0f8 <vecliteralcast1.c:1:17,
col:28> '__vector int'
  (InitListExpr 0x2e7b188 <col:17, col:28> '__vector int'
    (IntegerLiteral 0x2e51730 <col:30> 'int' 1)
    (IntegerLiteral 0x2e4f068 <col:33> 'int' 2)
    (IntegerLiteral 0x2e4f0c0 <col:36> 'int' 3)
    (IntegerLiteral 0x2e7aff0 <col:39> 'int' 4)))
;
__vector char v2 = (CStyleCastExpr 0x2e7b748 <vecliteralcast1.c:2:18,
col:56> '__vector char' <BitCast>
  (ParenExpr 0x2e7b700 <col:31, col:56> '__vector int'
    (CompoundLiteralExpr 0x2e7b580 <col:32, col:43> '__vector int'
      (InitListExpr 0x2e7b610 <col:32, col:43> '__vector int'
        (IntegerLiteral 0x2e7b330 <col:45> 'int' 1)
        (IntegerLiteral 0x2e7b388 <col:48> 'int' 2)
        (IntegerLiteral 0x2e7b3e0 <col:51> 'int' 3)
        (IntegerLiteral 0x2e7b438 <col:54> 'int' 4)))))
;
Thanks.

-John

-- 
John Thompson
John.Thompson.JTSoftware at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20100625/d4b48557/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: veclitcast.patch
Type: application/octet-stream
Size: 13115 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20100625/d4b48557/attachment.obj>


More information about the cfe-commits mailing list