chapter 1 introduction. chapter 1 -- introduction2 def: compiler -- a program that translates a...

23
Chapter 1 Introduction Introduction

Upload: nancy-curtis

Post on 17-Jan-2016

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1

IntroductionIntroduction

Page 2: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 2

Def:Def: CompilerCompiler -- -- a program that translates a program a program that translates a program

written in a language like Pascal, C, written in a language like Pascal, C, PL/I, FORTRAN, or COBOL into PL/I, FORTRAN, or COBOL into machine language.machine language.

Page 3: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 3

1. Machine Language, Assembly Language, High-level Languages Machine LanguageMachine Language -- the native language of -- the native language of

the computer on which the program is run.the computer on which the program is run. It consists of bit strings which are interpreted It consists of bit strings which are interpreted

by the mechanism inside the computer. These by the mechanism inside the computer. These strings looked like: strings looked like:

00011000001101010001100000110101 In the early days people programmed in this, In the early days people programmed in this,

and wrote out these bit strings. and wrote out these bit strings.

Page 4: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 4

The first translators were The first translators were assemblersassemblers. . They translated from They translated from

LR 3,5 LR 3,5 to the bit string on the previous slide. to the bit string on the previous slide. It did this by looking up the mnemonic (LR in It did this by looking up the mnemonic (LR in

this case) in a table and pulled out its this case) in a table and pulled out its corresponding opcode, found the binary corresponding opcode, found the binary representations of 3 and 5 and assembled them representations of 3 and 5 and assembled them into the instructioninto the instruction

This representation is known as assembly This representation is known as assembly language language

Page 5: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 5

Languages like Pascal, C, PL/I, FORTRAN Languages like Pascal, C, PL/I, FORTRAN and COBOL are known as and COBOL are known as high-level high-level languageslanguages. .

They have the property that a single statement They have the property that a single statement such as such as

X := Y + Z;X := Y + Z;

corresponds to more than one machine corresponds to more than one machine language instruction. language instruction.

Page 6: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 6

The previous statement could be translated to: The previous statement could be translated to: L L 3,Y3,Y

AA 3,Z3,ZST ST 3,X 3,X

The main virtue of high-level languages is The main virtue of high-level languages is productivity. productivity. It has been estimated that the average It has been estimated that the average

programmer can produce 10 lines of programmer can produce 10 lines of debuggeddebugged code in a working day code in a working day

and that number is independent of the language.and that number is independent of the language.

Page 7: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 7

2. Terminology

source languagesource language -- the high level language -- the high level language that the compiler accepts as its input. that the compiler accepts as its input.

source codesource code -- the source language program -- the source language program that is fed to the compiler. that is fed to the compiler.

object languageobject language -- the particular machine -- the particular machine language that the compiler generates. language that the compiler generates.

object codeobject code -- the output of the compiler -- the output of the compiler

Page 8: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 8

object file object file -- the file to which object code is -- the file to which object code is normally written to (in external storage). normally written to (in external storage). This This is sometimes called and is sometimes called and object moduleobject module

target machinetarget machine -- the computer on which the -- the computer on which the program is to be run. program is to be run.

cross-compilercross-compiler -- a compiler that generates -- a compiler that generates code for a machine that is different from the code for a machine that is different from the machine on which the compiler runs.machine on which the compiler runs.

Page 9: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 9

3. Compilers and Interpreters

A compiler translates; an interpreter executes.A compiler translates; an interpreter executes.

the main advantage of interpreters is the the main advantage of interpreters is the immediacy of the response. immediacy of the response.

the main disadvantage is the slow speed of the main disadvantage is the slow speed of execution. execution.

Page 10: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 10

4. The Environment of the Compiler

The object file produced by the compiler is The object file produced by the compiler is normally not ready to run.normally not ready to run.

It is not practical for a compiler to have at hand It is not practical for a compiler to have at hand all the various methods for computing things all the various methods for computing things like square roots, logs, and other functionslike square roots, logs, and other functions

Page 11: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 11

def:def: run-time libraryrun-time library -- a collection of object -- a collection of object modules for computing basic functions.modules for computing basic functions. math functions.math functions. character and string i/ocharacter and string i/o

Another step is needed…the linkerAnother step is needed…the linker all the required run-time library services are all the required run-time library services are

identified and put with the user’s object with identified and put with the user’s object with the linkerthe linker

The linker normally generates an executable The linker normally generates an executable programprogram

Page 12: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 12

Page 13: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 13

Another step is a loader, Another step is a loader, Places the executable into memory and Places the executable into memory and

executes it.executes it. if the system uses shared libraries, also does if the system uses shared libraries, also does

