the functions and purposes of translators code generation (intermediate code, optimisation, final...

15
The Functions and The Functions and Purposes of Purposes of Translators Translators Code Generation Code Generation (Intermediate (Intermediate Code, Code, Optimisation, Final Code) Optimisation, Final Code) , , Linkers & Loaders Linkers & Loaders

Upload: sabastian-plaisted

Post on 15-Dec-2015

234 views

Category:

Documents


1 download

TRANSCRIPT

The Functions and The Functions and Purposes of TranslatorsPurposes of Translators

Code Generation Code Generation (Intermediate Code, (Intermediate Code,

Optimisation, Final Code)Optimisation, Final Code), ,

Linkers & LoadersLinkers & Loaders

Learning ObjectivesLearning Objectives

Explain the code generation phase.

Explain the purpose of linkers and loaders.

Translation ProcessTranslation Process

Code GenerationCode Generation

The address of each variable is now The address of each variable is now calculated and stored in the symbol table calculated and stored in the symbol table as each is encountered.as each is encountered.

Intermediate code is produced which, after Intermediate code is produced which, after optimisation optimisation (see next slide)(see next slide), is turned into , is turned into executable code / machine codeexecutable code / machine code All errors due to incorrect use of the language All errors due to incorrect use of the language

have been removed by this stage.have been removed by this stage.

OptimisationOptimisation

Reduce the size of the object code (so it will Reduce the size of the object code (so it will take up less memory) by removing any duplicate take up less memory) by removing any duplicate or redundant instructions which improve speed or redundant instructions which improve speed of execution.of execution.

One practical example of code which would One practical example of code which would benefit from optimising would be when inputting benefit from optimising would be when inputting a list of numbers to find their total as there would a list of numbers to find their total as there would be no need to store the numbers first.be no need to store the numbers first. For further explanations and examples of “duplicate” For further explanations and examples of “duplicate”

or “redundant” instructions:or “redundant” instructions:

http://en.wikipedia.org/wiki/Peephole_optimization

LoadersLoaders

Copy object code into (primary) memory ready Copy object code into (primary) memory ready for execution at run time.for execution at run time.The loader decides where in memory to place The loader decides where in memory to place the code and then adjusts memory addresses as the code and then adjusts memory addresses as described in described in Memory ManagementMemory Management..

However, this applies to “whole” programs which However, this applies to “whole” programs which have all the procedures and code they use have all the procedures and code they use within them. within them. Therefore “absolute” addresses are known and are Therefore “absolute” addresses are known and are

adjusted by loader into “relative” addresses at run adjusted by loader into “relative” addresses at run time.time.

Library RoutinesLibrary Routines

Already compiled procedures / modules Already compiled procedures / modules which are intentionally external to the which are intentionally external to the main compiled program so that:main compiled program so that:

1.1. They are available for other programs to use.They are available for other programs to use.

2.2. If they are changed then these changes If they are changed then these changes affect all programs which use them without affect all programs which use them without the need to change each program which the need to change each program which uses them.uses them.

LinkersLinkers

As library routines are external and are already As library routines are external and are already compiled when a program which requires them compiled when a program which requires them is compiled then the addresses of these routines is compiled then the addresses of these routines are completely unknown to the program.are completely unknown to the program. Only their names / identifiers are known.Only their names / identifiers are known.

Linkers calculate the addresses of any external Linkers calculate the addresses of any external references in a compiled program to library references in a compiled program to library routines, after the main program and the library routines, after the main program and the library routines routines have been loaded (by a loader) into have been loaded (by a loader) into main memory.main memory. This enables the main program together with the This enables the main program together with the

library routines it uses (modules) to be executed and library routines it uses (modules) to be executed and interact with one another.interact with one another.

Linking Loaders Linking Loaders

Load and link.Load and link.

The variable identifier.The variable identifier.The kind of variable.The kind of variable. E.g. Simple variable, structured variable such as an E.g. Simple variable, structured variable such as an

array, procedure, keyword etc …array, procedure, keyword etc …

Type of variable.Type of variable. E.g. Integer, decimal, string etc …E.g. Integer, decimal, string etc …

Other Information.Other Information. E.g. Bounds of arrayE.g. Bounds of array

Run-time address of the variable or its value if it Run-time address of the variable or its value if it is a constant.is a constant.

Symbol TableSymbol Table

Syntax (& Semantic Analysis

Lexical Analysis

Code Generation

PlenaryPlenary

Explain the code generation phase.Explain the code generation phase.

PlenaryPlenary

The address of each variable is now The address of each variable is now calculated and stored in the symbol table calculated and stored in the symbol table as each is encountered.as each is encountered.

Intermediate code is produced which, after Intermediate code is produced which, after optimisation optimisation (see next slide)(see next slide), is turned into , is turned into executable code / machine codeexecutable code / machine code All errors due to incorrect use of the language All errors due to incorrect use of the language

have been removed by this stage.have been removed by this stage.

PlenaryPlenary

Explain the purpose of linkers and loaders.Explain the purpose of linkers and loaders.

LoadersLoaders

Copy object code into (primary) memory ready Copy object code into (primary) memory ready for execution at run time.for execution at run time.The loader decides where in memory to place The loader decides where in memory to place the code and then adjusts memory addresses as the code and then adjusts memory addresses as described in described in Memory ManagementMemory Management..

However, this applies to “whole” programs which However, this applies to “whole” programs which have all the procedures and code they use have all the procedures and code they use within them. within them. Therefore “absolute” addresses are known and are Therefore “absolute” addresses are known and are

adjusted by loader into “relative” addresses at run adjusted by loader into “relative” addresses at run time.time.

LinkersLinkers

As library routines are external and are already As library routines are external and are already compiled when a program which requires them compiled when a program which requires them is compiled then the addresses of these routines is compiled then the addresses of these routines are completely unknown to the program.are completely unknown to the program. Only their names / identifiers are known.Only their names / identifiers are known.

Linkers calculate the addresses of any external Linkers calculate the addresses of any external references in a compiled program to library references in a compiled program to library routines, after the main program and the library routines, after the main program and the library routines routines have been loaded (by a loader) into have been loaded (by a loader) into main memory.main memory. This enables the main program together with the This enables the main program together with the

library routines it uses (modules) to be executed and library routines it uses (modules) to be executed and interact with one another.interact with one another.