Compilers and Language Translation Gordon College.

  • Published on

  • View

  • Download


  • Slide 1

Compilers and Language Translation Gordon College Slide 2 2 Whats a compiler? All computers only understand machine language Therefore, high-level language instructions must be translated into machine language prior to execution 10000010010110100100101 This is a program Slide 3 3 Whats a compiler? Compiler A piece of system software that translates high-level languages into machine language 10000010010110100100101 Congrats! while (c!='x') { if (c == 'a' || c == 'e' || c == 'i') printf("Congrats!"); else if (c!='x') printf("You Loser!"); } Compiler gcc -o prog program.c program.c prog Slide 4 4 Assembler (a kind of compiler) LOADXAssembly 0101 0000 0000 1001Machine Language (symbol table)(opcode table) One-to-one translation Slide 5 5 Compiler (high-level language translator) a = b + c - d; One-to-many translation 0101 00001110001LOAD B 0111 00001110010ADD C 0110 00001110011SUBTRACT D 0100 00001110100STORE A 0101 00001110001 0111 00001110010. Slide 6 6 Goals of a compiler Code produced must be correct A = (B+C)-(D+E); Possible translation: LOAD B ADD C STORE B LOAD D ADD E STORE D LOAD B SUBTRACT D STORE A No - STORE B and STORE D changes the values of variables B and D which is the high-level language does not intend Is this correct? Slide 7 7 Goals of a compiler Code produced should be reasonably efficient and concise Compute the sum - 2x 1+ 2x 2+ 2x 3+ 2x 4+. 2x 50000 sum = 0.0 for(i=0;i 37 Phase IV: Code Optimization Examples of possible local optimizations Constant evaluation x = 1 + 1 ---> x = 2 Strength reduction x = x * 2 ---> x = x + x Eliminating unnecessary operations Slide 38 38 Phase IV: Code Optimization Global optimization The compiler looks at large segments of the program to decide how to improve performance Much more difficult; usually omitted from all but the most sophisticated and expensive production-level optimizing compilers Optimization cannot make an inefficient algorithm efficient - only makes an efficient algorithm more efficient Slide 39 39 Summary A compiler is a piece of system software that translates high-level languages into machine language Goals of a compiler: Correctness and the production of efficient and concise code Source program: High-level language program Slide 40 40 Summary Object program: The machine language translation of the source program Phases of the compilation process Phase I: Lexical analysis Phase II: Parsing Phase III: Semantic analysis and code generation Phase IV: Code optimization


View more >