linking on the fly.linking on the fly.

The study of linkers and loaders is beyond the The study of linkers and loaders is beyond the scope of this class.scope of this class.

Page 14: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 14

5. Phases of a Compiler

Lexical AnalysisLexical Analysis -- breaking up the source -- breaking up the source code into meaningful units (tokens) code into meaningful units (tokens) This is covered in Chapter 2This is covered in Chapter 2

Syntactic AnalysisSyntactic Analysis -- determines the structure -- determines the structure of the program and of all the individual of the program and of all the individual statements. statements. This is covered in Chapters 3 and 4 This is covered in Chapters 3 and 4

Page 15: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 15

Intermediate Code GenerationIntermediate Code Generation -- An internal -- An internal representation of the program that reflects the representation of the program that reflects the information uncovered by the parser. 3-address information uncovered by the parser. 3-address code, or 4-tuples code, or 4-tuples This is covered in Chapter 5 This is covered in Chapter 5

OptimizationOptimization -- Code Enhancement -- Code Enhancement This is covered in Chapter 6 This is covered in Chapter 6

Page 16: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 16

Object Code GenerationObject Code Generation -- translate the -- translate the optimized intermediate code into the target optimized intermediate code into the target language. language. This is covered in Chapter 7This is covered in Chapter 7

Page 17: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 17

6. Passes, Front End, Back End

PassPass -- A pass consists of reading a version of -- A pass consists of reading a version of the program from a file, and writing a new the program from a file, and writing a new version of it to an output file. version of it to an output file. A Pass normally comprises more than one A Pass normally comprises more than one

phase, but the number of passes and phases phase, but the number of passes and phases varies.varies.

Single pass compilers tend to be fastest, but Single pass compilers tend to be fastest, but there are reasons for more than one pass there are reasons for more than one pass (memory and language issues)(memory and language issues)

Page 18: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 18

Front EndFront End -- the phases of the compiler that -- the phases of the compiler that are heavily dependent upon the source are heavily dependent upon the source language and have little or no concern with the language and have little or no concern with the target machine. target machine. (Lexical Analysis, Parsing, Intermediate Code (Lexical Analysis, Parsing, Intermediate Code

Generation, and some Optimizations) Generation, and some Optimizations)

Back EndBack End -- those phases that are machine -- those phases that are machine dependent. dependent. (some Optimization, and Code Generation)(some Optimization, and Code Generation)

Page 19: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 19

7. System Support

Symbol Table Symbol Table the central repository of information about the the central repository of information about the

names (identifiers) created by the programmer.names (identifiers) created by the programmer.

Error HandlingError Handling this implements the compiler’s response to this implements the compiler’s response to

errors in the code it is compiling.errors in the code it is compiling. The error handler must tell the user as much The error handler must tell the user as much

about the error as possible.about the error as possible.

Page 20: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 20

8. Writing a Compiler

When you start with a brand new piece of When you start with a brand new piece of hardware, you write a compiler in assemblerhardware, you write a compiler in assembler At first you have no choice.At first you have no choice.

Once an adequate high level language is Once an adequate high level language is available, there are more attractive options available, there are more attractive options availableavailable like writing the compiler for the language you like writing the compiler for the language you

want in the language you have.want in the language you have.

Page 21: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 21

Boot StrappingBoot Strapping -- writing a minimal compiler, -- writing a minimal compiler, then writing the full compiler in that minimal then writing the full compiler in that minimal language. language. Write a minimal C compiler in assembler.Write a minimal C compiler in assembler. Write a C compiler in minimal C.Write a C compiler in minimal C.

Tools for helping with compiler writing Tools for helping with compiler writing LEX (flex) LEX (flex) YACC (bison)YACC (bison)

Page 22: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 22

9. Retargetable Compiler

One way is to take advantage of the break One way is to take advantage of the break between the front end and the back end between the front end and the back end Front end for the language Front end for the language Back end for the machine Back end for the machine

Examples:Examples: P-code -- UCSD P-systemP-code -- UCSD P-system GNU Compilers (gcc, g++, g77, …) GNU Compilers (gcc, g++, g77, …)

Page 23: Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,

Chapter 1 -- Introduction 23

10. Summary

We have seen a quick overall picture of what a We have seen a quick overall picture of what a compiler does, what goes into it, and how it is compiler does, what goes into it, and how it is organized. Details on all these will appear in organized. Details on all these will appear in subsequent chapters.subsequent chapters.

Probably the one sure way to learn about Probably the one sure way to learn about compilers in depth is to write one. So, … compilers in depth is to write one. So, …