Compiler Construction Naveed Ejaz Lecture 1 General Information

  • Slides: 17
Download presentation
Compiler Construction Naveed Ejaz Lecture 1

Compiler Construction Naveed Ejaz Lecture 1

General Information Instructor Dr. Naveed Ejaz Text Compilers – Principles, Techniques and Tools by

General Information Instructor Dr. Naveed Ejaz Text Compilers – Principles, Techniques and Tools by Aho, Sethi and Ullman 2

Why Take this Course Reason #1: understand compilers and languages § understand the code

Why Take this Course Reason #1: understand compilers and languages § understand the code structure § understand language semantics § understand relation between source code and generated machine code § become a better programmer 3

Why Take this Course Reason #2: nice balance of theory and practice § Theory

Why Take this Course Reason #2: nice balance of theory and practice § Theory • mathematical models: regular expressions, automata, grammars, graphs • algorithms that use these models 4

Why Take this Course Reason #2: nice balance of theory and practice § Practice

Why Take this Course Reason #2: nice balance of theory and practice § Practice • Apply theoretical notions to build a real compiler 5

Why Take this Course Reason #3: programming experience § write a large program which

Why Take this Course Reason #3: programming experience § write a large program which manipulates complex data structures § learn more about C++ and Intel x 86 6

What are Compilers § Translate information from one representation to another § Usually information

What are Compilers § Translate information from one representation to another § Usually information = program 7

Examples § Typical Compilers: • VC, VC++, GCC, Java. C • FORTRAN, Pascal, VB(?

Examples § Typical Compilers: • VC, VC++, GCC, Java. C • FORTRAN, Pascal, VB(? ) § Translators • Word to PDF • PDF to Postscript 8

In This Course We will study typical compilation: § from programs written in highlevel

In This Course We will study typical compilation: § from programs written in highlevel languages to low-level object code and machine code 9

Typical Compilation High-level source code Compiler Low-level machine code 10

Typical Compilation High-level source code Compiler Low-level machine code 10

Source Code int expr( int n ) { int d; d = 4*n*n*(n+1); return

Source Code int expr( int n ) { int d; d = 4*n*n*(n+1); return d; } 11

Source Code § Optimized for human readability § Matches human notions of grammar §

Source Code § Optimized for human readability § Matches human notions of grammar § Uses named constructs such as variables and procedures 12

Assembly Code. globl _expr: pushl %ebp movl %esp, %ebp subl $24, %esp movl 8(%ebp),

Assembly Code. globl _expr: pushl %ebp movl %esp, %ebp subl $24, %esp movl 8(%ebp), %eax movl %eax, %edx leal 0(, %edx, 4), %eax movl %eax, %edx imull 8(%ebp), %edx movl 8(%ebp), %eax incl %eax imull %eax, %edx movl %edx, -4(%ebp) movl -4(%ebp), %edx movl %edx, %eax jmp L 2. align 4 L 2: leave ret 13

Assembly Code Optimized for hardware § Consists of machine instructions § Uses registers and

Assembly Code Optimized for hardware § Consists of machine instructions § Uses registers and unnamed memory locations § Much harder to understand by humans 14

How to Translate Correctness: the generated machine code must execute precisely the same computation

How to Translate Correctness: the generated machine code must execute precisely the same computation as the source code 15

How to Translate § Is there a unique translation? No! § Is there an

How to Translate § Is there a unique translation? No! § Is there an algorithm for an “ideal translation”? No! 16

How to Translate § Translation is a complex process § source language and generated

How to Translate § Translation is a complex process § source language and generated code are very different § Need to structure the translation 17