2.1. Compilers and Interpreters
Compile-Link Process Next topic
Compilers and Interpreters
Compilers vs Interpreters
High-level View of a Compiler
Compilation
Interpretation
Language Implementation
Cross Compilation
Calling conventions
Static & Dynamic Allocation
Parameter Passing Mechanism
Call by Value
Call by Value Result
Call by Reference
Java Programming Environment Java compiler produces an intermediate code known as byte code for a machine, known as JVM. It exists only inside the computer memory. Machine code is generated by the java interpreter by acting as an intermediary between the virtual machine and real machine. Java Program Java Compiler Virtual Machine Bytecode Java Interpreter Machine Code 16
Java Programming Environment Compile-time environment Run-time environment A.java B.java C.java Java compiler A.class B.class C.class Your program’s source files Your program’s class files Your program’s class files A.class B.class C.class Your class files move locally or through a network Java Virtual Machine Java Interpreter Object.class String.class Java API’s class files
What is ‘Java Virtual Machine’ …? The abstract specification A concrete implementation A run-time instance ByteCode (class files) JVM Linux Win Mac Compiled Java Program Java Virtual Machine H/W Platform & OS
Java Virtual Machine Architecture A.java A.class Java Compiler Compile source code Java Virtual Machine Loader Network B.class Verifier Linker Bytecode Interpreter
Basic Block Diagram of JVM Java Virtual Machine Your program’s class files Java API’s class files Class Loader ByteCodes Execution Engine Native method invocation Host Operating System RealTimeSystems Lab
Md. Samsuzzaman ,Lecturer Faculty of CSE, PSTU Primary Memory . Disk Editor Compiler Class Loader Program is created in an editor and stored on disk in a file ending with .java. Compiler creates bytecodes and stores them on disk in a file ending with .class. Class loader reads .class files containing bytecodes from disk and puts those bytecodes in memory. Phase 1 Phase 2 Phase 3 Bytecode Verifier Bytecode verifier confirms that all bytecodes are valid and do not violate Java’s security restrictions. Phase 4 Interpreter Interpreter reads bytecodes and translates them into a language that the computer can understand, possibly storing data values as the program executes. Phase 5 JVM Md. Samsuzzaman ,Lecturer Faculty of CSE, PSTU 05/07/2008 Mosarratj Jahan, Dept. of CSE, DU 21
.NET Compilation and Execution Also called Assembly (.EXE or .DLL file) Source Code Language Compiler Code MSIL Metadata Before installation or the first time each method is called Execution Native Code JIT Compiler
The Common Language Runtime Compilation and Execution Source code written in Visual C#, Visual Basic .NET or another language that targets the CLR is first transformed into MSIL by the appropriate language compiler. Before execution, this MSIL is compiled by the Just-in-Time (JIT) complier into native code for the processor on which the code will operate.
The Common Language Runtime Compilation and Execution The default is to JIT compile each method when it is first called, but it is also possible to “preJIT” MSIL code to native code at assembly install-time. With this option, all methods are compiled before the application is loaded so as to avoid the overhead of JIT compilation on each initial method call.
The Common Language Runtime Compilation and Execution You use the Native Image Generator (Ngen.exe) to create a native image from a managed assembly and install it into the native image cache. Once the code is compiled and cached, the CLR does not need to re-compile the MSIL code until the assembly is updated, the Just-In-Time compiled code is removed from the cache, or the machine is restarted.
summary
Interpreters
Compiler vs. Interpreter
Compiler vs. Interpreter