Compilers
       Group #4
Independent studies and
        seminar
       CST 477-1
 Computer Science and
      Technology
 Uva Wellassa University
Introduction

What is translator
  A Translator is a computer program that translates one
  programming language instruction(s) into another
  programming language instruction(s) without the loss
  of original meaning.
What is a compiler?

If the translator translates a high level language into an
   assembly or machine language it is called a compiler.




Functionalities of a Compiler

• recognize legal (and illegal) programs
• generate correct code
• manage storage of all variables and code
• agree on format for object (or assembly) code
                                      3
History

1952: First compiler (linker/loader) written by Grace Hopper
for A-0 programming language

1957: First complete compiler for FORTRAN by John Backus
and team

1960: COBOL compilers for multiple architectures

1962: First self-hosting compiler for LISP
Some examples for compilers

Compiler       Author        Windows   Unix-like   Other OSs      IDE

GNU Java       GNU Project      No        Yes           No         No

               Sun
               Microsystem
Javac                           Yes       Yes           Yes        No
               s (Owned by
               Oracle)

C++ Builder    Embarcadero      Yes       No            OS X       Yes

                                                      Yes IBM
                                                     mainframe,
GCC C          GNU Project      Yes       Yes        AmigaOS,      No
                                                     VAX/VMS,
                                                      RTEMS
Visual Basic   Microsoft        Yes       No            DOS        Yes
Structure of a compiler




• intermediate representation (IR)
• front end maps legal code into IR
• back end maps IR onto target machine
• simplify retargeting
• allows multiple front ends
• multiple passes better code
Intermediate representation

Suppose we wish to build compilers for n source
  languages and m target machines.

  Case 1: no IR
     Need separate compiler for each source language/target
    machine combination.
     A total of n ∗ m compilers necessary.
     Front-end becomes cluttered with machine specific
    details, back-end becomes cluttered with source
    language specific details.
  Case 2: IR present
    Need just n front-ends, m back ends.
Front end




•Line reconstruction.
•Lexical analysis
•Preprocessing
•Syntax analysis
•Semantic analysis
Scanner




• Lexical analysis breaks the source code text into small pieces called
  tokens.
• Each token is a single atomic unit of the language, for instance a
  keyword, identifier or symbol name
• This phase is also called lexing or scanning
Parser




• recognize context-free syntax
• guide context-sensitive analysis
• produce meaningful error messages
• attempt error correction
Back end




 Analysis
        This is the gathering of program information from the intermediate
        representation derived from the input.
 Optimization
        the intermediate language representation is transformed into functionally
        equivalent but faster (or smaller) forms.
 Code generation
        the transformed intermediate language is translated into the output
        language, usually the native machine language of the system.
Instruction selection




• produce compact, fast code
• use available addressing modes
• pattern matching problem
    —   ad hoc techniques
    —   tree pattern matching
    —   string pattern matching
    —   dynamic programming
Register allocation




• have value in a register when used
• limited resources
• changes instruction choices
• can move loads and stores
• optimal allocation is difficult
Other types of compilers

source-to-source compiler
  a type of compiler that takes a high level language as its
  input and outputs a high level language.
Stage compiler
  compiles to assembly language of a theoretical
  machine, like some Prolog implementations
    This Prolog machine is also known as the Warren Abstract
    Machine (or WAM).
    Bytecode compilers for Java, Python, and many more are also a
    subtype of this.
Just-in-time compiler
  Applications are delivered in bytecode, which is compiled to
  native machine code just prior to execution.
Issues Driving Compiler Design

 Advantage:
   Self-Contained and Efficient
   Hardware Optimization

 Disadvantage:
   Compiled programs are targeted towards a particular
   platform and hence are platform dependent.
   Compile Times
   They are slower in execution than assembly programs.
   Compiled programs do not allow security to be implemented
   with in the code - e.g. a compiled program can access any area of the
   memory, and can do whatever it wants with your PC (most of the viruses are made
   in compiled languages).

                                      15
Reference

http://lambda.uta.edu/cse5317/notes/node5.html
http://dragonbook.stanford.edu/lecture-
notes/Stanford-CS143/16-Intermediate-Rep.pdf
http://en.wikipedia.org/wiki/Compiler
http://www.personal.kent.edu/~rmuhamma/Compiler
s/MyCompiler/phase.htm
Group members


