+ All Categories
Home > Documents > 2009 Pearson Education, Inc. All rights reserved. 1 Compilers Most slides courtesy of Ms. Stephany...

2009 Pearson Education, Inc. All rights reserved. 1 Compilers Most slides courtesy of Ms. Stephany...

Date post: 03-Jan-2016
Category:
Upload: lorin-hodge
View: 214 times
Download: 1 times
Share this document with a friend
34
2009 Pearson Education, Inc. All rights rese 1 Compilers Most slides courtesy of Ms. Stephany Coffman-Wolph Many slides modified by Prof. L. Lilien (even many without explicit message). Slides added by L.Lilien are © 2006-2014 Leszek T. Lilien. Permision to use for non- commercial purposes slides added by L.Lilien’s will be gladly granted upon a written (e.g., emailed) request.
Transcript

2009 Pearson Education, Inc. All rights reserved.

1

Compilers

Most slides courtesy of Ms. Stephany Coffman-Wolph

Many slides modified by Prof. L. Lilien (even many without explicit message).

Slides added by L.Lilien are © 2006-2014 Leszek T. Lilien. Permision to use for non-commercial purposes slides added by L.Lilien’s will be gladly granted upon a written (e.g., emailed) request.

2009 Pearson Education, Inc. All rights reserved.

2

1) Machine Languages, Assembly Languagesand High-Level Languages

2) The Compiler and the Java Virtual Machine (JVM)

3) Java Software Editions (Versions)

4) Compiling a Java Program

5) The Complete Compilation Process

Outline

2009 Pearson Education, Inc. All rights reserved.

3

Sources

• From Chapter 1 of the textbook (p.12)

• Plus many slides courtesy of Ms. Stephany Coffman-Wolph

• Plus slides modified and added by Dr. Lilien

2009 Pearson Education, Inc. All rights reserved.

41. Machine Languages, Assembly Languages and High-Level Languages

• Programmers write instructions in various programming languages

• Three general types of computer languages :

– Machine languages

– Assembly languages

– High-level languages

2009 Pearson Education, Inc. All rights reserved.

5

Fig. 1.1 | Comparing machine, assembly and high-level languages.

1. Machine Languages, Assembly Languages and High-Level Languages (Cont.)

2009 Pearson Education, Inc. All rights reserved.

6++READ LATER++ 1. Machine Languages, Assembly Languages and High-Level Languages (Cont.)

• A computer can directly understand only its own machine language

– It consists of streams of numbers- Ultimately reduced to binary 1s and 0s

• Machine-language programs - nearly incomprehensible to humans

– Example machine-language program snippet:

+1300042774

+1400593419

+1200274027

2009 Pearson Education, Inc. All rights reserved.

7

• Machine-language programming is slow and error prone• English-like abbreviations form the basis of assembly languages

– Easier to program in them– Easier to understand them

• A computer does not understand assembly languages– Until they are translated into computer’s machine language– Translation done by assemblers

• Assemblers convert assembly-language programs to machine-language programs

– Example assembly-language program snippet:LOAD BASEPAY or: LD BASEPAYADD OVERPAYSTORE GROSSPAY or

++READ LATER++ 1. Machine Languages, Assembly Languages and High-Level Languages (Cont.)

2009 Pearson Education, Inc. All rights reserved.

8

