Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a normal Integer and an aliased Integer in Ada?

Tags:

types

ada

I am still confused on what's the difference between a var : Integer; and var : aliased Integer;. According to wikibooks "if you want to take access from any variable you need to tell the compiler that the variable needs to be in memory and may not reside inside a register". Isn't registers memory also? So the real question that's been bothering me is where var : Integer; and var : aliased Integer; is being stored.

like image 319
Amben Uchiha Avatar asked Sep 06 '25 03:09

Amben Uchiha


2 Answers

The Wikibooks answer is valid for most current general-purpose computers, where registers are part of the processor and not of memory (RAM), and registers do not have memory addresses and so cannot be "pointed to" by pointers, or "access values" as they are called in Ada.

Some microcontrollers, such as the Intel-8051 architecture, do map registers to memory locations and in such machines an aliased variable could be stored in a register. However, it would then have to be stored always in that register, so that it always has the same address and all pointers to it remain valid for as long as the variable exists. Compilers usually do not want to dedicate registers to variables, because registers are more useful as working storage for temporary values.

While the C language has a "register" keyword that can be used to hint to the compiler that the programmer thinks it would be beneficial (usually for speed) to store a certain variable in some register, many compilers today ignore that keyword, and use their own analysis to decide which variables should be held in registers and when. C compilers usually detect if the code ever takes the address of a variable (the "&" operator) and use that information to avoid storing that variable in a register (except very temporarily).

In Ada the more important reason for requiring the "aliased" keyword whenever there could be an access value pointing to the variable is to make it clear to the compiler, the human reader, and static analysis tools which variables can be accessed via access values. So the only difference to you, the programmer, is that you cannot use var'Access unless the "var" is marked "aliased". Where the "var" is stored should be the compiler's business.

One reason why Ada compilers need that information is that Ada subprograms can have nested subprograms within them. These nested subprograms can access the variables of the containing subprogram (if the variables are declared before the nested subprogram), and if the variable is not marked aliased the compiler can better optimize the code of the nested subprogram by, indeed, keeping the variable's value in some register for a while, without fear that some other part of the program might try to access the variable "behind the scenes" by an access value.

like image 52
Niklas Holsti Avatar answered Sep 07 '25 22:09

Niklas Holsti


The difference is that you can take 'Access of an aliased variable and not of a normal variable. As to where they're stored, a normal variable may be stored anywhere the compiler likes; theoretically, the location could even change during execution. An aliased variable must be stored somewhere that 'Access makes sense, and must stay there for the life of the variable.

However, there's more to this than just whether you can take 'Access. This kind of information can be used by the compiler to make optimizations that are impossible without it. In C, all variables are aliased and you can take pointers to them with the unary "&" operator; in Ada, only variables marked aliased are aliased. "C vs Ada: Arguing Performance Religion discusses why the Tartan Ada (83) compiler produced faster code than the Tartan C compiler, and negative effects of C's pointers-to-everything approach is part of the reason.

like image 41
Jeffrey R. Carter Avatar answered Sep 07 '25 21:09

Jeffrey R. Carter