REVIEW OF STACK LAYOUT GENERIC SUBROUTINES AND MODULES

REVIEW OF STACK LAYOUT, GENERIC SUBROUTINES AND MODULES COROUTINES Muhammad Touseef Muhammad Nadeem Mohammad Aqeel Noman Rasool

Review Of Stack Layout ■ Stack recap: Each routine, as called, gets a new frame put on the top of the stack – Contains arguments, return values, book-keeping info, local variables, and temporaries ■ Stack pointer: the address of either: – last used location at the top of the stack – First unused location ■ Frame pointer: address within the frame – Objects in the frame are accessed via offset from the frame pointer – Variable size things are towards top of the frame, since can’t be preset, with dope in fixed size part

Review Of Stack Layout ■ If no variable-size objects, then everything has fixed offset from stack pointer – In this case, frame pointer isn’t needed at all – Frees up a register for use ■ Static and dynamic links maintained on stack: – Static store context for scope purposes – Dynamic store saved value of the frame pointer, so that it returns to correct calling method

Review Of Stack Layout

Motivations ■ Subroutines provide a natural way of abstracting data operations. ■ With large programs containing many methods it is possible that subroutines will be needed to perform similar operations on many different types. ■ Consider some code from any language: String add (String a, String b) return a + b; int add ( int a, int b ) return a + b; double add ( double a, double b) return a + b;

Motivations ■ The code is correct but the repetitious operations must be defined statically many times over. ■ We need a form of parametric parameterisation i. e the subroutine should only be written once and be capable of accepting any arguments. ■ This idea is known as generic programming. ■ In Java version 5, generic programming has been added. ■ In C++, generics are known as templates. ■ Other languages that feature generics are Ada, Modula-3 and C#.

Generic programming ■ is a style of computer programming in which algorithms are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters. ■ This approach, developed by ML in 1973. ■ permits writing common functions or types that differ only in the set of types on which they operate when used, thus reducing duplication.

Generics ■ Generics are especially helpful for creating container classes. ■ Container classes are data abstractions that hold a collection of objects. ■ The operation of the container class is independent of the type of object stored. ■ Some examples of container classes include…. – – Stack Queue Set Heap…

Generic Subroutines ■ Generic subroutines are needed in generic classes. ■ They allow a method to be parameterised by a single type. ■ Hence our earlier example becomes vastly simplified: public T <T> add ( T a, T b) return a + b; ■ This code is now identical to the first slide except we now have a generic type T passed as a parameter to the add method. ■ Hence we can now call the code: int c = add( 5 , 7 ); double d = add ( 4. 5, 6. 9 ); String concat = add( “gen” , “erics” );

Implementing Generics ■ In C++, they are a static mechanism. ■ All of the work required to use multiple instances of the generic code is handled by the compiler. ■ The compiler creates a separate copy of the code for every instance. ■ In Java, all instances of the generic code will shae that same code at run-time. ■ If we call some generic parameter, T in Java, this behaves identically to java. lang. Object except that we do not have to use casts.

Generics in C++ ■ The syntax is similar but the behaviour is more flexible than Java. ■ A generic add method in C++: template<typename T> T add (T a, T b) { return a+b; } ■ To call the method add(5, 6); add(5. 6 , 7. 8); ■ The compiler figures out all of the hard work.

Java Generics ■ Generics in Java are defined in terms of type erasure. ■ This means that every generic type parameter is replaced by java. lang. Object. ■ E. g. class choice <T> { public boolean best ( T a , T b ) {} } Is replaced by: class choice { public boolean best ( Object a, Object b ) {} }

Java Generics VS C++ Generics ■ Recall the C++ example: template<typename T> T add (T a, T b) { return a+b; } ■ This allows any number ( int, float, short, double etc ) to be added together. ■ How can we achieve this is Java? public <T> T add ( T a, T b ) { return a+b; }

Co-routines ■ A coroutine is similar to a thread (in the sense of multithreading). ■ It is a line of execution, with its own stack, its own local variables, and its own instruction pointer. ■ It shares global variables and mostly anything else with other coroutines.

--THANKS--
- Slides: 15