Compilers Group #4Independent studies and seminar CST 477-1 Computer Science and Technology Uva Wellassa University
IntroductionWhat 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
History1952: First compiler (linker/loader) written by Grace Hopperfor A-0 programming language1957: First complete compiler for FORTRAN by John Backusand team1960: COBOL compilers for multiple architectures1962: First self-hosting compiler for LISP
Some examples for compilersCompiler Author Windows Unix-like Other OSs IDEGNU Java GNU Project No Yes No No Sun MicrosystemJavac 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, RTEMSVisual 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 representationSuppose 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
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 compilerssource-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