• Example: Assembly code and its machine code [ http://en.wikipedia.org/wiki/Assembly_language#Assembler ]

++READ LATER++1.  Machine Languages, Assembly Languages and High-Level Languages (Cont.)

NOTE: The “object code” of the assembler is the machine code

Slide added by L.Lilien

2009 Pearson Education, Inc. All rights reserved.

9

• To facilitate programming, high-level languages were developed

• Keywords in English form the basis of high-level languages– Much easier to program in them– Much easier to understand them– Look almost like everyday English with math expressions:

grossPay = basePay + overTimePay

• A computer does not understand high-level languages– Until they are translated into computer’s machine languages– Translation done by compilers

• Compilers convert high-level-language programs to machine-language programs

++READ LATER++ 1.  Machine Languages, Assembly Languages and High-Level Languages (Cont.)

2009 Pearson Education, Inc. All rights reserved.

10

• A typical compilation process

[ Fig. from: http://en.wikipedia.org/wiki/Compiler ]

– With more detail that you need to know

++OPTIONAL++ 1.  Machine, Assembly and High-Level Languages (Cont.)

Slide added by L.Lilien

2009 Pearson Education, Inc. All rights reserved.

Program Development Process

Text editor Source code

(.java)

Saves Java statements

Java compiler

Is read by

Byte code(.class)

Produces

Java

Virtual Machine

(JVM)

Is interpreted by

Program

Execution

Results in

2. The Compiler and the Java Virtual Machine11

2009 Pearson Education, Inc. All rights reserved.

The Compiler and the Java Virtual Machine

• A programmer writes Java programming statements for a program.

• These statements are known as source code.

• A text editor is used to edit and save a Java source code file.

– Java source code files have a .java file extension.

• A compiler is a program that translates source code into an executable form.

12

2009 Pearson Education, Inc. All rights reserved.

++READ LATER++ The Compiler and the Java Virtual Machine

• A compiler is run using a source code file as input.• Syntax errors that may be in the program will be

discovered during compilation. • Syntax errors are mistakes that the programmer has

made that violate the rules of the programming language.• The compiler creates another file that holds the

translated instructions.

13

2009 Pearson Education, Inc. All rights reserved.

The Compiler and the Java Virtual Machine

• Most compilers translate source code into executable files (executables) containing machine code.

• The Java compiler translates a Java source file into a file that contains byte code instructions.

• Byte code instructions are the machine language of the Java Virtual Machine (JVM) and cannot be directly executed directly by the CPU.

14

2009 Pearson Education, Inc. All rights reserved.

++READ LATER++ The Compiler and the Java Virtual Machine

• Byte code files end with the .class file extension.

• JVM is a program– It emulates a micro-processor.

– It executes instructions as they are read.

• JVM is often called an interpreter.– And Java is often referred to as an interpreted language.

• See figure on Slide 12

• See next slide and Fig. 1-6/p.13

15

2009 Pearson Education, Inc. All rights reserved.

Portability

JVM for Windows

Byte code(.class)

JVM for Linux JVM for Mac

JVM for Unix

JVM = Java Virtual Machine

16

Text editor Source code

(.java)

Saves Java statements

Java compiler

Is read by

Byte code(.class)

Produces

Java

Virtual Machine

(JVM)

Is interpreted by

Program

Execution

Results in

2009 Pearson Education, Inc. All rights reserved.

Portability

• Program portability – a program written on one type of computer can be run on a wide variety of computers, with little or no modification.

• Java byte code runs on the JVM– Not on any particular CPU

– Therefore, compiled Java programs are highly portable.

• JVMs exist on many platforms:•Unix

•BSD

•Etc.

•Windows

•Mac

•Linux

17

2009 Pearson Education, Inc. All rights reserved.

++READ LATER++

Portability

• With most programming languages, portability is achieved by compiling a program for each CPU it will run on.

• Java provides an JVM for each platform so that programmers do not have to recompile for different platforms.

18

2009 Pearson Education, Inc. All rights reserved.

3. Java Software Editions (Versions)

• The software you use to write Java programs is called the Java Development Kit, or JDK.

• There are different editions of the JDK:– Java SE - Java2 Standard Edition

– Java EE - Java2 Enterprise Edition (for large business apps)

– Java ME - Java2 Micro Edition (for cell phones, pagers, appliances, etc.)

• Available for download athttp://java.sun.com

19

2009 Pearson Education, Inc. All rights reserved.

4. Compiling a Java Program

• We use Eclipse IDE (or NetBeans IDE) to compile and run Java programs.

• There is also a Java compiler executing as a command line utility.

– javac is this Java compiler.

• The command to compile a program is:javac filename.java

– The .java file extension must be used.

• Example: To compile a java source code file named Payroll.java you would use the command:

javac Payroll.java

20

2009 Pearson Education, Inc. All rights reserved.

Example: Using the command-line javac CompilerC:\>C:\>cd Temp2

C:\Temp2>dir Volume in drive C has no label. Volume Serial Number is 0694-AD70

Directory of C:\Temp2

01/20/2014 10:33 PM <DIR> .01/20/2014 10:33 PM <DIR> ..01/20/2014 09:07 PM 361 HelloWorld.java01/20/2014 09:59 PM 344 HelloWorld2.java01/20/2014 10:21 PM 0 PATH 3 File(s) 705 bytes 2 Dir(s) 56,425,979,904 bytes free

C:\Temp2>type H*d2.java

HelloWorld2.java

// PROJECT: HelloWorld2// AUTHOR: Donna Kaminski// DESCRIPTION: Program displays Hello World to IDE output window.// ****************************************************************************/

public class HelloWorld2 {

public static void main(String[] args) { System.out.println("Hello World!"); }}

C:\Temp2>type H*d.java

HelloWorld.java

package helloworld; <------[This line is not in HelloWorld2.java above.]

// PROJECT: HelloWorld// AUTHOR: Donna Kaminski// DESCRIPTION: Program displays Hello World to IDE output window.// ****************************************************************************/

public class HelloWorld {

public static void main(String[] args) { System.out.println("Hello World!"); }}

21

© 2014 L.T. Lilien

2009 Pearson Education, Inc. All rights reserved.

Cont.1 - Example: Using the command-line javac Compiler

C:\Temp2>pathPATH=C:\Program Files\Caminova\Document Express DjVu Plug-in\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\ZipGenius 6\;c:\Program Files\Microsoft SQL Server\90\Tools\binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Users\Adm\AppData\Local\Smartbar\Application\

C:\Temp2>PATH=C:\Program Files\Caminova\Document Express DjVu Plug-in\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\ZipGenius 6\;c:\Program Files\Microsoft SQL Server\90\Tools\binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Users\Adm\AppData\Local\Smartbar\Application\;C:\Program Files\Java\jdk1.7.0_45\bin\ <----[Note last PATH component (folder with javac) added.]

C:\Temp2>pathPATH=C:\Program Files\Caminova\Document Express DjVu Plug-in\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\ZipGenius 6\;c:\Program Files\Microsoft SQL Server\90\Tools\binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Users\Adm\AppData\Local\Smartbar\Application\;C:\Program Files\Java\jdk1.7.0_45\bin\

C:\Temp2>dir Volume in drive C has no label. Volume Serial Number is 0694-AD70

Directory of C:\Temp2

01/20/2014 10:33 PM <DIR> .01/20/2014 10:33 PM <DIR> ..01/20/2014 09:07 PM 361 HelloWorld.java01/20/2014 09:59 PM 344 HelloWorld2.java01/20/2014 10:21 PM 0 PATH 3 File(s) 705 bytes 2 Dir(s) 56,425,979,904 bytes free

C:\Temp2>javac H*d2.java --[Compilation completed, generated HelloWorld2.class file (see below).]

C:\Temp2>dir Volume in drive C has no label. Volume Serial Number is 0694-AD70

Directory of C:\Temp2

01/20/2014 10:35 PM <DIR> .01/20/2014 10:35 PM <DIR> ..01/20/2014 09:07 PM 361 HelloWorld.java01/20/2014 10:35 PM 428 HelloWorld2.class01/20/2014 09:59 PM 344 HelloWorld2.java01/20/2014 10:21 PM 0 PATH 4 File(s) 1,133 bytes 2 Dir(s) 56,425,979,904 bytes free

22

© 2014 L.T. Lilien

2009 Pearson Education, Inc. All rights reserved.

Cont.2 - Example: Using the command-line javac CompilerC:\Temp2>java -cp . H*d2Error: Could not find or load main class H*d2 <---[Windows doesn't like '*']

C:\Temp2>java -cp . HelloWorld2Hello World! <---[correct program execution now]

C:\Temp2>dir Volume in drive C has no label. Volume Serial Number is 0694-AD70

Directory of C:\Temp2

01/20/2014 10:35 PM <DIR> .01/20/2014 10:35 PM <DIR> ..01/20/2014 09:07 PM 361 HelloWorld.java01/20/2014 10:35 PM 428 HelloWorld2.class01/20/2014 09:59 PM 344 HelloWorld2.java01/20/2014 10:21 PM 0 PATH 4 File(s) 1,133 bytes 2 Dir(s) 56,425,979,904 bytes free

C:\Temp2>javac HelloWorld.java --[Compilation seems OK, generated HelloWorld.class file (see

below)]

C:\Temp2>dir Volume in drive C has no label. Volume Serial Number is 0694-AD70

Directory of C:\Temp2

01/20/2014 10:38 PM <DIR> .01/20/2014 10:38 PM <DIR> ..01/20/2014 10:38 PM 437 HelloWorld.class 01/20/2014 09:07 PM 361 HelloWorld.java01/20/2014 10:35 PM 428 HelloWorld2.class01/20/2014 09:59 PM 344 HelloWorld2.java01/20/2014 10:21 PM 0 PATH 5 File(s) 1,570 bytes 2 Dir(s) 56,425,979,904 bytes free

C:\Temp2>java -cp . HelloWorld <-----[Problem with execution of the HellowWorld.class]Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: helloworld/HelloWorld) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

C:\Temp2>

23

© 2014 L.T. Lilien

2009 Pearson Education, Inc. All rights reserved.

5. The Complete Compilation Process

• Complete compilation process I/O: – Input: high-level language (HLL) code – source code– Output: machine-code – object code

24

Slide added by L.Lilien

2009 Pearson Education, Inc. All rights reserved.

5. The Complete Compilation Process (Cont.)

• Four basic steps performed by a typical compiler:

1) Preprocessing (a part of the compiler!)

- To expand macros (details later)

2) Compilation (in the narrow sense)- Translates from high-level source code to assembly code

