Friday, January 16


Reasons for compilers and high level languages

Computers today are similar in one respect to all but the very earliest computers.  They all have a machine language that is a set of instructions that are expressed in binary (usually written in hexadecimal for brevity).  An instruction might look something like

7C0300000010

where 7C is the operation code for the instruction (perhaps meaning LOAD), 03 is the register, and 00000010 is the address.  In this case the instruction might mean that the hardware is to load register 3 with the contents of memory location 00000010.

The very earliest computers were essentially "rewired" when they were to execute a different program.  Patch panels were  used for this purpose.  Once the idea of the stored program was formulated (usually attributed to John von Neumann, although it is said that Ada Agusta, Countess of Lovelace, daughter of the English poet Lord Byron, actually proposed the idea to Charles Babbage a century earlier), computers no longer had to be rewired to be reprogrammed.  Instructions were stored in memory just like data and were executed by bringing them from memory into the processor one at a time and decoded and executed there.

The problem was writing a program using numeric codes.  It was difficult and error prone.  It wasn't long before assembly languages were invented as a way to circumvent this problem.  An assembly language instruction for the above might be

LOAD R3, X

where X is a symbolic name for the address 00000010.  Note that assembly language is

In order for assembly language to work, though, a program called an assembler is needed.  The assembler translates the mnemonic assembly instructions into the binary number form of the actual hardware instructions.

Still there are serious drawbacks to assembly language.

High level languages were thus developed.  This was quite a leap, as it was widely believed that the translators for high level languages would produce such inefficient code as to be nearly useless.  The first high level language was Fortran (for FORmula TRANslator), which proved that the worries about inefficiency were largely unfounded.  New high level languages proliferated, and with them was born the study of compilers, needed to effect the translations of the high level languages into the languages of the computers that were targeted.  Notice that high level programming languages really are abstract languages.  No computer "understands" them.  To get a concrete program that a computer can execute, a compiler must be written to make this translation from the abstract to the concrete.

The objective of a compiler

Recall the objective of a compiler.  Given a program in a source programming language, translate the program into the machine language of some target computer and operating system.

Source Program                          Target Machine
--------------------->  Compiler  ----------------------->
      File                                         Code File

There are a number of modules to the compiler.

Source                Token             Parse                       Intermediate
----------> Scanner ----------> Parser -------> Semantic Analyzer --------------->
Program               File              Tree                        Code (IR)     |                                                                                                                                |
 _________________________________________________________________________________v
v
|                        Optimized                     Target Machine
>----------- Optimizer -------------> Code Generator ------------------>
                         IR                            Code

The portions are usually referred to as the front end of the compiler, whereas the green portions are referred to as the back end.

Reasons for a front end and a back end

As discussed in the last lecture, the front end of a compiler serves a number of purposes:

Similarly, the back end serves a number of purposes.

An Example of a Complete Translation

An example of a Pascal program that has been translated into the machine code of the DEC Alpha (esus) is given here.