Chapter 5 What Is Compiler? Memory Allocation

Chapter 5 What Is Compiler? Memory Allocation

Chapter 5 What is compiler? A compiler is a special program that processes statements written in a particular programming language and turns them into machine language or "code" that a computer's processor uses. Typically, a programmer writes language statements in a language such as Pascal or C one line at a time using an editor. The file that is created contains what are called the source statements. The programmer then runs the appropriate language compiler, specifying the name of the file that contains the source statements. OR A compiler is a computer program that translates computer code written in one programming language (the source language) into another language (the target language). The name compiler is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language, object code, or machine code) to create an executable program. Role of Compiler Compliers reads the source code, outputs executable code Translates software written in a higher-level language into instructions that computer can understand. It converts the text that a programmer writes into a format the CPU can understand. The process of compilation is relatively complicated. It spends a lot of time analyzing and processing the program. The executable result is some form of machine-specific binary code. Memory allocation Memory allocation is primarily a computer hardware operation but is managed through operating system and software applications. Memory allocation process is quite similar in physical and virtual memory management. Programs and services are assigned with a specific memory as per their requirements when they are executed. Once the program has finished its operation or is idle, the memory is released and allocated to another program or merged within the primary memory. Memory allocation has two core types; Static Memory Allocation: The program is allocated memory at compile time. Dynamic Memory Allocation: The programs are allocated with memory at run time. Aspects or Phases of a Compilation The compilation process is a sequence of various phases. Each phase takes input from its previous stage, has its own representation of source program, and feeds its output to the next phase of the compiler. We basically have two phases of compilers, namely Analysis phase and Synthesis phase. Analysis phase creates an intermediate representation from the given source code. Synthesis phase creates an equivalent target program from the intermediate representation. Symbol Table – It is a data structure being used and maintained by the compiler, consists all the identifier’s name along with their types. It helps the compiler to function smoothly by finding the identifiers quickly. The compiler has two modules namely front end and back end. Front-end constitutes of the Lexical analyzer, semantic analyzer, syntax analyzer and intermediate code generator. And the rest are assembled to form the back end. 1. Lexical Analyzer – It reads the program and converts it into tokens. It converts a stream of lexemes into a stream of tokens. Tokens are defined by regular expressions which are understood by the lexical analyzer. It also removes white-spaces and comments. 2. Syntax Analyzer – It is sometimes called as parser. It constructs the parse tree. It takes all the tokens one by one and uses Context Free Grammar to construct the parse tree. Why Grammar ? The rules of programming can be entirely represented in some few productions. Using these productions we can represent what the program actually is. The input has to be checked whether it is in the desired format or not. Syntax error can be detected at this level if the input is not in accordance with the grammar. 3.Semantic Analyzer – It verifies the parse tree, whether it’s meaningful or not. It furthermore produces a verified parse tree.It also does type checking, Label checking and Flow control checking. 4.Intermediate Code Generation- After semantic analysis the compiler generates an intermediate code of the source code for the target machine. It represents a program for some abstract machine. It is in between the high-level language and the machine language. This intermediate code should be generated in such a way that it makes it easier to be translated into the target machine code. 5.Code Optimization The next phase does code optimization of the intermediate code. Optimization can be assumed as something that removes unnecessary code lines, and arranges the sequence of statements in order to speed up the program execution without wasting resources (CPU, memory). 6.Target Code Generator – The main purpose of Target Code generator is to write a code that the machine can understand and also register allocation, instruction selection etc. The output is dependent on the type of assembler. This is the final stage of compilation. Code Optimisation The programs represented in the intermediate code form usually contains much scope for optimisation both in terms of storage space as well as run time efficiency of the intended output program. Sometimes the input program itself contains such scope. Besides that, the process of generating the intermediate code representation usually leaves much room for such optimisation. Hence, compilers usually implement explicit steps to optimise the intermediate code. OR Optimization is a program transformation technique, which tries to improve the code by making it consume less resources (i.e. CPU, Memory) and deliver high speed. In optimization, high-level general programming constructs are replaced by very efficient low-level programming codes. A code optimizing process must follow the three rules given below: The output code must not, in any way, change the meaning of the program. Optimization should increase the speed of the program and if possible, the program should demand less number of resources. Optimization should itself be fast and should not delay the overall compiling process. Efforts for an optimized code can be made at various levels of compiling the process. At the beginning, users can change/rearrange the code or use better algorithms to write the code. After generating intermediate code, the compiler can modify the intermediate code by address calculations and improving loops. While producing the target machine code, the compiler can make use of memory hierarchy and CPU registers. Optimization can be categorized broadly into two types : Machine independent and Machine Dependent. Machine-independent Optimization In this optimization, the compiler takes in the intermediate code and transforms a part of the code that does not involve any CPU registers and/or absolute memory locations.For example do { item = 10; value = value + item; } while(value<100); This code involves repeated assignment of the identifier item, which if we put this way: Item = 10; do { value = value + item; } while(value<100); should not only save the CPU cycles, but can be used on any processor. Machine-dependent Optimization Machine-dependent optimization is done after the target code has been generated and when the code is transformed according to the target machine architecture. It involves CPU registers and may have absolute memory references rather than relative references. Machine- dependent optimizers put efforts to take maximum advantage of memory hierarchy. What Is Interpreter? In computer science, an interpreter is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program. An interpreter generally uses one of the following strategies for program execution: 1. Parse the source code and perform its behavior directly; 2. Translate source code into some efficient intermediate representation and immediately execute this; 3. Explicitly execute stored precompiled code made by a compiler which is part of the interpreter system. Role of Interpreter The interpreter converts the source code line-by-line during RUN Time. Interpret completely translates a program written in a high-level language into machine level language. Interpreter allows evaluation and modification of the program while it is executing. Relatively less time spent for analyzing and processing the program Program execution is relatively slow compared to compiler OR KEY DIFFERENCE BETWEEN COMPILER AND INTERPRETER Compiler transforms code written in a high-level programming language into the machine code, at once, before program runs, whereas an Interpreter coverts each high- level program statement, one by one, into the machine code, during program run. Compiled code runs faster while interpreted code runs slower. Compiler displays all errors after compilation, on the other hand, the Interpreter displays errors of each line one by one. Compiler is based on translation linking-loading model, whereas Interpreter is based on Interpretation Method. Compiler takes an entire program whereas the Interpreter takes a single line of code. CHAPTER 6 LINKERS Linker • A System Software that Combines two or more separate object programs and supplies the information needed to allow references between them . • Two Types/scheme – Dynamic Linking – Static Linking 1. Static Linking: It is performed during the compilation of source program. Linking is performed before execution in static linking. Static linkers takes input a collection of relocatable object files and command line arguments and generate as output a

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    19 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us