0

We are basically using sparc architecture and our language is Ada we are using gnat compiler to compile our code. We observed something funny.

Some of the constant in our code are having two or more copies.

file.adb:

With FileConsts; USE FileConsts
Procedure SomeProcedure is
A : LONG_FLOAT;
Begin
    A := cSomeConstant;
End SomeProcedure;

FileConsts.ads

cSomeConstant : CONSTANT LONG_FLOAT := 100.0;

In the Map file we have basically

.rodata 0x40010000 (0x8)file.o
.rodata 0x40010008 pfileconsts__csomeconstant

In the Assembly it is accessing the area of file.o, i.e 0x40010000 instead if 0x40010008. In the binary file the value at 0x40010000 and 0x40010008 is actually same, so program is behaving as expected. But why would the compiler do that

If any other package(file2.adb) also accesses the cSomeConstant, it is making another copy in the section

.rodata 0x40010010 (0x8)file2.o

Again the value in binary file is same as cSomeConstant

Why compiler is behaving this way? How to suppress this behavior ?

It really is confusing while debugging.

Jere
  • 3,124
  • 7
  • 15
zephyr0110
  • 223
  • 1
  • 11
  • Can you construct a [mcve] with some source that produces this result? Is it possible the value got inlined multiple places but then the compiler decided it wanted to load it from memory, and ended up with two separate copies that it failed to merge? – Peter Cordes May 18 '18 at 08:43
  • 1
    Or do SPARC PC-relative loads have limited range so it replicated the read-only constant into convenient places for two different blocks of code that use it? – Peter Cordes May 18 '18 at 08:44
  • 1
    The assembler cannot merge two constants that have the same data. Suppose you did `a = &address; b = &address2; a==b`. The behavior would be wrong, right? You have to explicitly tell the linker to merge sections. Look at the merge flag or comdat. – Ajay Brahmakshatriya May 18 '18 at 08:55
  • Right, I shall edit my answer and produce an example. – zephyr0110 May 18 '18 at 09:06
  • @ajaybrahmakshatriya no, it is consistent as these are constant and legally you should not change them. There is just one declaration but multiple copies(file wise) of the same constant with same value – zephyr0110 May 18 '18 at 09:16
  • @Prakhar I meant the compiler won't do it automatically for you because that might not be the correct behavior always. If you specifically want this behavior, you should tell the compiler. In this case we will have to see some code to tell you how to fix it. – Ajay Brahmakshatriya May 18 '18 at 09:18
  • @ajaybrahmakshatriya Edited the answer – zephyr0110 May 18 '18 at 12:57
  • 1
    @AjayBrahmakshatriya Since the constant isn't aliased, the compiler frontend at least knows that nobody can make access types point to it. – Jacob Sparre Andersen May 18 '18 at 17:46

1 Answers1

2

You should remember that typed "constants" aren't static in Ada.

If you want a static constant, use a "named number":

Some_Constant : constant := 100.0;

(I don't know which code the compiler will generate in this case.)

Jacob Sparre Andersen
  • 6,733
  • 17
  • 22