Professional Documents
Culture Documents
Example 1: We shall design a TM to implement the function "multiplica-tion." That is, our TM will start with 0'9041 on its tape, and will
end with 0"'" on the tape. An outline of the strategy is:
1. The tape will, in general, have one nonblank string of the form Of10"10k" for some k.
2. In one basic step, we change a 0 in the first group to B and add n 0's to the last group, giving us a string of the form 0'101'10+1)".
3. As a result, we copy the group of n 0's to the end m times, once each time we change a 0 in the first group to B. When the first group
of 0's is completely changed to blanks, there will be mn 0's in the last group.
4. The final step is to change the leading 10"l to blanks, and we are done.
EXERCISE : A COMMON OPERATION IN TURING-MACHINE PROGRAMS INVOLVES "SHIFTING
OVER." IDEALLY, WE WOULD LIKE TO CREATE AN EXTRA CELL AT THE CURRENT HEAD
POSITION, IN WHICH WE COULD STORE SOME CHARACTER. HOWEVER, WE CANNOT EDIT
THE TAPE IN THIS WAY. RATHER, WE NEED TO MOVE THE CONTENTS OF EACH OF THE
CELLS TO THE RIGHT OF THE CURRENT HEAD POSITION ONE CELL RIGHT, AND THEN FIND
OUR WAY BACK TO THE CURRENT HEAD POSITION. SHOW HOW TO PERFORM
• Intermediate codes may be represented mostly in quadruples, triples, indirect triples, Postfix notation, syntax trees,
DAG's, etc.
*The code generation phase just proceeds on an assumption that the input are free from all of syntactic and state
semantic errors, the necessary type checking has taken place and the type-conversion operators have been inserted
wherever necessary.
2. Target program
The target program is the output of the code generator.
•Relocatable machine language as an output allows subprograms and subroutines to be compiled separately.
Relocatable object modules can be linked together and loaded by linking loader. But there is added expense of
linking and loading. •Assembly language as output makes the code generation easier. We can genera symbolic
instructions and use macro-facilities of assembler in generating code.
3) Memory Management
1. Mapping the names in the source program to the addresses of data objects is done by the
front end and the code generator.
2. A name in the three address statements refers to the symbol table entry for
name.
3. Then from the symbol table entry, a relative address can be determined for the
name.
4) Instruction selection
1. Selecting the best instructions will improve the efficiency of the program. It includes the
instructions that should be complete and uniform. Instruction speeds and machine idioms also
plays a major role when efficiency is considered.
5) Register allocation
Use of registers make the computations faster in comparison to that of memory, so efficient utilization of
registers is important.
The code generator decides the order in which the instruction will be executed. The order of computations
affects the efficiency of the target code. Among many computational orders, some will require only fewer
registers to hold the intermediate results. However, picking the best order in the general case is a difficult NP-
complete problem.