Compiler optimization

  • Inline code
    • macro: replace function calls with actual codes
  • move code out of loops
    • loop invariant code notion
      i.e.: for( int i=0; i< 1000000; i++){
      arr[i] = i+x*y
  • put some variables into registers
  • strictly evaluate some ports
  • reorder instruction for process pipeline
  • Usually have to use an option for compiler
    • Haskell : -0 = -01 fast
      -02 really fast

Just-in-Two Compilation (JIT)

  • e.g. Java
  • interpreter does some compilation before execution
  • Java virtual Machine
  • Identify parts of the code that benefit for compilation
    • ask a lot
  • compiles it to machine codes and store it in memory
  • faster => cost is memory
  • active research area

Extra Topic

  • A compiler or an interpreter is a tool
  • We tend to associate certain languages (like C) with compilation and others (like Java) with interpretation/JIT
  • With many languages, we can do both
    • Example: Low level Virtual Machine:
      • C, Python, Ruby, Haskell, etc
      • Interpret C with PicoC
      • Java can be compiled by GCJ -> only faster at startup
      • Python interpreted for Python UM
        • PyPy (JIT)
        • compile to .NET CLR (Iron Python) or JVM (Jython)
      • Haskell can be compiled by GHC (machine) or Huge (byte code)


  • primitive -> e.g. Int, float, char
  • more complicated -> e.g. Object, user-defined types
  • Type Safety:Legal operations are being applied to a certain data object
    • e.g: sqrt “Hello World”
  • Memory Safety: pointers
    • wild pointers -> reassign a pointer to an object with a different size
  • Static Typing: we know type at compilation (e.g. C, Java, Haskell)
    • Advantage:
      • optimization
      • prevent errors
  • Dynamic Typing;we know types at run time (e.g. Python, Ruby, Lisp, Javascript)
    • Advantage:
      • easier to code for programmers
      • flexible
      • polymorphism is easy (polymorphism: treating different object in the same manner
        • Example:
          Class Vehicle
          Class Car extends Vehicle
          Class Boat extends Vehicle

Some things are not solved by static typing:

  • have to be checked at runtime
    • e.g. Divide by 2050, array bounds

in between features (exceptions)

  • Ruby, Python: everything has a type
    • Object if not otherwise defined (not very useful)
  • Haskell: Type Classes
    • Function is defined only generally
      • figure it out at runtime
  • C/C++: void pointer
    • can point to anything

Type Interface

  • implicit typing
  • compiler figures it out
  • has to be at least some static typing in language
    • otherwise it would never check type at all
    • Example: C# var
      var x = new Whatever();
  • PyPy

Static/Dynamic Binding

  • How do we know which function/definition/variable to use?
  • static binding:the compiler can figure it out
    • String s;
      Integer i;

      System.out.print(s); <- look the same, but they are different
    • specify function is decided on approximately at compile time
  • dynamic binding:correction function is decided upon at execution
    • x = 1
      y = 2
      z = x + y [*the (+) sign]
    • more flexible
    • x and y could change before being added
    • slow <- overhead for type check
      • checked every time for each line of code like this
      • can be usually significantly faster than static binding