{"id":241,"date":"2018-07-20T09:19:08","date_gmt":"2018-07-20T09:19:08","guid":{"rendered":"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=241"},"modified":"2018-07-20T09:19:41","modified_gmt":"2018-07-20T09:19:41","slug":"code-generator-introduction-and-basic-blocks","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/chapter\/code-generator-introduction-and-basic-blocks\/","title":{"rendered":"Code Generator Introduction and Basic Blocks"},"content":{"raw":"<p style=\"text-align: justify\">After discussing the various semantic rules necessary to convert every programming construct into three-address code, in this module, we will discuss the next phase of the compiler \u2013 Code generation. We will start this module by understanding the issues in generating code. As a first step in code generation, we will discuss the concept of basic blocks and flow graphs<\/p>\r\n&nbsp;\r\n\r\n<strong>28.1 Code Generation \u2013 Introduction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The position of the code generation module is shown in figure 28.1. As shown in figure 28.1, the front-end of the compiler delivers intermediate code as output after processing the lexical, syntactic and semantic phases. This intermediate code is used for code generation. The intermediate code could also be optimized and the optimized intermediate code could also be fed to the code generator.<\/p>\r\n<img class=\"size-full wp-image-242 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-141.png\" alt=\"\" width=\"668\" height=\"263\" \/>\r\n<div>\r\n\r\nThe following are the expectations of the code generator:\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Code produced by compiler must be correct \u2013 The conversion from the source to the target language should preserve the semantics of the source program.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Code produced by compiler should be of high quality \u2013 The code generation algorithm should be aware of the instructions set of the target language. The code should be generated, by effectively using these target machine resources. Normally, heuristic techniques are used to generate good but suboptimal code, as optimal code generation is an undecidable problem.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>28.2 Issues in the Code Generator<\/strong><\/p>\r\n<p style=\"text-align: justify\"><\/p>\r\n<p style=\"text-align: justify\">The code generator needs to address so many issues before actually generating the code. The following are some of the issues and let us discuss each one of them in brief.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Input to the code generator: The code generator gets the three-address code as input. The format in which this three-address code is fed as input need to be decided. The following are some of the ways of feeding input to the code generator.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Linear \u2013 The input could be a string of characters where we use postfix notation to express the input.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Tables \u2013 As discussed in the previous modules, the three-address code is typically represented using Quadruples, Triples or Indirect triples and this table could be served as input<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Non-linear \u2013 Abstract Syntax tree (AST) or Directed Acyclic Graph (DAG) could be used as input to the code generator after converting the input into AST or DAG representation\u00a0 In addition to the input, symbol table information need to be given to the code generator. The format in which symbol table needs to be available to the code generator needs to be decided.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Target Program Code: The back-end code generator of a compiler may generate different forms of code, depending on the requirements. The target program code needs to be specified as assembly language or machine language. The following are some of the forms<\/p>\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Absolute machine code \u2013 This code is directly executable\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Relocatable machine code \u2013 This is a relocatable machine language and is normally available as an object files\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Assembly language \u2013 This is available in any assembly language which depends on the target machine and later an assembler converts this to machine language.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Byte code forms for interpreters \u2013 Java virtual machine has the write once and run anywhere concept where the output should be in Byte code.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Target Machine: Implementing code generation requires thorough understanding of the target machine architecture and its instruction set. Consider a hypothetical machine with the following features:<\/p>\r\n&nbsp;\r\n\r\n\u2013\u00a0 The instructions are byte-addressable where a word is 4 bytes\r\n\r\n\u2013\u00a0 Has <em>n<\/em> general purpose registers <strong>R0<\/strong>, <strong>R1<\/strong>, \u2026, <strong>R<\/strong><em>n-<\/em>1\r\n\r\n\u2013\u00a0 Instructions are of Two-address, ones of the form\r\n\r\n<em>op source<\/em>,<em> destination\u00a0<\/em>where op stands for op-codes.\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\nFor example\r\n\r\n&nbsp;\r\n\r\n<strong>MOV <\/strong>(move content of <em>source<\/em> to <em>destination<\/em>)\r\n\r\n<strong>ADD <\/strong>(add content of <em>source<\/em> to <em>destination<\/em>)\r\n\r\n<strong>SUB <\/strong>(subtract content of <em>source<\/em> from <em>destination<\/em>)\r\n\r\n<img class=\"size-full wp-image-243 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-142.png\" alt=\"\" width=\"670\" height=\"878\" \/>\r\n\r\n<img class=\"size-full wp-image-244 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-143.png\" alt=\"\" width=\"651\" height=\"840\" \/>\r\n<div>\r\n\r\nThe following would be the instructions with a total cost of 6 (2+2+2):\r\n\r\n&nbsp;\r\n\r\n<strong>MOV <\/strong>y<strong>,R0<\/strong>\r\n\r\n<strong>ADD <\/strong>z<strong>,R0<\/strong>\r\n\r\n<strong>MOV R0,<\/strong>x\r\n\r\n&nbsp;\r\n\r\nConsider another instruction <strong>a:=a+1<\/strong> and if we adopt the same strategy to convert this instruction to target code, the following would be the result with a cost of 6 (2+2+2)\u201d\r\n\r\n&nbsp;\r\n\r\n<strong>MOV a,R0<\/strong>\r\n\r\n<strong>ADD #1,R0<\/strong>\r\n\r\n<strong>MOV R0,a<\/strong>\r\n\r\n&nbsp;\r\n\r\nIf we replace this with the following instruction, the cost would be 3 (1 + 1 +1) which is \u201c1\u201d each for ADD, #1, a\r\n\r\n&nbsp;\r\n\r\n<strong>ADD #1,a<\/strong>\r\n\r\n&nbsp;\r\n\r\nOn the other hand, if this is replaced by the following instruction the cost would be 2 (1 for INC and 1 for \u201ca\u201d)\r\n\r\n&nbsp;\r\n\r\n<strong>INC a<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Instruction Selection \u2013 The choice of instructions also could change based on the addressing Modes\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Suppose we translate <strong>a:=b+c<\/strong> into\r\n\r\n&nbsp;\r\n\r\n<strong>MOV b,R0 <\/strong>\r\n\r\n<strong>ADD c,R0 <\/strong>\r\n\r\n<strong>MOV R0,a<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe cost of this would be 6 (2 + 2 +2)\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 On the other hand, assuming addresses of <strong>a<\/strong>, <strong>b<\/strong>, and <strong>c<\/strong> are stored in <strong>R0<\/strong>, <strong>R1<\/strong>, and <strong>R2<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>MOV *R1,*R0<\/strong>\r\n\r\n<strong> ADD *R2,*R0<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe cost of this would be 2 (1 +1) which is just due to the MOV and ADD instructions\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Consider, <strong>R1<\/strong> and <strong>R2<\/strong> contain values of <strong>b<\/strong> and <strong>c<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>ADD R2,R1 <\/strong>\r\n\r\n<strong>MOV R1,a<\/strong>\r\n\r\n&nbsp;\r\n\r\nWould incur a cost of 3 (1 + 2) for ADD, MOV.\r\n\r\n<\/div>\r\n<ul>\r\n \t<li>Need for Global Code optimization<\/li>\r\n<\/ul>\r\nThis is the next issue in code generation. Consider the instruction and its corresponding three-address code as given in the table 28.3(a):\r\n\r\n&nbsp;\r\n\r\nTable 28.3 a \u2013 Instruction and code\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 349.063px\"><strong>Instruction<\/strong><\/td>\r\n<td style=\"width: 311.063px\"><strong>Code<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 349.063px\"><em>x<\/em><strong>:=<\/strong><em>y<\/em><strong>+<\/strong><em>z<\/em><\/td>\r\n<td style=\"width: 311.063px\"><strong>MOV<\/strong><em>y<\/em><strong>,R0<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 349.063px\"><\/td>\r\n<td style=\"width: 311.063px\"><strong>ADD <\/strong><em>z<\/em><strong>,R0<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 349.063px\"><\/td>\r\n<td style=\"width: 311.063px\"><strong>MOV R0,<\/strong><em>x<\/em><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 349.063px\"><\/td>\r\n<td style=\"width: 311.063px\"><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nConsider the following sequence of instructions and supposing we use the same logic to translate, then we will end up in the table 28.3 (b).\r\n\r\n&nbsp;\r\n\r\nTable 28.3 (b) \u2013 Sequence of Instructions and code\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" style=\"height: 224px\" border=\"1\">\r\n<tbody>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 132.063px;height: 28px\"><strong>Instruction<\/strong><\/td>\r\n<td style=\"width: 528.063px;height: 28px\"><strong>Code<\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 132.063px;height: 28px\"><\/td>\r\n<td style=\"width: 528.063px;height: 28px\"><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 132.063px;height: 28px\"><strong>a:=b+c<\/strong><\/td>\r\n<td style=\"width: 528.063px;height: 28px\"><strong>MOVa,R0<\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 132.063px;height: 28px\"><\/td>\r\n<td style=\"width: 528.063px;height: 28px\"><strong>ADD b,R0<\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 132.063px;height: 28px\"><\/td>\r\n<td style=\"width: 528.063px;height: 28px\"><strong>MOV R0,a<\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 132.063px;height: 28px\"><strong>d:=a+e<\/strong><\/td>\r\n<td style=\"width: 528.063px;height: 28px\"><strong>MOV a,R0\u00a0 ------------ Redundant as R0 is used<\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 132.063px;height: 28px\"><\/td>\r\n<td style=\"width: 528.063px;height: 28px\"><strong>ADD e,R0<\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 132.063px;height: 28px\"><\/td>\r\n<td style=\"width: 528.063px;height: 28px\"><strong>MOV R0,d<\/strong><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\">As given in the Table 28.3 (b), register R0 already had the value of \u2018a\u2019 according to the previous computation and hence this MOV instruction as indicated is redundant and this calls for Global code optimization.<\/p>\r\n\r\n<ul>\r\n \t<li>Register allocation and Assignment<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">One of the important issues in code generation is register allocations. The number of registers in any architecture is limited to match the number of variable in a high- level program. Efficient utilization of the limited set of registers is important to generate good code. Registers are assigned by<\/p>\r\n&nbsp;\r\n\r\n\u2013 <em>Register allocation<\/em> to select the set of variables that will reside in registers at a point in the code\r\n\r\n\u2013\u00a0 <em>Register assignment<\/em> to pick the specific register that a variable will reside in\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">However, finding an optimal register assignment in general is NP-complete. Consider the table 28.3 (c) that has the sequence of instructions and their corresponding three-address code.<\/p>\r\n<img class=\"size-full wp-image-245 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-144.png\" alt=\"\" width=\"652\" height=\"831\" \/>\r\n\r\n<strong>28.3 Basic Blocks and Flowgraphs<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To handle all the issues in the code generation process, we need to construct basic blocks and flow graphs from the sequence of three-address code. Basic blocks are computation sequences and they form the node of a flow graph. Flow graph is a graphical representation of three-address code where nodes are the basic blocks and edges indicate the flow of control between basic blocks. The primary objective of flow graphs is code optimization include optimal register allocation.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">A <em>flow graph<\/em> is a graphical depiction of a sequence of instructions. A flow graph can be defined at the intermediate code level or target code level. Consider the following example at the final code level and the corresponding flow graph of Figure 28.2.<\/p>\r\n<img class=\"size-full wp-image-246 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-145.png\" alt=\"\" width=\"739\" height=\"670\" \/>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-247 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-146.png\" alt=\"\" width=\"479\" height=\"290\" \/>\r\n\r\n&nbsp;\r\n<div>\r\n\r\nFigure 28.3 Flow graph with basic blocks highlighted\r\n\r\n&nbsp;\r\n\r\nIn a flow graph, suppose there is an edge from basic block B1 to B2 as <em>B<\/em>1\u00ae<em>B<\/em>2, then <em>B<\/em>1 is a <em>predecessor <\/em>of<em> B<\/em>2 and<em> B<\/em>2 is a<em> successor <\/em>of<em> B<\/em>1\r\n\r\n&nbsp;\r\n\r\nIn figure 28.3, we have grouped the statements into basic blocks 1, 2, 3 and the manner to do it is given algorithm 28.1\r\n\r\n&nbsp;\r\n\r\nAlgorithm 28.1 Basic block construction\r\n\r\n&nbsp;\r\n\r\n<em>Input<\/em>:\u00a0 A sequence of three-address statements\r\n\r\n<\/div>\r\n<div>\r\n\r\n<em>Output<\/em>:\u00a0 \u00a0A\u00a0 \u00a0 \u00a0list\u00a0 of\u00a0 basic\u00a0 blocks\u00a0 with each\r\n\r\nthree-address statement<span style=\"text-align: initial;font-size: 1em\">in\u00a0 exactly one block<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n1.\u00a0\u00a0\u00a0\u00a0\u00a0 Determine the set of <em>leaders<\/em>, the first statements if basic blocks\r\n\r\na)\u00a0\u00a0\u00a0\u00a0\u00a0 The first statement is the leader\r\n\r\nb)\u00a0\u00a0\u00a0\u00a0\u00a0 Any statement that is the target of a goto is a leader\r\n\r\nc)\u00a0\u00a0\u00a0\u00a0\u00a0 Any statement that immediately follows a goto is a leader\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\n2.\u00a0\u00a0 For\u00a0\u00a0\u00a0 each\u00a0\u00a0\u00a0 leader, statements\u00a0\u00a0\u00a0 up\u00a0\u00a0\u00a0 to\u00a0 its\u00a0 but\u00a0 basic not\u00a0 block including\u00a0 consist the\u00a0 of\u00a0 next\u00a0 the\u00a0 \u00a0leader leader\u00a0\u00a0\u00a0 or\u00a0 and\u00a0 the all end\u00a0\u00a0<span style=\"font-size: 1em;text-align: initial\">of the program<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\nFrom algorithm 28.1, it is evident that the first step is to identify the set of leaders and group the statements between a pair of leaders into a basic block. An example of this is given in the next module\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>: In this module, we understood the need for code generation and the various issues that need to be handled in code generation. Basic block and algorithm for basic block creation from three address \/ final code was discussed.","rendered":"<p style=\"text-align: justify\">After discussing the various semantic rules necessary to convert every programming construct into three-address code, in this module, we will discuss the next phase of the compiler \u2013 Code generation. We will start this module by understanding the issues in generating code. As a first step in code generation, we will discuss the concept of basic blocks and flow graphs<\/p>\n<p>&nbsp;<\/p>\n<p><strong>28.1 Code Generation \u2013 Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The position of the code generation module is shown in figure 28.1. As shown in figure 28.1, the front-end of the compiler delivers intermediate code as output after processing the lexical, syntactic and semantic phases. This intermediate code is used for code generation. The intermediate code could also be optimized and the optimized intermediate code could also be fed to the code generator.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-242 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-141.png\" alt=\"\" width=\"668\" height=\"263\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-141.png 668w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-141-300x118.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-141-65x26.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-141-225x89.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-141-350x138.png 350w\" sizes=\"auto, (max-width: 668px) 100vw, 668px\" \/><\/p>\n<div>\n<p>The following are the expectations of the code generator:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Code produced by compiler must be correct \u2013 The conversion from the source to the target language should preserve the semantics of the source program.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Code produced by compiler should be of high quality \u2013 The code generation algorithm should be aware of the instructions set of the target language. The code should be generated, by effectively using these target machine resources. Normally, heuristic techniques are used to generate good but suboptimal code, as optimal code generation is an undecidable problem.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>28.2 Issues in the Code Generator<\/strong><\/p>\n<p style=\"text-align: justify\">\n<p style=\"text-align: justify\">The code generator needs to address so many issues before actually generating the code. The following are some of the issues and let us discuss each one of them in brief.<\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Input to the code generator: The code generator gets the three-address code as input. The format in which this three-address code is fed as input need to be decided. The following are some of the ways of feeding input to the code generator.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Linear \u2013 The input could be a string of characters where we use postfix notation to express the input.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Tables \u2013 As discussed in the previous modules, the three-address code is typically represented using Quadruples, Triples or Indirect triples and this table could be served as input<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Non-linear \u2013 Abstract Syntax tree (AST) or Directed Acyclic Graph (DAG) could be used as input to the code generator after converting the input into AST or DAG representation\u00a0 In addition to the input, symbol table information need to be given to the code generator. The format in which symbol table needs to be available to the code generator needs to be decided.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Target Program Code: The back-end code generator of a compiler may generate different forms of code, depending on the requirements. The target program code needs to be specified as assembly language or machine language. The following are some of the forms<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Absolute machine code \u2013 This code is directly executable<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Relocatable machine code \u2013 This is a relocatable machine language and is normally available as an object files<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Assembly language \u2013 This is available in any assembly language which depends on the target machine and later an assembler converts this to machine language.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Byte code forms for interpreters \u2013 Java virtual machine has the write once and run anywhere concept where the output should be in Byte code.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Target Machine: Implementing code generation requires thorough understanding of the target machine architecture and its instruction set. Consider a hypothetical machine with the following features:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2013\u00a0 The instructions are byte-addressable where a word is 4 bytes<\/p>\n<p>\u2013\u00a0 Has <em>n<\/em> general purpose registers <strong>R0<\/strong>, <strong>R1<\/strong>, \u2026, <strong>R<\/strong><em>n-<\/em>1<\/p>\n<p>\u2013\u00a0 Instructions are of Two-address, ones of the form<\/p>\n<p><em>op source<\/em>,<em> destination\u00a0<\/em>where op stands for op-codes.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>For example<\/p>\n<p>&nbsp;<\/p>\n<p><strong>MOV <\/strong>(move content of <em>source<\/em> to <em>destination<\/em>)<\/p>\n<p><strong>ADD <\/strong>(add content of <em>source<\/em> to <em>destination<\/em>)<\/p>\n<p><strong>SUB <\/strong>(subtract content of <em>source<\/em> from <em>destination<\/em>)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-243 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-142.png\" alt=\"\" width=\"670\" height=\"878\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-142.png 670w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-142-229x300.png 229w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-142-65x85.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-142-225x295.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-142-350x459.png 350w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-244 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-143.png\" alt=\"\" width=\"651\" height=\"840\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-143.png 651w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-143-233x300.png 233w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-143-65x84.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-143-225x290.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-143-350x452.png 350w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/p>\n<div>\n<p>The following would be the instructions with a total cost of 6 (2+2+2):<\/p>\n<p>&nbsp;<\/p>\n<p><strong>MOV <\/strong>y<strong>,R0<\/strong><\/p>\n<p><strong>ADD <\/strong>z<strong>,R0<\/strong><\/p>\n<p><strong>MOV R0,<\/strong>x<\/p>\n<p>&nbsp;<\/p>\n<p>Consider another instruction <strong>a:=a+1<\/strong> and if we adopt the same strategy to convert this instruction to target code, the following would be the result with a cost of 6 (2+2+2)\u201d<\/p>\n<p>&nbsp;<\/p>\n<p><strong>MOV a,R0<\/strong><\/p>\n<p><strong>ADD #1,R0<\/strong><\/p>\n<p><strong>MOV R0,a<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>If we replace this with the following instruction, the cost would be 3 (1 + 1 +1) which is \u201c1\u201d each for ADD, #1, a<\/p>\n<p>&nbsp;<\/p>\n<p><strong>ADD #1,a<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>On the other hand, if this is replaced by the following instruction the cost would be 2 (1 for INC and 1 for \u201ca\u201d)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>INC a<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Instruction Selection \u2013 The choice of instructions also could change based on the addressing Modes<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Suppose we translate <strong>a:=b+c<\/strong> into<\/p>\n<p>&nbsp;<\/p>\n<p><strong>MOV b,R0 <\/strong><\/p>\n<p><strong>ADD c,R0 <\/strong><\/p>\n<p><strong>MOV R0,a<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The cost of this would be 6 (2 + 2 +2)<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 On the other hand, assuming addresses of <strong>a<\/strong>, <strong>b<\/strong>, and <strong>c<\/strong> are stored in <strong>R0<\/strong>, <strong>R1<\/strong>, and <strong>R2<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>MOV *R1,*R0<\/strong><\/p>\n<p><strong> ADD *R2,*R0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The cost of this would be 2 (1 +1) which is just due to the MOV and ADD instructions<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Consider, <strong>R1<\/strong> and <strong>R2<\/strong> contain values of <strong>b<\/strong> and <strong>c<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>ADD R2,R1 <\/strong><\/p>\n<p><strong>MOV R1,a<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Would incur a cost of 3 (1 + 2) for ADD, MOV.<\/p>\n<\/div>\n<ul>\n<li>Need for Global Code optimization<\/li>\n<\/ul>\n<p>This is the next issue in code generation. Consider the instruction and its corresponding three-address code as given in the table 28.3(a):<\/p>\n<p>&nbsp;<\/p>\n<p>Table 28.3 a \u2013 Instruction and code<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td style=\"width: 349.063px\"><strong>Instruction<\/strong><\/td>\n<td style=\"width: 311.063px\"><strong>Code<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 349.063px\"><em>x<\/em><strong>:=<\/strong><em>y<\/em><strong>+<\/strong><em>z<\/em><\/td>\n<td style=\"width: 311.063px\"><strong>MOV<\/strong><em>y<\/em><strong>,R0<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 349.063px\"><\/td>\n<td style=\"width: 311.063px\"><strong>ADD <\/strong><em>z<\/em><strong>,R0<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 349.063px\"><\/td>\n<td style=\"width: 311.063px\"><strong>MOV R0,<\/strong><em>x<\/em><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 349.063px\"><\/td>\n<td style=\"width: 311.063px\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Consider the following sequence of instructions and supposing we use the same logic to translate, then we will end up in the table 28.3 (b).<\/p>\n<p>&nbsp;<\/p>\n<p>Table 28.3 (b) \u2013 Sequence of Instructions and code<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\" style=\"height: 224px\">\n<tbody>\n<tr style=\"height: 28px\">\n<td style=\"width: 132.063px;height: 28px\"><strong>Instruction<\/strong><\/td>\n<td style=\"width: 528.063px;height: 28px\"><strong>Code<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 132.063px;height: 28px\"><\/td>\n<td style=\"width: 528.063px;height: 28px\"><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 132.063px;height: 28px\"><strong>a:=b+c<\/strong><\/td>\n<td style=\"width: 528.063px;height: 28px\"><strong>MOVa,R0<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 132.063px;height: 28px\"><\/td>\n<td style=\"width: 528.063px;height: 28px\"><strong>ADD b,R0<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 132.063px;height: 28px\"><\/td>\n<td style=\"width: 528.063px;height: 28px\"><strong>MOV R0,a<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 132.063px;height: 28px\"><strong>d:=a+e<\/strong><\/td>\n<td style=\"width: 528.063px;height: 28px\"><strong>MOV a,R0\u00a0 &#8212;&#8212;&#8212;&#8212; Redundant as R0 is used<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 132.063px;height: 28px\"><\/td>\n<td style=\"width: 528.063px;height: 28px\"><strong>ADD e,R0<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 132.063px;height: 28px\"><\/td>\n<td style=\"width: 528.063px;height: 28px\"><strong>MOV R0,d<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As given in the Table 28.3 (b), register R0 already had the value of \u2018a\u2019 according to the previous computation and hence this MOV instruction as indicated is redundant and this calls for Global code optimization.<\/p>\n<ul>\n<li>Register allocation and Assignment<\/li>\n<\/ul>\n<p style=\"text-align: justify\">One of the important issues in code generation is register allocations. The number of registers in any architecture is limited to match the number of variable in a high- level program. Efficient utilization of the limited set of registers is important to generate good code. Registers are assigned by<\/p>\n<p>&nbsp;<\/p>\n<p>\u2013 <em>Register allocation<\/em> to select the set of variables that will reside in registers at a point in the code<\/p>\n<p>\u2013\u00a0 <em>Register assignment<\/em> to pick the specific register that a variable will reside in<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">However, finding an optimal register assignment in general is NP-complete. Consider the table 28.3 (c) that has the sequence of instructions and their corresponding three-address code.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-245 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-144.png\" alt=\"\" width=\"652\" height=\"831\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-144.png 652w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-144-235x300.png 235w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-144-65x83.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-144-225x287.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-144-350x446.png 350w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/p>\n<p><strong>28.3 Basic Blocks and Flowgraphs<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To handle all the issues in the code generation process, we need to construct basic blocks and flow graphs from the sequence of three-address code. Basic blocks are computation sequences and they form the node of a flow graph. Flow graph is a graphical representation of three-address code where nodes are the basic blocks and edges indicate the flow of control between basic blocks. The primary objective of flow graphs is code optimization include optimal register allocation.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A <em>flow graph<\/em> is a graphical depiction of a sequence of instructions. A flow graph can be defined at the intermediate code level or target code level. Consider the following example at the final code level and the corresponding flow graph of Figure 28.2.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-246 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-145.png\" alt=\"\" width=\"739\" height=\"670\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-145.png 739w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-145-300x272.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-145-65x59.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-145-225x204.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-145-350x317.png 350w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-247 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-146.png\" alt=\"\" width=\"479\" height=\"290\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-146.png 479w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-146-300x182.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-146-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-146-225x136.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-146-350x212.png 350w\" sizes=\"auto, (max-width: 479px) 100vw, 479px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p>Figure 28.3 Flow graph with basic blocks highlighted<\/p>\n<p>&nbsp;<\/p>\n<p>In a flow graph, suppose there is an edge from basic block B1 to B2 as <em>B<\/em>1\u00ae<em>B<\/em>2, then <em>B<\/em>1 is a <em>predecessor <\/em>of<em> B<\/em>2 and<em> B<\/em>2 is a<em> successor <\/em>of<em> B<\/em>1<\/p>\n<p>&nbsp;<\/p>\n<p>In figure 28.3, we have grouped the statements into basic blocks 1, 2, 3 and the manner to do it is given algorithm 28.1<\/p>\n<p>&nbsp;<\/p>\n<p>Algorithm 28.1 Basic block construction<\/p>\n<p>&nbsp;<\/p>\n<p><em>Input<\/em>:\u00a0 A sequence of three-address statements<\/p>\n<\/div>\n<div>\n<p><em>Output<\/em>:\u00a0 \u00a0A\u00a0 \u00a0 \u00a0list\u00a0 of\u00a0 basic\u00a0 blocks\u00a0 with each<\/p>\n<p>three-address statement<span style=\"text-align: initial;font-size: 1em\">in\u00a0 exactly one block<\/span><\/p>\n<\/div>\n<div>\n<p>1.\u00a0\u00a0\u00a0\u00a0\u00a0 Determine the set of <em>leaders<\/em>, the first statements if basic blocks<\/p>\n<p>a)\u00a0\u00a0\u00a0\u00a0\u00a0 The first statement is the leader<\/p>\n<p>b)\u00a0\u00a0\u00a0\u00a0\u00a0 Any statement that is the target of a goto is a leader<\/p>\n<p>c)\u00a0\u00a0\u00a0\u00a0\u00a0 Any statement that immediately follows a goto is a leader<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>2.\u00a0\u00a0 For\u00a0\u00a0\u00a0 each\u00a0\u00a0\u00a0 leader, statements\u00a0\u00a0\u00a0 up\u00a0\u00a0\u00a0 to\u00a0 its\u00a0 but\u00a0 basic not\u00a0 block including\u00a0 consist the\u00a0 of\u00a0 next\u00a0 the\u00a0 \u00a0leader leader\u00a0\u00a0\u00a0 or\u00a0 and\u00a0 the all end\u00a0\u00a0<span style=\"font-size: 1em;text-align: initial\">of the program<\/span><\/p>\n<\/div>\n<div>\n<p>From algorithm 28.1, it is evident that the first step is to identify the set of leaders and group the statements between a pair of leaders into a basic block. An example of this is given in the next module<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong>: In this module, we understood the need for code generation and the various issues that need to be handled in code generation. Basic block and algorithm for basic block creation from three address \/ final code was discussed.<\/p>\n","protected":false},"author":4,"menu_order":28,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-rajeswari-sridhar"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-241","chapter","type-chapter","status-publish","hentry","contributor-dr-rajeswari-sridhar"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters\/241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":2,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters\/241\/revisions"}],"predecessor-version":[{"id":249,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters\/241\/revisions\/249"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters\/241\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/media?parent=241"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapter-type?post=241"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/contributor?post=241"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/license?post=241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}