UWU/CST/08/0019   LANKATHILAKA B.A.R.
UWU/CST/08/0020   LIYANAGE C.J.V.
UWU/CST/08/0021   MAHANAMA A.C.
UWU/CST/08/0023   MUTUNAYAKE M.N.A.
UWU/CST/08/0024   NASHAN M.H.M.
UWU/CST/08/0025   NAYANATHARA J.A.J.U.
Thank You

Compilers

  • 1.
    Compilers Group #4 Independent studies and seminar CST 477-1 Computer Science and Technology Uva Wellassa University
  • 2.
    Introduction What is translator A Translator is a computer program that translates one programming language instruction(s) into another programming language instruction(s) without the loss of original meaning.
  • 3.
    What is acompiler? If the translator translates a high level language into an assembly or machine language it is called a compiler. Functionalities of a Compiler • recognize legal (and illegal) programs • generate correct code • manage storage of all variables and code • agree on format for object (or assembly) code 3
  • 4.
    History 1952: First compiler(linker/loader) written by Grace Hopper for A-0 programming language 1957: First complete compiler for FORTRAN by John Backus and team 1960: COBOL compilers for multiple architectures 1962: First self-hosting compiler for LISP
  • 5.
    Some examples forcompilers Compiler Author Windows Unix-like Other OSs IDE GNU Java GNU Project No Yes No No Sun Microsystem Javac Yes Yes Yes No s (Owned by Oracle) C++ Builder Embarcadero Yes No OS X Yes Yes IBM mainframe, GCC C GNU Project Yes Yes AmigaOS, No VAX/VMS, RTEMS Visual Basic Microsoft Yes No DOS Yes
  • 6.
    Structure of acompiler • intermediate representation (IR) • front end maps legal code into IR • back end maps IR onto target machine • simplify retargeting • allows multiple front ends • multiple passes better code
  • 7.
    Intermediate representation Suppose wewish to build compilers for n source languages and m target machines. Case 1: no IR Need separate compiler for each source language/target machine combination. A total of n ∗ m compilers necessary. Front-end becomes cluttered with machine specific details, back-end becomes cluttered with source language specific details. Case 2: IR present Need just n front-ends, m back ends.
  • 8.
    Front end •Line reconstruction. •Lexicalanalysis •Preprocessing •Syntax analysis •Semantic analysis
  • 9.
    Scanner • Lexical analysisbreaks the source code text into small pieces called tokens. • Each token is a single atomic unit of the language, for instance a keyword, identifier or symbol name • This phase is also called lexing or scanning
  • 10.
    Parser • recognize context-freesyntax • guide context-sensitive analysis • produce meaningful error messages • attempt error correction
  • 11.
    Back end  Analysis This is the gathering of program information from the intermediate representation derived from the input.  Optimization the intermediate language representation is transformed into functionally equivalent but faster (or smaller) forms.  Code generation the transformed intermediate language is translated into the output language, usually the native machine language of the system.
  • 12.
    Instruction selection • producecompact, fast code • use available addressing modes • pattern matching problem — ad hoc techniques — tree pattern matching — string pattern matching — dynamic programming
  • 13.
    Register allocation • havevalue in a register when used • limited resources • changes instruction choices • can move loads and stores • optimal allocation is difficult
  • 14.
    Other types ofcompilers source-to-source compiler a type of compiler that takes a high level language as its input and outputs a high level language. Stage compiler compiles to assembly language of a theoretical machine, like some Prolog implementations This Prolog machine is also known as the Warren Abstract Machine (or WAM). Bytecode compilers for Java, Python, and many more are also a subtype of this. Just-in-time compiler Applications are delivered in bytecode, which is compiled to native machine code just prior to execution.
  • 15.
    Issues Driving CompilerDesign Advantage: Self-Contained and Efficient Hardware Optimization Disadvantage: Compiled programs are targeted towards a particular platform and hence are platform dependent. Compile Times They are slower in execution than assembly programs. Compiled programs do not allow security to be implemented with in the code - e.g. a compiled program can access any area of the memory, and can do whatever it wants with your PC (most of the viruses are made in compiled languages). 15
  • 16.
  • 17.
    Group members UWU/CST/08/0019 LANKATHILAKA B.A.R. UWU/CST/08/0020 LIYANAGE C.J.V. UWU/CST/08/0021 MAHANAMA A.C. UWU/CST/08/0023 MUTUNAYAKE M.N.A. UWU/CST/08/0024 NASHAN M.H.M. UWU/CST/08/0025 NAYANATHARA J.A.J.U.
  • 18.