------------------Jan3.01-------------------- *1 Succeed to optimize the expression. It took me hours to find out a bug for the optimizing. The VM we are given is a stack-based one, it is convinent to use the triple-format of inter-code. I am not going to add the duplicated expression reusing or DAG optimization, since it is a nightmare to do that on a stack-base machine--at least for me it is. 2 Finish optimization for array element access. This part is tough. The load and store of array element have to be treated seperatly, since it is related with the address calculation. During the element address calculation, the top of stack is changing. I spent long time observing this before I could make it explicit. 3 Add short-circuit calculation for bool type. 4 Merge the number in expression. To do this, I add a new attribute for intercode:type. It is NOT for the data type. It is used to distinguish different type of operator in intercode. '2' for binary; '1' for monadic;'0' for number or variable. 5 Add simple algebraic elimination for expressions like these: x+0, x-0, 1*x, x*1, x*0, 0*x, 0/x, x/1. This can bring up some speed for some test sample(eg."a-b+2*c(1*2*1*1*1*1*1)" in proc2.pl0). 6 Add divide by 0 check. 7 Add two operators: '%' for mode AND '^' for power. I planned to add self-increase and self-decrease, but when I tried, I found that it is more complecated. Because I have to ensure the operand is a variable but not a const or number. So I drop this plan and only use seft-increase and self-decrease instruction for optimization. 8 Add target code optimization. I add several optimization checking for the P-code. The function "optimize_target()" will scan the generated P-code and optimize the following types of codes: a. STO following LOD(in PL/0: x:=x;) b. self-increase of self-decrease by 1(x:=x+1 turned to X++) So far only optimize two, it is tough to optimize these P-code. I would rather consider them as target code than intermediate code. I only scan the code for one round. The removed code will be replaced by "NOP,0,0", it means do nothing. Of course, All the NOP can be removed at the end, the address for branch in the code following should be updated. It's a little trouble, I don't do that. ----------------------Jan2.02--------------- *1 introduce the intermediatecode, since the 'type' property for express and term and factor conflict with the property of 'ps'(position), it was temporally removed. When the type-checking actions removed, '.y' becomes much tidy. Yeah, type-checking makes thing messy, if it is placed in the '.y'. the new inter-code generating and interpreting function seem working well. However, by far, they do no more than the old directly interpreting. And it only work for the expression, since there are too many final exams, I seem to have no much time to complete it before the deadline. I will add the optimizing right when I am back from supper. 2 change the char symbol for not-equal, use '!' -------------------Jan2.01-------------------- 1 remove the JPC without pop instruction, since the 'a' of it is already used as branch target. Use a CPY before JPC, instead. 2 change all gendo() into gen(),just for convinience *3 improve the allocate for array, array size is reduced from (endindex+1) to (endinex-beginindex+1). Only store the assigned elements ---------------------Jan1.01------------------------ *1 Add data type. Bool type introduced. Two boolean constant 'true' and 'false' introduced. I also add type checking. The type declaring and method of combining bool and int expression is leart from the EBNF of PASCAL on the wibesite that TA give us. The instruction set is expanded to do the bool calculating. In order to be compatible with the testset TA give us. I reserve the old declaring syntax. As a result, two r-s conflicts introduced. The defalt shift rule is excactly what I want, I use '%expect' to mute it. 2 FOR TO/DOWNTO DO clause introduced. I found this rule on the EBNF for PASCAL, then I tried to add it. It is a little different from the WHILE DO, because it include an interator which value should be updated during every loop. I used subtract to calculate the interactor. In every interation in loop, the value of interactor will be increase or decreased by 1 and STO back into variable stack. In this clause, the interacting times will easily mistaken by 1. -------------------Dec30.01------------------------ 1 Actions for all rules added. I used the global variable to store the entrance of action.(e.g, to mark the current cx for JPC). But when I patched the previous entrance it went wrong. It's because of the global variable(e.g cx0) will be changed when entering the next nested level. The PL/0 in textbook use recursive function to pass the level as a parameter. I have to change my design. I first consider using the attribute in ".y" to record and patch. Only there are too many address and other value to record, I then used array. Different level only change the the record by it's index. 2 Correct the line counting bug. In hw2, I extend the comment of PL/0 into multiple lines. The line was not counted in comment. I correct this. -------------------Dec27.01------------------------ 1 Removed most of the error detecting and recovering. Although it is what I am proud of, I removed them. Because they will arouse lots of conflicts when action added. 2 Finish the syntax checking. By far, all the conflicts are removed. -------------------Dec25.01------------------------ *1 Finish the basic framework. I decide to base on my hw2, since I am more familiar with it. And I am satisfied with the error dectecting and recovering function. I analyzed the source of PL/0 written in C from the appendix of our textbook, and mark the action entrance and exit.