2009 Pearson Education, Inc. All rights reserved. 1 Compilers Most slides courtesy of Ms. Stephany Coffman-Wolph Many slides modified by Prof. L. Lilien.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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).
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.)
// 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!"); }}
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
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)
• 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