CD UNIT 4
CD UNIT 4
Type Conversions
• Type conversion rules vary from language to
language.
• widening conversions, which are intended to
preserve information, and narrowing conversions,
which can lose information.
• Conversion from one type to another is said to be
implicit if it is done automatically by the compiler.
• Implicit type conversions are also called coercions.
• Conversion is said to be explicit if the programmer
must write something to cause the conversion.
• Explicit conversions are also called casts.
Overloading of Functions and
Operators
• An overloaded symbol has different meanings depending on
its context.
• Overloading is resolved when a unique meaning is
determined for each occurrence of a name.
• The + operator in Java denotes either string concatenation
or addition, depending on the types of its operands.
• The signature for a function consists of the function name
and the types of its arguments. Overloading of functions
can be resolved based on signatures.
• void x() { }
• void x(String s) { }
Control Flow
• Boolean expressions are composed of the
boolean operators (which we denote &&, ||,
and !, using the C convention for the operators
AND, OR, and NOT, respectively) applied to
elements that are boolean variables or
relational expressions.
• Short-Circuit Code:
In short-circuit (or jumping) code, the boolean
operators &&, ||, and ! translate into jumps. The
operators themselves do not appear in the code;
instead, the value of a boolean expression is
represented by a position in the code sequence.
Backpatching
• Boolean expressions are usually translated using the jump method
since this is convenient for optimization.
• However, more than a single pass may be needed in order to
generate code for boolean expressions and flow of control during
bottom-up parsing.
• Indeed, when translating forward jumps, at the time we generate
the code we do not know the (numerical) address of the label we
want to branch to.
• The solution is to generate a sequence of branching statements
where the addresses of the jumps are temporarily left unspecified.
• For each boolean expression E we maintain two lists
• E.truelist which is the list of the (addresses of the) jump
statements appearing in the translation of E and forwarding to
E.true.
• E.falselist which is the list of the (addresses of the) jump
statements appearing in the translation of E and forwarding to
E.false.
• When the label E.true (resp. E.false) is eventually defined we can
walk down the list, patching in the value of its address.
Switch-Statements
Intermediate Code for Procedures
Unit 4 Part-2
• Run-Time Environments: Storage organization, Stack
Allocation of Space, Access to Nonlocal Data on the
Stack, Heap Management, Introduction to Garbage
Collection, Introduction to Trace – Based Collection.
• Compiler must cooperate with OS and other system software
to support implementation of different abstractions (names,
scopes, bindings, data types, operators, procedures,
parameters, flow-of-control) on the target machine
•Compiler does this by Run-Time Environment in which it
assumes its target programs are being executed
• Run-Time Environment deals with
–Layout and allocation of storage for the objects named in the
source program
–the mechanisms used by the target program to access
variables
– Linkage between procedures
– the mechanisms for Parameter passing
– Interface to OS, I/O devices and other programs etc
Storage Organization
• the executing target program runs in its own logical
address space in which each program value has a
location.
• The management and organization of this logical
address space is shared between the compiler,
operating system, and target machine.
• The operating system maps the logical addresses into
physical addresses, which are usually spread
throughout memory.
• The run-time representation of an object program in
the logical address space consists of data and program
areas as shown in Fig.
Int a;
Goto L1;
• The storage layout for data objects is strongly
influenced by the addressing constraints of the
target machine.
• The size of the generated target code is fixed at
compile time, so the compiler can place the
executable target code in a statically determined
area Code, usually in the low end of memory.
• Similarly, the size of some program data objects,
such as global constants, and data generated by
the compiler, such as information to support
garbage collection, may be known at compile
time, and these data objects can be placed in
another statically determined area called Static.
• Dynamic space areas – size changes during
program execution.
• • Heap Grows towards higher address
s
Reference Counting Garbage Collectors
With a reference-counting garbage collector, every object must have a held for the
reference count. Reference counts can be maintained as follows:
1. Object Allocation. The reference count of the new object is set to 1.
2. Parameter Passing. The reference count of each object passed into a procedure is
incremented
3. Reference Assignments. For statement u = v, where u and v are refer- ences, the
reference count of the object referred to by v goes up by one, and the count for the old
object referred to by u goes down by one.
4. Procedure Returns. As a procedure exits, objects referred to by the localvariables in its
activation record have their counts decremented. If several local variables hold references
to the same object, that object's count must be decremented once for each such reference.
5. Transitive Loss of Reachability. Whenever the reference count of an object becomes zero,
we must also decrement the count of each object pointed to by a reference within the
object.
Reference counting has two main disadvantages: it cannot collect unreach- able, cyclic data
structures, and it is expensive. Cyclic data structures are quite plausible; data structures
often point back to their parent nodes, or point toeach other as cross references.