compiler presentaion

27

Upload: shady-a-alefrangy

Post on 16-Jul-2015

60 views

Category:

Technology


0 download

TRANSCRIPT

A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code).

Source code

OptimizingObject code

The term decompiler is most commonly appliedto a program which translates executableprograms (the output from a compiler) intosource code in a (relatively) high levellanguage which, when compiled, will producean executable whose behavior is the same asthe original executable program

1. Lexical analysis: in a compiler linearanalysis is called lexical analysis or scanning

2. Preprocessor: in addition to a compilerseveral other programs may be required tocreate and executable target program. Asource program may be divided intomodules stored in spa rate files. The task ofcollection the source program is sometimesentrusted to distinct program called apreprocessing.

3. Parsing: hierarchical analysis is called parsing orsyntax analysis.

4. Semantic analysis: is the phase in which thecompiler adds semantic information to the parsetree and builds the symbol table. This phaseperforms semantic checks such as type checking(checking for type errors), or object binding(associating variable and function references withtheir definitions), or definite assignment (requiringall local variables to be initialized before use),rejecting incorrect programs or issuing warnings.

5. Code generation: the final phase of the compiler is the generation of target code consisting normally of relocatable machine code or assembly code.

6. Code optimization: the code optimization phase attempts to improve the intermediate code, so that faster-running machine code will result.

Compilers bridge source programs in high-level languages with the underlying hardware.

A compiler requires :

1) Determining the correctness of the syntax of programs.

2) Generating correct and efficient object code.

3) Run-time organization.

4) Formatting output according to assembler and/or linker conventions.

The front end

The middle end

The back end

1. The front end:

checks whether the program is correctly written in terms of the programming language syntax and semantics. Here legal and illegal programs are recognized. Errors are reported, if any, in a useful way. Type checking is also performed by collecting type information. The frontend then generates an intermediate representation or IR of the source code for processing by the middle-end.

2. The middle end:

Is where optimization takes place. Typical transformations for optimization are removal of useless or unreachable code, discovery and propagation of constant values, relocation of computation to a less frequently executed place (e.g., out of a loop), or specialization of computation based on the context. The middle-end generates another IR for the following backend. Most optimization efforts are focused on this part.

3. The back end:

Is responsible for translating the IR from the middle-end into assembly code. The target instruction(s) are chosen for each IR instruction. Register allocation assigns processor registers for the program variables where possible. The backend utilizes the hardware by figuring out how to keep parallel execution units busy, filling delay slots, and so on.

One classification of compilers is by the platform on which their generated code executes. This is known as the target platform.

The output of a compiler that produces code for a virtual machine (VM) may or may not be executed on the same platform as the compiler that produced it. For this reason such compilers are not usually classified as native or cross compilers.

EQN, a preprocessor for typesetting mathematics

Compilers for Pascal

The C compilers

The Fortran H compilers.

The Bliss/11 compiler.

Modula – 2 optimization compiler.

Compiler Passes

Multi PassSingle Pass

A single pass compiler makes a single pass overthe source text, parsing, analyzing, andgenerating code all at once.

let var n: integer;var c: charin begin

c := ‘&’;n := n+1

end

PUSH 2LOADL 38STORE 1[SB]LOAD 0[SB]LOADL 1CALL addSTORE 0[SB]POP 2HALTIdent

Nc

Type

Intchar

Address

0[SB]1[SB]

A multi pass compiler makes several passesover the program. The output of a precedingphase is stored in a data structure and used bysubsequent phases.

Automatic parallelization:

The last one of which implies automation when used in context, refers to converting sequential code into multi-threaded or vectorized (or even both) code in order to utilize multiple processors simultaneously in a shared-memory multiprocessor (SMP) machine.

The compiler usually conducts two passes of analysis before actual parallelization in order to determine the following:

Is it safe to parallelize the loop? Answering this question needs accurate dependence analysis and alias analysis

Is it worthwhile to parallelize it? This answer requires a reliable estimation (modeling) of the program workload and the capacity of the parallel system.

The Fortran code below can be auto-parallelized by a compiler because each iteration is independent of the others, and the final result of array z will be correct regardless of the execution order of the other iterations.

do i= 1n ,

z(i) = x(i) + y(i(

enddo

On the other hand, the following code cannot be auto-parallelized, because the value of z(i) depends on the result of the previous iteration, z(i-1).

do i=2, n

z(i) = z(i-1)*2

enddo

This does not mean that the code cannot be parallelized. Indeed, it is equivalent to

do i=2, n

z(i) = z(1)*2**(i-1)

enddo

Automatic parallelization by compilers or tools is very difficult due to the following reasons:

Dependence analysis is hard for code using indirect addressing, pointers, recursion, and indirect function calls.

loops have an unknown number of iterations.

Accesses to global resources are difficult to coordinate in terms of memory allocation, I/O, and shared variables.

Due to the inherent difficulties in full automatic parallelization, several easier approaches exist to get a parallel program in higher quality. They are:

Allow programmers to add "hints" to their programs to guide compiler parallelization.

Build an interactive system between programmers and parallelizing tools/compilers.

Hardware-supported speculative multithreading.