-Not to byte code

3) Assembly (a part of the compiler!)

- Translates from assembly code to machine code- When there are calls to external functions in the assembly source file, the assembler leaves the addresses of the external functions undefined, to be filled in later by the linker

4) Linking (a part of the compiler!)

- Takes one or more objects and combines them into a single executable- An executable requires many external functions from system and run-time libraries

- Creates the final executable (<name>.exe in Windows)

25

Slide modifieds by L.LilienThis and next 9 slides courtesy of S. Coffman-Wolph, a few modified (slightly) by L. Lilien

2009 Pearson Education, Inc. All rights reserved.

1) Preprocessing• First stage of the overall compilation process • Preprocessor processes compiler directives• Directives

– Allow for conditional compilations- To conditionally skip sections of source files, - To report errors and warning conditions- To have distinct regions of source code

– E.g., in C#, always placed on their own line and begin with #

• NOTE: – There is no “Java preprocessor” on Oracle’s Java pages

– See:– No “preprocessor “ found if searched for it on the Oracle page:

http://www.oracle.com/technetwork/java– If interested, see also:

– http://www.javalobby.org/java/forums/t18116.html– Hence, Java does not provide the capability to communicate with

the Java compiler using compiler directives

26

2009 Pearson Education, Inc. All rights reserved.

