Vector indexing patch

Artem Shinkarov artyom.shinkaroff@gmail.com
Tue Jun 1 20:21:00 GMT 2010


This is a reworked patch of Andrew Pinski "Subscripting on vector
types" in terms of GSoC 2010 [Artjoms Sinkarovs].

This patch allows to index individual elements of vector type in C.
For example: vec[i], where vec is a vector with a base type T and i is
an integer type.
If i is a constant then we construct a BIT_FIELD_REF node considering
boundchecking, otherwise we expand it to (((T *)&(vec))[i]).

If the vector type variable has a register storage class then it can
be accessed only via constant index.

Changelog:
gcc/c-common.h: helper function c_build_vector_indexed_by_cst added
gcc/c-common.c: helper function c_build_vector_indexed_by_cst added
gcc/c-typeck.c: build_array_ref modified to handle vector indexing,
lvalue_p modified for considering vector element as lvalue.

gcc/testsuite/gcc.c-torture/execute/vector-subscript-1.c: test taken
from A.Pinski patch
gcc/testsuite/gcc.c-torture/execute/vector-subscript-2.c: test taken
from A.Pinski patch

gcc/testsuite/gcc.dg/vector-subscript-3.c: new test
gcc/testsuite/gcc.dg/array-8.c: Update error message about subscripting
gcc/testsuite/gcc.dg/vector-subscript-1.c: test taken from A.Pinski patch
gcc/testsuite/gcc.dg/vector-subscript-2.c: test taken from A.Pinski patch

Teseted on x86_64-unknown-linux-gnu.

The documentation was not changed. May be there could be some more
ideas to implement.
The patch can be found in attachment.


_______________________ C++ related question _______________________

Question about C++ frontend people. I would like to implement the same
functionality in C++ frontend and typechecker as well. However the
problem is that at the moment I have the same code in typechecker and
in the decl2.c file (part of frontend). The code looks like this:

Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c	(revision 159699)
+++ gcc/cp/typeck.c	(working copy)
@@ -2912,7 +2912,30 @@
     default:
       break;
     }
+
+  /* For vector[index], convert the vector to a pointer of the underlying
+     type. */

+  if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
+    {
+      tree type = TREE_TYPE (array);
+      tree type1;
+
+      if (TREE_CODE (idx) == INTEGER_CST)
+        c_build_vector_indexed_by_cst (loc, array, idx);
+      else
+        {
+          /* Mark the vector as addressable but ignore the
+             register storage class.  */
+          cxx_mark_addressable (array);
+          type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
+          type = build_pointer_type (type);
+          type1 = build_pointer_type (TREE_TYPE (array));
+          array = build1 (ADDR_EXPR, type1, array);
+          array = convert (type, array);
+        }
+    }
+
   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
     {
       tree rval, type;
Index: gcc/cp/decl2.c
===================================================================
--- gcc/cp/decl2.c	(revision 159699)
+++ gcc/cp/decl2.c	(working copy)
@@ -365,7 +365,30 @@
   else
     {
       tree p1, p2, i1, i2;
+      /* For vector[index], convert the vector to a pointer of the underlying
+	 type. */
+      if (TREE_CODE (type) == VECTOR_TYPE)
+	{
+          tree type = TREE_TYPE (array_expr);
+          tree type1;

+          /* If constant index is in bound of vector type */
+          if (TREE_CODE (index_exp) == INTEGER_CST)
+             c_build_vector_indexed_by_cst (input_location, array_expr,
+                                            index_exp);
+          else
+            {
+              /* Mark the vector as addressable but ignore the
+                 register storage class.  */
+              cxx_mark_addressable (array_expr);
+              type = build_qualified_type (TREE_TYPE (type),
TYPE_QUALS (type));
+              type = build_pointer_type (type);
+              type1 = build_pointer_type (TREE_TYPE (array_expr));
+              array_expr = build1 (ADDR_EXPR, type1, array_expr);
+              array_expr = convert (type, array_expr);
+            }
+        }
+
       /* Otherwise, create an ARRAY_REF for a pointer or array type.
 	 It is a little-known fact that, if `a' is an array and `i' is


My question is whether I can be sure that the tree that I'll have in
typeck.c will be passed through the decl2.c? In that case I will be
able to avoid code doubling in typeck.c just skipping this check.
Because I can be sure that the code will be checked in decl2.c.

Can anyone give me a quick description of the C++ front end. Or point
me to the right document.


-- 
Thank you,
Artem Shinkarov
-------------- next part --------------
A non-text attachment was scrubbed...
Name: subscript.c.diff
Type: text/x-diff
Size: 9692 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20100601/d4dfaeb6/attachment.bin>


More information about the Gcc mailing list