15-01-2013, 09:48 AM
Compilers and Interpreters
Compilers.ppt (Size: 588.5 KB / Downloads: 185)
What's the difference?
Computer programs are compiled or interpreted. Languages like Assembly Language, C, C++, Fortran, Pascal were almost always compiled into machine code. Languages like Basic,VbScript and JavaScript were usually interpreted.
So what is the difference between a compiled program and an Interpreted one?
Compiling
To write a program takes these steps:
1.Edit the Program
2.Compile the program into Machine
code files.
3.Link the Machine code files into a
runnable program (also known as an
exe).
4.Debug or Run the Program
With some languages like Turbo
Pascal and Delphi steps 2 and 3 are
combined
Machine code files are self-contained modules of machine code that require linking together to build the final program. The reason for having separate machine code files is efficiency; compilers only have to recompile source code that have changed. The machine code files from the unchanged modules are reused. This is known as Making the application. If you wish to recompile and rebuild all source code then that is known as a Build.
Linking is a technically complicated process where all the function calls between different modules are hooked together, memory locations are allocated for variables and all the code is laid out in memory, then written to disk as a complete program. This is often a slower step than compiling as all the machine code files must be read into memory and linked together.
Interpreting
The steps to run a program via an interpreter are :
1. Edit the Program
2. Debug or Run the Program
3. This is a far faster process and it helps novice programmers edit and test their code quicker than using a compiler. The disadvantage is that interpreted programs run much slower than compiled programs. As much as 5-10 times slower as every line of code has to be re-read, then re-processed.
Java and C#
Both of these languages are semi-compiled. They generate an intermediate code that is optimized for interpretation. This intermediate language is independant of the underlying hardware and this makes it easier to port programs written in either to other processors, so long as an interpreter has been written for that hardware.
Java when compiled produces bytecode that is interpreted at runtime by a Java Virtual Machine (JVM). Many JVMs use a Just-In-Time compiler that converts bytecode to native machine code and then runs that code to increases the interpretation speed. In effect the Java source code is compiled in a two-stage process.
What is a Compiler?
A compiler is a program that translates human readable source code into computer executable machine code. To do this successfully the human readable code must comply with the syntax rules of whichever programming language it is written in. The compiler is only a program and cannot fix your programs for you. If you make a mistake, you have to correct the syntax or it won't compile.
What happens When You Compile Code?:
A compiler's complexity depends on the syntax of the language and how much abstraction that programming language provides. A C compiler is much simpler than
C++ Compiler or a C# Compiler.
One Pass Or Two?:
Some languages have been written so that a compiler can get away with reading the source code once and generating the machine code. Pascal is one such language. Many compilers require at least two passes. Why is this?
Sometimes it is because of
- Forward Declarations of functions or classes.
- How much optimization you require of the compiler.
Assuming that the compiler has successfully completed these stages
- Lexical Analysis.
- Syntactical Analysis.
The final stage is generating machine code. This can be an extremely complicated process, especially with modern CPUs.
The speed of the compiled executable should be as fast as possible and can vary enormously according to
The quality of the generated code.
How much optimization has been requested.
Most compilers let you specify the amount of optimization. Typically none for debugging (quicker compiles!) and full optimization for the released code.
Code Generation Is Challenging!:
The compiler writer faces challenges when writing a code generator. Many processors speed up processing by using
Instruction Pipelining.
Internal caches.
If all of the instructions within a loop can be held in the CPU cache then that loop will run much faster than if the CPU has to fetch instructions from main RAM. The CPU cache is a block of memory built into the CPU chip that is accessed much faster than data in the main RAM.
Caches And Queues:
Most CPUs have a prefetch queue where the CPU reads in instructions into the cache prior to executing them. If a conditional branch happens then the CPU has to reload the queue. So code should be generated to minimize this.
Many CPUs have separate parts for
- Integer Arithmetic
- Floating Point Arithmetic
So these operations can often run in parallel to increase the speed.
Compilers typically generate code into object files which are then linked together by a Linker program.
Managing the computer
The job of an OS is to manage all the resources in a computer. When user input is received from mouse and keyboard it has to be handled in a timely fashion. When you create or copy a file, the OS takes care of it all behind the scenes. It may store a file in a hundred different places on disk but it keeps you well away from that level of detail. You'll just see one file entry in a directory listing.
An OS is just a very complex collection of programs and nowadays takes hundreds or thousands of man hours to develop. We've come along way since Dos 6.22 which fitted on a 720 Kb floppy and Vista promises to be very large- 9 or 10 Gigabytes