Description
Examples
-
emphasis
-
bold
-
[Rascal Web site](http:///rascal-mpl.org)
-
[CallAnalysis]()
-
Table:
| Module | LOC | |--------|-----| | A | 10 | | B | 20 |
| Operator | Description |
|------------|------------|
| $A$ \| $B$ | alternative |
| \|\| | or |
Horizontal rule:
-
code -
in code: italics
<toc Test 2>
Benefits
Pitfalls
1. Synopsis
Analyzing the call structure of an application.
2. Description
Suppose a mystery box ends up on your desk. When you open it, it contains a huge software system with several questions attached to it:
-
How many procedure calls occur in this system?
-
How many procedures does it contains?
-
What are the entry points for this system, i.e., procedures that call others but are not called themselves?
-
What are the leaves of this application, i.e., procedures that are called but do not make any calls themselves?
-
Which procedures call each other indirectly?
-
Which procedures are called directly or indirectly from each entry point?
-
Which procedures are called from all entry points?
Let’s see how these questions can be answered using Rascal.
Examples: Consider the following call graph (a box represents a procedure and an arrow represents a call from one procedure to another procedure):

Rascal supports basic data types like integers and strings which are sufficient to formulate and answer the questions at hand. However, we
can gain readability by introducing separately named types for the items we are describing.
First, we introduce therefore a new type Proc (an alias for strings) to denote procedures:
rascal>alias Proc = str;
[red]##Error: Not supported construct: alias Proc = str;##
Next, we have to represent the call relation as a Rascal datatype, and the relation is the most appropriate for it. As preparation, we also import the libraries [$Rascal:Prelude/Set], [$Rascal:Prelude/Relation] and [$Rascal:Prelude/Graph] that will come in handy.
rascal>import Set;
ok
rascal>import Relation;
ok
rascal>import analysis::graphs::Graph;
ok
rascal>rel[Proc, Proc] Calls = {<"a", "b">, <"b", "c">, <"b", "d">, <"d", "c">, <"d", "e">, <"f", "e">, <"f", "g">, <"g", "e">};
ok
Now we are in a good position to start asking some questions.
How many calls occur in this system?
We use the function [Rascal:Set/size] to determine the number of elements in a set or relation.
Since each tuple in the Calls relation represents a call between procedures, the number of tuples is equal
to the number of calls.
rascal>size(Calls);
[red]##Error: Error: Name Calls is not in scope##
How many procedures occur in this system? This question is more subtle, since a procedure may call (or be called) by several others and the number of tuples is therefore not indicative. What we need are the set of procedures that occur (as first or second element) in any tuple. This is precisely what the function [$Rascal:carrier] gives us:
rascal>carrier(Calls)
[red]##Error: Error: Name Calls is not in scope##
and computing the number of procedures is now easy:
rascal>size(carrier(Calls));
[red]##Error: Error: Name Calls is not in scope##
As an aside, functions [$Rascal:Prelude/Relation/domain] and [$Rascal:Prelude/Relation/range] do the same for the first, respectively, second element of the pairs in a relation:
rascal>domain(Calls);
[red]##Error: Error: Name Calls is not in scope##
rascal>range(Calls);
[red]##Error: Error: Name Calls is not in scope##
What are the entry points for this system? The next step in the analysis is to determine which entry points this application has, i.e., procedures which call others but are not called themselves. Entry points are useful since they define the external interface of a system and may also be used as guidance to split a system in parts. The top of a relation contains those left-hand sides of tuples in a relation that do not occur in any right-hand side. When a relation is viewed as a graph, its top corresponds to the root nodes of that graph. Similarly, the bottom of a relation corresponds to the leaf nodes of the graph. See the section called ?Graph? for more details. Using this knowledge, the entry points can be computed by determining the top of the Calls relation:
rascal>top(Calls);
[red]##Error: Error: Name Calls is not in scope##
What are the leaves of this application?
In a similar spirit, we can determine the leaves of this application, i.e., procedures that are being called but do not make any calls themselves:
rascal>bottom(Calls);
[red]##Error: Error: Name Calls is not in scope##
Which procedures call each other indirectly?
We can also determine the indirect calls between procedures, by taking the transitive closure of the Calls relation, written as Calls+.
Observe that the transitive closure will contain both the direct and the indirect calls.
rascal>rel[Proc,Proc] closureCalls = Calls+;
ok
Which procedures are called directly or indirectly from each entry point?
We now know the entry points for this application ("a" and "f") and the indirect call relations. Combining this information, we can determine which procedures are called from each entry point. This is done by indexing closureCalls with appropriate procedure name. The index operator yields all right-hand sides of tuples that have a given value as left-hand side. This gives the following:
rascal>set[Proc] calledFromA = closureCalls["a"]
>>>>>>>set[Proc] calledFromF = closureCalls["f"]
[red]##Parse error##
Which procedures are called from all entry points?
Finally, we can determine which procedures are called from both entry points by taking the intersection of the two sets
calledFromA and calledFromF:
rascal>calledFromA & calledFromF
[red]##Error: Error: Name calledFromA is not in scope##
[red]##Error: Name calledFromF is not in scope##
or if your prefer to write all of the above as a one-liner using a [$Rascal:Expressions/Reducer] expression:
rascal>(carrier(Calls) | it & (Calls+)[p] | p <- top(Calls));
[red]##Error: Error: Name Calls is not in scope##
[red]##Error: Name Calls is not in scope##
The reducer is initialized with all procedures (carrier(Calls)) and iterates over all entry points (p ← top(Calls)).
At each iteration the current value of the reducer (it) is intersected (&) with the procedures called directly or indirectly
from that entry point ((Calls+)[p]).
3. Benefits
-
In small examples, the above results can be easily obtained by a visual inspection of the call graph. Such a visual inspection does not scale very well to large graphs and this makes the above form of analysis particularly suited for studying large systems.
4. Pitfalls
-
We discuss call analysis in a, intentionally, simplistic fashion that does not take into account how the call relation is extracted from actual source code. The above principles are, however, applicable to real cases as well.
# Questions
4.1. If
Conditional statement.
-
if ( Exp ) Statement; -
if ( Exp ) Statement1 else Statement2;
|
|
|
|
|
|
Statement1 |
Statement2 |
|
|
T1 |
T2 |
|
The test Exp is evaluated and its outcome determines the statement to be executed:
Statement1 if Exp yields true and Statement2 otherwise.
The value of an if-then statement is equal to Statement when its test is true. Otherwise it is void.
The value of an if-then-else statement is the value of the statement that was executed.
rascal>if( 3 > 2 ) 30; else 40;
int: 30
rascal>x = if( 3 > 2 ) 30; else 40;
int: 30
rascal>if( 3 > 2 ) 30;
int: 30
An if-then statement yields void when its test is false
(demonstrated by the ok that is printed by the Rascal system):
rascal>if( 2 > 3 ) 30;
int: 666
4.2. Synopsis
Library functions
Unresolved directive in Libraries/Libraries.adoc - include::Boolean/Boolean.adoc[Boolean]
