 Every day, we have about eight to nine books. We have four evenings, and we missed two classes. So we're going to have evening extra classes, one on tomorrow evening. Out of the Wednesdays, there was a mail bar. How many of you are registered for MBA 6.0? And what are the? There is a class instruction, but it's the last time, so it was 6.0. So please ask the instructor to leave you at time, so that you can start there. We'll have a class from 6.0, so that you get a 6.0. All right, so let's start looking at now, what do we do for generation? Boolean expressions. So far, what we have seen are arithmetic expressions, and we have also seen control flow and decision making statements. Now Boolean expressions typically are going to be of two kinds. One that will involve some operator like n or m0. And then we'll have the relational operations. So typically, let's say something like a less than c. Now, when you want to do more generation, if I have a Boolean expression, there are two ways of doing more generation. Of course, I mean there are two ways Boolean expressions are used. One is that I have to evaluate the value of a Boolean expression and run through a conclusion whether this is true or false. But after this evaluation, normally what is it that I'm going to do? Either I'm going to assign this value to a variable, so I may have some situation like saying x is a fine a less than b. And what I need to explicitly therefore do in this case is that I need to find out what is this value and assign this value to x. Another situation maybe. Where I say that I want to evaluate this Boolean expression, but I don't have a variable to assign to it. So I may have a situation like saying if a less than b, then I just want to change the rule of flow. So this is another way, another use of a Boolean expression. So what we are going to do are two things. One, we'll see how to do this kind of evaluation and how to do this kind of evaluation. And second and more important thing is that we also want to see that sometimes, for example, when we are either doing in this type or this type, if I can do a partial evaluation. So take a situation like this. If I have something like a or b. Now to find out value of whether this is true or false, I don't have to evaluate the Boolean expression if I have to evaluate the form. I have to write it as soon as I know that value of a is 2, then irrespective of what is the value of b, I can come to a conclusion that the value of b is 2. So we also do complete evaluation as well as partial evaluation. So we have to see both these types of evaluation and we also have to see that how to compute a value and how to assign a value. Now one way of doing this is that I keep all my Boolean expressions, Boolean operators as a reference operators. So when I say that let's go back to what we did for our reference operators when I had x plus y plus z. What are the kind of clear dress code I generated for this? The dress code was of the form where I said t1 is x plus y and t2 is t1 plus z. And then t1 and t2 were taking the values of these arithmetic expressions. Similarly now I can say that instead of these arithmetic operators I have Boolean operators. So suppose I say this is not addition but this is let's say off. The only thing that will happen is that t1 and t2 will take values to a false. So for this discussion we will treat either this will take value to a false or we will say that this will take value 0 or 1. Now depending on the language if you have a C which says any non-zero value could be a true value and 0 value is the false value but some languages will put a restriction and they will say that either have this keyword like to a false or we will say only 0 and 1 and so on. So really that does not affect our discussion so they will change it for that. So this is really the overall theme of code generation for Boolean expressions. Now before I go into details of looking at the syntax translation looking at how do we write dramas are there any general comments any questions about this? If not then we will start going into details. It is conceptually clear what we are trying to achieve. We are now trying to do code generation for Boolean expressions which will involve these operators and which will be low of these operators and will use for either assigning a value or for changing the flow of a control and we will also be we will have to handle both the situations where we say that I want to have a complete evaluation of the Boolean expression or a partial evaluation of the Boolean. So let us look at this. So we are trying to compute logical values and we are trying to change whatever Boolean operators are and or and not and typically I may have a grammar rule which is of this form which says E goes to E or E or so this is the grammar for Boolean expressions which says either I have a non-operator end operator or a unity operator which is non-operator and then I may have bracketed expression or I may have something like saying that ideal of height and I may have this value being assigned to E which are either true or false so in this case I do not have ID but I will just say that this takes values which are true or false. So what are the matters of translation? Either I am going to do it similar to arithmetic expressions which will give me value either 1 or 0 or I will implement flow of control where I will say that I will do only partial evaluation at the end of this depending upon whether I have an assignment or I have a change of flow of control I will do that. So let's look at the first numerical computation and start with an example and then go back and see how do I implement this in extra-active translation session. So suppose I have an expression of this form which says A of B and not C I was to write 3 and just vote for this this is what I am going to do let me apply this unity operator and start with the result T1 and then I say T2 is B and T1 and T3 is A of T2 so T3 finally you will have this section. Now same I mean we know that what was the existence and associative things. So unity operator is the highest existence and not is going to take existence over and is going to take existence over and see the technical moves for this. So similarly if I want to say A less than B then what is the equivalent 3 and just vote for this so suppose I was to generate so you have seen this right this is clear now suppose this is what my grammar is this is what my Boolean expressions and what is the 3 and just vote for A less than B in exactly the same manner I am going on numerical representation 0 or 1 that is the only thing you can how will I translate this one also if you can go ok so all I need to do is I will now first have to check if A is less than B then what happens then what should be the value of this one right so then I can say just remember we create this code I can have a then statement right so I can just say go to some label L1 and L1 will say that P1 is assigned 1 and if this is false then I can just call 2 and then I say P1 is assigned 0 and then I need to do something more after this assignment I need to make sure that I jump over this statement so that this does not get anything once again ok so I just say now go to exit and my exit is here then have I achieved what was the desired result and is this 3 address code so this is what we do ok that what we are saying is this is equivalent to saying if A is less than B then value is 1 otherwise value is 0 and if I translate this this is how you translate that if A is less than B then go to 4 now 4 is really nothing like an absolute label but saying that this is a label which is relevant to or which is at a relative distance from this statement so now I can see that does not matter what is the address of this it always has to jump to whatever is the current address plus 3 right so if I replace this 4 by saying that if A less than B then go to current address plus 3 then I say P is assigned 0 then I say go to current address plus 2 and then I say 1 then I am done there is a kind of 3 address code I think then ok so this is exactly what we are going to do now taking the grammar and writing the rules for 3 address code generation now we already know how do we generate these templates there are no labels which are required as far as this is concerned but here we will require labels but labels I am going to say are just an offset form whatever is my current Pp value ok so I generate templates how do I generate templates I have a function called new so whenever I want a new template name I am just going to make a function for that so when I say that I have this Boolean expression e1 or e2 ok what will I do here ok again I will use the same synthesize that you do saying that e1 value is available in a place e2 value is available in a place and e value will be available in a place so this is first thing I will do that I will say e place is new template location ok and then I am going to emit the code sequence if you say that e place is a sign e1 place of e2 place ok and e code I am not writing explicitly but basically that is going to be e1 code followed by e2 code followed by this particular instruction so if I omitted everything then I am going to accumulate it so both the styles are used and I am going to accumulate it in a variable which is a synthesize that you do or I can keep on having it what happens in this case what is the kind of code sequence I will generate for this exactly the same one except that this operator will get replaced by the end of it so this is the kind of code sequence I will generate for this it is about not e or not e1 what is the kind of code sequence I will generate for this so again I need a new temp for e so e place will be new temp and what will I generate here I will say e place is not of e1 place so this is the kind of code sequence I am going to generate here so you can see that what I am computing now is instead of value of an expression I am computing 0's and 1's so I am still not going partial evaluation or short circuit evaluation I am doing complete evaluation but this is what is going to be the value so let us move on and if I have practical expression then I don't have to do anything in the previous case when I was dealing with arithmetic expressions I just say e place is e1 place I just need to carry that way and now if I have id1 develop id2 then what is the code sequence I generate exactly what we just discussed here we will say that e place is now going to be new temp and then I will say that I am first going to admit that if I have id2 place then I am going to go to whatever is my location plus 3 and then I am going to say that e place is assigned 0 and then I will say go to whatever is the current location plus 2 and then e place is 1 and then that's it and if this is true then what is the code sequence I generate for this so I just need to say e place is new temp it is going to be assigned 1 and if this is false then it is going to be assigned 0 so that is the best way so now we know how to generate code for Boolean expressions at least I can arrive to a conclusion saying that given a Boolean expression which either has all these Boolean operators or a relational operation I can find out whether the value of that Boolean expression is going to be 0 or 1 that is how my final code is made now code for this particular Boolean expression where I want to find out what is the value of a less than b or c less than b and e less than f and let's say that I want to assign it to a variable t final value so what did I do in this case exactly same thing that the way to look at this is that you have an ideal of id here you have an ideal of id here which gets reduced to e and then this is e and e which gets reduced to e and then this is e of e this is how the class 3 for this particular Boolean expression is going to look so first I will say that let's generate code for a less than b and then slow the value of that in a variable for t 1 so I can just find code like this and say that if a less than b then go to 100 so I am assuming that my code generation starts from location 100 so if I have a set of 3 I can jump here and say t 1 is 1 or if it falls through then t 1 is 0 and then I jump here okay so at end of these 4 instructions t 1 is going to contain value 0 or 1 depending upon whether this is true or false okay similarly I can now write code sequence corresponding to c less than b and that is going to be mean this particular instruction is starting from address 100 to 4 I will have an offset of 3 and so on and the value of t 2 is going to be the same now I will have to go similarly I can write code for e less than f so you can see that these are exactly the same schemas the only thing that is changing now is that addresses here I started with 100 here I am starting with 104 and here I am starting with 108 and values are going to be t 1, t 2 and t 3 now once I know that this is t 1, t 2 and t 3 I go now by presidences that first I have to evaluate this particular Boolean expression and then this so that is what exactly I do I say now t 4 is t 2 and t 3 and t 5 is going to be t 1 or t 2 okay and how did I get these t 4 and t 5 they are coming from all to new time clear any confusion about this any problem any question anything because now it is going to get little more complex so far we have been using only synthesized attributes and the style was exactly same as if we are evaluating the other kind of expression okay now I have to come to the short circuit evaluation okay so what we want to do in short circuit evaluation is we do not want to evaluate the full Boolean expression and we just want to evaluate part of the Boolean expression and I want to come to a conclusion saying that as soon as I know for sure what is going to be the value of the Boolean expression and my evaluation is going to be from left to right okay so I will not randomly pick up some phrases or some sub terms within the Boolean expression and say that I am first going to evaluate that okay but I will say that let me start evaluating from left to right so if I say I am evaluating a less than b or t less than b and t less than f okay what I want to do now is I do not want to now say that this will have value of t1 this will have value of t2 and this will be the value p3 and then do n and all on that okay but I will say suppose I evaluate this and this evaluates to 2 then I know irrespective of what is c less than b and t less than f okay the whole expression is going to be now if I know that for sure then not only I will jump over this part of the code I will straight away go to a location saying that if this is true then I should be able to reach some level which will be saying that t is assigned 1 okay and if this is false then what do I do then I should jump to this okay and evaluate it right this is the only thing I need to do okay so this is exactly what we do in short circuit evaluation so now for argument sake and just to make this question simple let's assume that now I have inherited attributes also I am not just dealing with synthesized attributes but I am dealing now with inherited attributes an inherited attribute is telling me that where do I jump after finishing after evaluating this code of Boolean expression okay so that is the attribute I will have and we are now going to translate Boolean expressions without generating code for Boolean operators and without evaluating the entire expression some language will allow you to do that in fact some languages want you to do that okay so first thing we want to do is we want to look at flow of control so we look at flow of control before getting into the Boolean expression and there we said that I am going to have labels like jumping to certain locations okay so remember that when I had this flow of control I also introduced the function called which was generating labels for me right so I had this function called new label which generated and then if I had this if E then S and this kind of statement then what did I do then I said that I am going to generate label if this evaluates to 2 where do I go then I was actually evaluating this I was falling through and if this was evaluating to false then I was generating a label which was beginning of this statement okay now I remember there was some discussion about saying is it possible that in this particular scheme I ended up generating multiple labels right so we had a situation where suppose this statement was followed by a wild statement where I was generating a label for beginning of wild and I was generating a label for next of S okay now slowly you will see that I am trying to address that situation also by having inherited attributes okay so for the scheme let's assume that we have an attribute which says that when I start parsing this I know where the control will go S next is available to me okay now once I say that S next is available to me that is inherited then if I look at this okay so let's look at this scheme I am trying to understand what will happen there okay so now suppose I say if E then S1 okay what will happen here I will first go into evaluate E and then I am going to have statement corresponding to S1 right now if I evaluate this and I know where S next is okay let's say that S next is known to me I have inherited this okay now suppose this evaluates to true where will control go so I have two exits from this I have E true and I have E false right it can either evaluate to true or false now I don't have a concept of a variable having value 0 over 1 I am only doing control for evaluation okay so where will control go if this is true here right control will come here I don't have this label okay so this label has to be generated but where will control go if it evaluates to false here right so now I can straight away say that this is so the way I start writing schema for this is so let's look at this schema now that we have code for E which is giving me two exits either on true or false true control comes here and the false control comes here okay so how will my code look okay now I will say that E2 is a new label because this label I did not have a new label function but E false is that's next which has already been initialized because this is an inherently active okay and now how will my code of hassle how will I write my S code there because if you just understand this okay that's because things will neatly fall in place so what will be my S code now so obviously first thing is going to be E code right I will have to evaluate E code okay and then what do I do what do I check there I don't have a variable which stores value of this because we are not doing that right we only want to change flow of control this is not effective like evaluation that's why I have to think now slightly differently then say check it for value of a machine and that's why I am using inherently active so after E code what will let me just show you the code and then sort of argue with you that this is correct okay and the code is as simple as this okay so now I say that this is E code followed by this label followed by S1 code that's the only thing does it make sense what is happening here so how is change of control taking place more relevant okay that I also have initialized the variable which does not have to be anywhere which is S1 max so you have to understand these two things so this initialization was simple because all I am saying is this exit jumping here and exiting from here but this initialization and this piece of code seems to be slightly tricky okay so let's first come to this initialization why do I need this initialization anyone any guesses idea there see S1 may again have a Boolean expression I don't know S1 is just a statement which will be unraveled only when I start asking that right so S1 inside suppose I have Boolean expression inside S1 then I need to know what is the location where I should jump after finishing S1 so since I am now using inherited active groups I am saying I know S next so obviously if I know S next S1 next also has to be initialized okay so that is what is being initialized here because I mean otherwise this question would have come how do I know S next at some point of time this S must have been part of a larger statement so if I initialize it there then I must initialize it here right and now what happens in this code now when I say E code what is E code here the jump statement will be part of this E code is now not saying that take A or B and store value into some variable P and then find out whether this value is true or false E code will just be saying that if this value is true jump to this location if this value is false jump to another location and so on okay so all the jump statements are actually part of E code and then I just need to generate this label and I need to generate code for S1 okay do I need to generate this label do I need to generate S next do I need to have a line which will say gen X next because again this label must have been generated where this whole S was being embedded in some statement this S must have occurred on the right hand side okay and that is where this label must have been generated like here I have just generated this S1 next initialize it not clear what is the code for so what is the code for I am not sure the code for it okay so let's look at code for it suppose I write something like this this is not my Julian expression okay so E is nothing but now this is a grammar tool which is passing my Julian expression right now earlier what did I do earlier I was generating 4 instructions for this saying that you first evaluate this and if this value is true and so on okay now I need to generate only short code all I need to say is if id is a lot id let's say id1 and 2 okay and if E is getting reduced by this rule then what is the code I need to write go to it good E is evaluating to false then I can go to E false right how can you create a Julian expression you either need base values or you need relational operation and once you have done this then what will happen if I say then what is the kind of code I write for this so what is the scheme of for doing this E1 or E2 so first I evaluate E1 and if this is true then what do I do I jump to E2 right and if this evaluates to false then what do I do then I need to generate a label which will take me here and then I need to evaluate so I don't have any variable being assigned 0 or 1 I am only jumping around jumping from one label to another till finally I reach the target so now in light of this does it make sense saying that what is the kind of code we will now do if I am doing short circuit evaluation see that short circuit evaluation always is going to be much shorter as compared to the complete evaluation where I need to assign value to 0 and so let me go to the next part and then if it is still not clear we come back to the base case so the next one is now if E goes to if E then S else S2 this is really what I am looking at now E then S1 else S2 so this is how the schema looks okay that I will have E code which will have 2 exits either 2 or false and if this is true then S1 code will get executed but after S1 I need to jump to S next and if this evaluates to false then I will jump to S2 okay and now what are the things I need to do I need to first generate for E and how do I initialize these labels so what is E2 being assigned how will I initialize E2 E2 is jumping here right which is in middle of this schema so that has to be from a new label it's not available to me I just have to generate this okay what about E false E false also has to be then initialize to the new label okay what about S1 next there will control go from S1 after it finishes control must go here and from S2 also control must go here okay so S1 next and S2 next both are being initialized to S next so this is the initialization of all the labels and how will my code look now so after I have initialized so I have this label this label being initialized this is inherited and S1 and S2 next up then I initialize to whatever I inherited here so how will my S code look S code will be first E4 now I don't have to worry about jumps because that is going to be further embedded in the E4 itself and then I will say I need to generate this label then I need to generate code for S1 then I need to generate this statement and then I need to generate this label and then I need to generate code for S1 if E is an actual assignment E is an actual assignment E is not an actual assignment so what you are saying is something like this is this what you are saying so what will I do in this case so look at it this way that this is still E and this statement is going from ID being assigned here to the actual assignment all I need to say is that do exactly the same thing what we are doing here and then I have two labels which will say E2 and E false and then E2 I will say X is sign 1 and we will say X is sign 0 right good question so what happens when you have side effects who is responsible for that if there are side effects you are not saying A less than B I have these as function calls right so suppose I have two functions F1 and F2 which return the Boolean values and I get this one right this is what your question is who is responsible for this now if I am doing exactly left to right evaluation with change of values of A and B suppose I say that I am going to evaluate this first this has a side effect which impacts this value okay who is responsible for this is this programming language of the compiler so if language permits you this facility so look at it this way language permits the facility then to make sure that one is not misusing it is job of the programmer but if suppose the programmer decides to write something like this then compiler has no control over it worst which can happen is that many times it is not clear it is not defined that when I say if A less than B is it the B I fetch first or A I fetch first so if I say that I am going to fetch this first evaluate this first some compilers will do that and some compilers will do that because language is not defined that if F1A is less than F2B which function should be evaluated first so two different compilers can give you a question answer so let's go to the next schema okay which is I have to write the right hand side code for this just take one minute to write it down write the complete code all initialization that will immediately tell you whether you have understood it then it will take more than a minute so you can see that I need to initialize both E2 and E false okay and what else I need to initialize has to begin so has to begin has to be now initialize by a function or new limit so this is what will happen how will my code look code is going to be E code followed by how about this one remember that since we are dealing with inherited attribute always whenever you have a statement but it is possible that I will never lose S1 next I will not have a jump statement but you still need to have that variable because we don't know and S code is now going to be that okay so I need to first generate this label then I need to generate this code I don't have to worry about generating jump when false that in the embedded E code then I need to generate this label then S1 code then I need to generate a jump statement right so a schema actually tells you systematically what all you have to generate so I may say it's still be straight forward just think of how it is going to be executed at one time right there down and then you can easily translate it into code because all you are doing is you are capturing the semantics of the statement I will generate this label then I will have code for E then I will generate this label then I will have code for S1 and then I will generate this jump statement here so let's look at now going back to the question which was asked earlier that how will code for E look so code for now if I say that I want to generate this code now I know that so remember that 2 and false before I came here so if I go back here 2 and false have already been initialized either by an inherited attribute or by the function false so when I start now generating this code I know that what is E2 and what will be false these are just some target locations so what will be E1 true E1 true is going to be E2 where control goes and what will be E1 false where will E1 false go E2 true has not been initialized yet it has to go to the beginning of E2 that has to be a new label so E1 false will jump to this location which is big of E2 and what is E2 true E2 true obviously is E2 and what is E2 false E false very good and what will be the code now code for E so let's try what E4 will be E1 false followed by I need to generate this label which is E1 false followed by code that is how much total code is going to now can I do the same thing for AND operation here instead of OR what will happen in case of AND if I do left to right evaluation then what will happen if I have AND here then where will E2 jump if I say E1 is valid into 2 where should control go so what will that be that will be a new label and what will be E1 false E1 false is going to be E false and if E2 evaluates to 2 then the whole thing is true because control would have come only if this was true and E2 false is E false and my code is same except that this label is now the number okay what about this now in this case I don't have to do anything I just need to switch labels right so if I know labels of E1 okay then I will say that E1 true is where E false is jumping and E1 false is where E2 is okay so I just need to copy the labels around okay and E4 is nothing but E1 true I am not editing any labels and then bracketed expression I will just say label is copied into true and false label is copied into false okay and code remains again the same right and what about this this we already discussed if E is of this form if A less than B then what is the code form code form is going to be that if A less than B then go to E2 otherwise false will go to E false okay so this is the kind of code sequence I will have okay so when I now say code sequence I did it I say if id colon then go to E2 followed by go to E false okay and if this is true then all I am saying is I am jumping to true and if this is false then I am jumping to false okay so I know now both the short circuit evaluation as well as arithmetic side of evaluation and in both the cases final target is going to decide what is the value of full expression so in one I will be just changing flow of control in another case I will be assigning the final value okay so if we have this kind of expression being assigned then finally I will jump to E2 and E false label and assign a temporary value or assign value to a temporary equation okay so there is an example same example okay if I was to write short circuit evaluation code for this so earlier how many instructions did I have for each subphase I generated 4 instructions so I had 12 and then I had one for this one for this and one for this and one final assignment so total of well plus 3 instructions okay how will it look now I will say that if a less than b then I will say jump to L2 where this whole thing is and if otherwise I will just say go to L1 but what will be L1 here L1 will be at this point of time I will not even try to optimize all I am saying is that for a less than b this is the two sequences I generated now it is possible that I have a redundant jump here and we see method of optimization where we try to remove redundant jumps but at this point of time we will not worry about that so if I now say that for c less than d what is I generated I say that if c less than d then go to L2 because this is an n clause I must jump to beginning of this and if this is false then I know for sure that the whole thing is false and I will jump to L2 okay and then what will happen what is my L2 here L2 is label of T less than f and I will say that if T less than f then jump to L2 because now I know that whole thing is true and if this is false then I know that this is L false okay and now you can see that L2 and L false are two labels depending upon whether I have an assignment there okay if I have an assignment there if I say X is being assigned this all I will do is I will say X is being initialized to 1 and X being initialized to 0 okay otherwise I will execute the two statement here if I say this is a Boolean condition and I say if this condition then S1 then I will have S1 here and otherwise I will have S2 here so everyone is confident about Boolean expressions okay so now you can see that we started with control flow we did not talk about Boolean expression at that point for 5 I think it might Boolean expressions as black boxes I just said that I am able to generate e-code something but I was able to generate 3 less code form that is for the arithmetic operations and then we went into the Boolean expressions and saw two methods and then we just exploded the whole thing and now we can see that how to generate Boolean expressions so what do I know by now so I know how to generate code for Boolean expressions I know how to generate code for arrays I know how to generate code for Boolean expressions I have to generate code for all the control flow statements and decision statements you can see that if you do not have procedures in your language then you can actually generate now code which will work 3 address code of course but it will work I am trying to sync it with your project because you are in now 4th generation phase you can see how to generate code for functions in procedures but at least if you have a single function of procedure then you are able to type checking and you are able to generate 3 address code for this and then mapping 3 address code on to string code or whatever is the target machine is not really the difficult one and as I said at this point of time I am not even worried about optimization here so even if there are redundant jumps like saying go to L1 and L1 is just the next statement I can just throw it out and so on but we will not worry about that we will live with unoptimized code but we will do that so let's move forward I have one more example here just we take this point and then we break here so I will just take a mean and then we will break so if I have a statement like this which says while a less than b then I have this if statement inside this and if c less than b then I am doing this assignment otherwise I am doing this assignment so you can see by partial evaluation of Boolean expression if I say this is true then I am jumping to L2 otherwise I am jumping to exit but in L2 I have this Boolean expression where I say that either x is going to get this value d1 or y is x is going to get this value which is y minus that and in both cases you can see that after I finish I am jumping to the meaning of the widespread so we just need to draw the pass three for this and what we do is we break here today and tomorrow's class, tomorrow evening class then we take the next one