Question: One problem (of many) with the code we generated in Section 15.3 is that it computes at run time the value of expressions that could

One problem (of many) with the code we generated in Section 15.3 is that it computes at run time the value of expressions that could have been computed at compile time. Modify the grammar of Figure 15.6 to perform a simple form of constant folding: whenever both operands of an operator are compile-time constants, we should compute the value at compile time and then generate code that uses the value directly. Be sure to consider how to handle overflow.

Figure 15.6:

reg_names : array [0..k–1] of register_name := [

reg_names : array [0..k1] of register_name := ["r1" "r2" ..., "rk"] --

reg_names : array [0..k1] of register_name := ["r1" "r2" ..., "rk"] -- ordered set of temporaries !! program - stmt stmt.next.free_reg := 0 program.code := ["main:"] + stmt.code + ["goto exit"] while : stmt expr stmtz stmtz expr.next.free_reg := stmt2.next.free_reg := stmt3.next_free_reg := stmt.next.free_reg L1:= new.label(); L2 := newJabel() stmtj.code := ["goto" L1] + [L2 ":"] + stmt2.code + [L1 ":"] + expr.code + ["if" expr.reg "goto" L2] + stmt3.code if : stmti expr stmtz stmt3 stmt4 expr.next.free_reg := stmt2.next_free_reg := stmt3.next_free_reg := stmt4.next_free_reg := stmtj.next-free-reg L1:= new.label(); L2 := newJabel() stmtj.code := expr.code + ["if" expr.reg "goto" L1] + stmt3.code + ["goto" L2] + [L1 ":"] + stmtz.code + [L2 ":"] + stmt.code assign : stmt id expr stmtz expr.next.free_reg := stmt,.next.free. reg := stmtj.next.free.reg stmti.code := expr.code + [id.stpname ":=" expr.reg] + stmt2.code read : stmt + id, idz stmt2 stmtj.code := ["a1 := &" idj.stp-name] + ["call" if id2.stp type = int then "readint" else ..] + [id2.stpname ":= rv"] + stmt2.code -- file write : stmt id expr stmt2 expr.next.free_reg := stmt2.next.free reg := stmtj.next.free.reg stmtj.code := ["a1 := &" id.stpname] + ["a2 :=" expr.reg] + ["call" if id.stp type int then "writeint" else ...] + stmt2.code -- file -- value %3D writeln : stmt id stmtz stmtj.code := ["a1 := &" id.stpname] + ["call writeln"] + stmt2.code null : stmt

Step by Step Solution

3.40 Rating (169 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

ANSWER regnames array 0k1 of registername r1 r2 rk ordered set of temporaries program stmt stmtnextf... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Language Pragmatics Questions!