XCSG:StackVariable

From AtlasWiki
Jump to: navigation, search

A StackVariable variable is a local variable declared inside a Function, and allocated on the stack or in a processor register, at the compiler's or interpreter's discretion.

Note that, unlike other variables, many local variables do not appear in XCSG graphs. Since local variables do not cause interprocedural flows unless their address is taken and passed to another method, they are typically represented in data flow only by Initialization and Assignment nodes. C/C++ static local variables are also an exception, but they are ProgramVariables, not StackVariables.

Consider the following example:

function foo(){
     int x = 5;
     x = x + 1;
     return x;
}

This function always returns 6, never 5, or any other value. However, if we were to represent DataFlow using a single node for the LocalVariable x, we would see incoming data flow from the Literal 5 through an Initialization node to the LocalVariable x, and another incoming data flow from the Operator +, which would read x and the literal 1.

Trying to make sense of this data flow graph, we would see a self-referential loop incrementing the variable, and would (at best) be able to conclude that the value returned was 5 or greater.

For this reason, it is preferable to leave out an actual LocalVariable node, and instead connect the Initialization node x= directly to the + Operator, and only the final Assignment node to the Return, giving a more realistic, tree-like structure, leaving it clear that the returned value comes from executing 5 + 1.

Alternatively, it would be possible to use multiple LocalVariable nodes to represent each LocalVariable, with each receiving its value from a single Assignment, and passing their value forward in DataFlow to reads which may see the result of that Assignment. This would become necessary if a language were analyzed for which the result of an Assignment is the assigned value (before type conversion), rather than the value assigned, after type conversion, as is the case in every language EnSoft has indexed to date, including Java, C, and C++.

If the address of a local variable is taken, the picture is complicated significantly, as it may receive a new value from any function to which that address is passed. Techniques for dealing with this are still being pursued.

Specification

Metaclass StackVariable
Extends Variable
Description A Variable declared inside a Function.
All Superkinds Variable, Node, ModelElement
Specified In Edges

Inherited From In Edge Predecessor Multiplicity Description
Variable DataFlow (Edge) DataFlow (Node) Connects DataFlow nodes to one another and to Variables, linking the origin of data to locations that it is modified or consumed.
Specified Out Edges

Inherited From Out Edge Successor Multiplicity Description
Variable DataFlow (Edge) DataFlow (Node) Connects DataFlow nodes to one another and to Variables, linking the origin of data to locations that it is modified or consumed.

DefinedAs TypeAlias 0..1 Connects a Variable or DataFlow node which received its Type via a TypeAlias (such as a C/C++ typedef) to that TypeAlias.
InstanceVariableWritten InstanceVariableAssignment Connects a value (DataFlow (Node) or Variable) representing an object instance to an InstanceVariableAssignment representing a write to a field of that object.
TypeOf Type 1 Links a Variable or DataFlow node to its Type
Known Possible In Edges

Inherited From In Edge Predecessor Multiplicity Description
Variable HasVariable Namespace 1 Links a Classifier or Package to one of its Variables.

InterproceduralDataFlow Assignment Represents data flows between Functions, or involving Variables visible to multiple Functions.
Node Contains Node 1 Indicates that the predecessor contains the successor in a sense specified by the specific type of Contains edge.
Known Possible Out Edges

Inherited From Out Edge Successor Multiplicity Description
Variable InterproceduralDataFlow DataFlow (Node) Represents data flows between Functions, or involving Variables visible to multiple Functions.
Node Contains Node Indicates that the predecessor contains the successor in a sense specified by the specific type of Contains edge.