Skip to content

Latest commit

 

History

History
32 lines (26 loc) · 1.11 KB

2.md

File metadata and controls

32 lines (26 loc) · 1.11 KB

Exercise 2: While0 to URM

Proposal: URM-sym language extension

Introduce a symbol table phase for URM to make "programming" a little less messy:

  1. Map every label <LBL> to the matching line number (output: {<LBL1>: <NO1>, <LBL2>: <NO2>}, ...}).
    • A label is defined as LBL: \<[A-Z0-9_]+\>;
    • Advantage: "Normal" line numbers can still be used with the expression defined above.
  2. Replace evert <LBL> with the actual line number using the output of 1.

Transformation

For the given while V1 != V2 do begin α end a transformation to URM could look as follows:

in(V1, V2);
loc(T1, T2);
<COP1>: COPY(V1, T1);
<COP2>: COPY(V2, T2);
<TEST_T1>: if T1 == 0 goto <TEST_T2>;
<ELSE>: if T2 == 0 goto <WHILEBODY>;
<T1>: T1--;
<T2>: T2--;
<GO_BACK>: goto <TEST_T1>;
<TEST_T2>: if T2 == 0 goto <ABORT>;
<WHILEBODY>: α
<LOOP>: goto <COP1>;
<ABORT>: ...
  • Temp variables T1, T2 are needed because α could have dependent calculations on V1 or V2. That is why we cannot work on them directly.
  • For the same reason described above, V1 and V2 are consequently copied in each iteration/loop.