This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Improved compile time eval of length attribute


This patch improves the compiler to catch some additional cases of
the length (and range_length) attributes applied to arrays and
slices, resulting in elimination of run time checks and improved
compile time warnings. The change involves a new version of the
routine Compile_Time_Compare in Sem_Eval which returns not only
GT and LT, but where possible the compile-time known difference.
Then this improved routine is used in Sem_Attr when evaluating
Length and Range_Length attributes.

Consider this test program:

procedure LengthEval (X : out String) is
begin
   X (X'First .. X'First) := "a";
   X (X'First .. X'First) := "aa";
end LengthEval;

Prior to this patch, both assignments did a run time length check.
With the patch, compiling with -gnatj60 -gnatG, we get:

Source recreated from tree for Lengtheval (body)


procedure lengtheval (x : out string) is
   subtype lengtheval__S1b is string (x'first(1) .. x'last(1));
begin
   [constraint_error when
     x'first > x'last(1)
     "range check failed"]
   [subtype lengtheval__T5b is integer range x'first .. x'first]
   [subtype lengtheval__T6b is string (lengtheval__T5b)]
   x ({x'first .. x'first}) := lengtheval__T6b!("a");
   [constraint_error when
     x'first > x'last(1)
     "range check failed"]
   [constraint_error "length check failed"]
   x ({x'first .. x'first}) := [constraint_error "length check failed"];
   return;
end lengtheval;

lengtheval.adb:4:30: warning: wrong length for array of
                     subtype of "Standard.String" defined
                     at line 4, "Constraint_Error" will be
                     raised at run time

and the run time check is eliminate in both cases, with an
appropriate compile time warning in the second case.

Tested on x86_64-pc-linux-gnu, committed on trunk

2009-04-20  Robert Dewar  <dewar@adacore.com>

	* sem_attr.adb (Eval_Attribute, case Length): Catch more cases where
	this attribute can be evaluated at compile time.
	(Eval_Attribute, case Range_Length): Same improvement

	* sem_eval.ads, sem_eval.adb (Compile_Time_Compare): New procedure

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]