How well can Ada optimize static constant arrays?
20 October 2022 at 04:26
Let's say I declare an array of constant values for use as a lookup table:
function Square(num : Integer) return Integer is
use Ada.Text_IO;
type LookupItemsRange is range 1..10;
type LookupTable is array(LookupItemsRange) of integer;
A : constant LookupTable := (2312,2,3,4,5,6,7,8,9, 10);
begin
Sqr := A(1);
Sqr := 2 * A(2);
return Sqr;
end Square;
The LookupTable shouldn't use any space. The compiler should be able to substitute array access with the actual inlined values. At the highest optimization setting, here's what Ada yields:
_ada_square:
li a0,4 #,
ret
I am planning on substituting a large amount of redundant MCU code with a cleaner lookup table implementation. Since the microcontroller's resources are constrained, I would like to avoid needless allocation and indexing.
Under what circumstances will Ada allocate the array or am I completely safe?