cs31 lecture 6&7

E where to resume when the function exits x this data

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: n internal state, which needs memory Arguments Local variables Return address (i.e., where to resume when the function exits) x This data is placed on the stack Special memory region in computers, to handle functions Stack has limited size by default (a few MB on linux) x Each time a function is called, some space on the stack is Each used used Recursive functions may go over the stack size! 19 The (memory) life of a function x A function can modify its own state (local variables / function arguments) arguments) x A function can modify global variables x A function in C++ can read/write any memory address in function the address space of the program! the x A function has side-effect when it modifies data outside function the function (global variable, random memory, …) the x Otherwise, it is side-effect free / “pure” 20 Compiler optimizations x The compiler is (very) smart! Can analyze a program across functions, and perform Can optimizations optimizations Compiler options to tune optimizations (-O2, -O3, … for GCC) x Typical optimization: inlining Takes the ‘text’ of a function, and inserts the implementation at Takes the call site the x Typical optimization: dead-code elimination Removes parts of the code that are never accessible x Typical optimization: constant propagation 21 Inlining int div2(int x) { return x / 2; return } int main() { int foo = div2(42); int } After inlining: int main() { int __x = 42; int int __tmp = __x / 2; int int foo = __tmp; int } 22 DCE int foo(int x) { if (x >= 42) return 1; return else return 2; return std::cout << “useless!” << std::endl; std::cout } After DCE: int foo(int x) { if (x >= 42) return 1; return else return 2; return } 23 Constant propagation int main() { int bar = 51; int int foo = (42 + bar) / bar; int } After constant prop: int main() { int bar = 51; int int foo = (42 + 51) / 51; int } 24 How smart? Really smart! int main() { int i; int int sum = 0; int for (i = 0; i < 1000000; ++i) for sum += i * 3 + 45; sum std::cout << sum << std::endl; std::cout } ASM code: movl $1099913696, %esi 25 Libraries x Libraries are roughly packages of functions x In a nutshell: someone else wrote functions, which are In accessible to the developer of a new application accessible Do not reprogram the wheel! x Needed: The code (source, object) implementing the functions to be used The prototype of the functions to be used Adding some options to the compiler about the location of those 2 Adding items items 26 Creating a library x In a nutshell, a library is a set of functions (no ‘main()’ In function) function) x When creating a library, a collection of .cpp and .hh files When are created by the developer are x Each file can be compiled as a stand-alone unit (-c option Each in GCC) to get object code in x The object code files are then packed into a single archive x The developer ships archive + headers, and/or source + The headers headers 27 A toy example: libdiv x Precise...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online