1) Preprocessing (Cont.)Example of C# Code w/Preprocessor Directives (lines starting with #) (Note: C# preprocessor used as an example, since

we do not use preprocessors for Java)

#define Debug // Debugging on#undef Trace // Tracing offclass PurchaseTransaction{ void Commit(){

#if DebugCheckConsistency();#if Trace

WriteToLog(this.ToString());#endif

#endifCommitHelper();

}}

27

2009 Pearson Education, Inc. All rights reserved.

2) Compilation

• The actual compilation — in the narrow sense

• Input: preprocessed source code

• Output: assembly language (for a specific processor)

– ++OPTIONAL++ Major phases:- Scanner

“Break up” the text/code into tokens

Done via Regular Expressions

- Parser

Insures that the source code follows all the rules

- Code generator

Creates assembly language code

28

2009 Pearson Education, Inc. All rights reserved.

++READ LATER++ 2) Compilation (Cont.) Regular Expressions

• Regular expressions are from the theoretical side of computer science

– Automata theory

– Formal language theory

– These fields study models of computation (automata) and ways to describe and classify formal languages

• Regular expressions are used mainly for pattern matching

29

2009 Pearson Education, Inc. All rights reserved.

++READ LATER++ 2) Compilation (Cont.)Regular Expression Basic Examples

• Match any digit

– \d or [0-9]

• Match any non-digit

– \D

• Match exactly three digits

– \d{3} or \d\d\d or [0-9][0-9][0-9] or [0-9]{3}

• Match any sequence of letters in upper or lower case, but there must be at least 1 letter

– [a-zA-Z]+

• Match any sequence of at least one word character

– \w+

30

2009 Pearson Education, Inc. All rights reserved.

3) Assembly

• Converts assembly language code into machine code – Input (source file): assembly language code

– Output (object file): machine code

• Processes calls to external functions – They can be present in the source file (assembly language file)

– Assembler leaves addresses of external functions undefined- To be filled in later by the linker (next overall compilation phase)

31

2009 Pearson Education, Inc. All rights reserved.

4) Linking

• The final stage of compilation is the linking of object files to create an executable

– Takes multiple object files and creates one executable file.

– Among these object files are many external functions taken from system and run-time libraries

• Linker complains if it can not link proper methods/functions– Earlier in compilation , if a method/function did not exist, it was

assumed that it was in another file (to be linked later)

– You can think of this phase as the one trying to fill-in any remaining “blanks” and “ties” it all together

32

2009 Pearson Education, Inc. All rights reserved.

The Complete Compilation Process (Cont.)

• Summary: Four basic compiler steps:

1) Preprocessing (a part of the compiler!)

2) Compilation (in the narrow sense)

3) Assembly (a part of the compiler!)

4) Linking (a part of the compiler!)

33

Slide modifieds by L.Lilien

2009 Pearson Education, Inc. All rights reserved.

34

The End


Recommended