int ax by x ai bj int a23
Στοιχεία Πίνακα (ΙΙ) Παράδειγμα int a[x], b[y]; …. x = a[i] + b[j]; int a[2][3]; …. x = c + a[i][j] ; 1: mul, i, 4, t 1 1: mul, i, 12, t 1 2: array, a, t 1, t 2 3: mul, j, 4, t 3 4: array, b, t 3, t 4 5: add, t 2, t 4, t 5 6: assign, t 5, -, x 2: mul, j, 4, t 2 3: add, t 1, t 2, t 3 4: array, a, t 3, t 4 5: add, c, t 4, t 5 5: assign, t 5, -, x W 1 = 4 (int) w 2 = 3 × 4 (δεύτερη διάσταση × w 1)
Εντολή if (I) if (expr) stmt if (a+b > c-d) x = a+b; Ενδιάμεσος κώδικας για το expr if, texpr, true, l 1 jump, -, -, l 2 l 1 : … Ενδιάμεσος κώδικας για το stmt l 2 : … 1: add, a, b, t 1 2: sub, c, d, t 2 3: great, t 1, t 2, 6 4: assign, false, -, t 3 5: jump, -, -, 7 6: assign, true, -, t 3 7, if, t 3, true, 9 8: jump, -, -, 11 9: add, a, b, t 4 10: assign, t 4, -, x 11: … expr quads (1 – 6) if quads (7 – 8) stmt quads (9 – 10)
Εντολή if (II) if_stmt: TK_IF ‘(‘ cond ‘)’ { c 1 } stmt { c 2 } c 1: $3. truelist = emit(“if”, $3. place, “true”, L 1); $3. falselist = emit(“jump”, “-”, L 2) backpatch($3. truelist, nextquad()); c 2: backpatch($3. falselist, nextquad());
Εντολή if – else (I) if (expr) stmt 1 else stmt 2 Ενδιάμεσος κώδικας για το expr if, texpr, true, l 1 jump, -, -, l 2 l 1 : … Ενδιάμεσος κώδικας για το stmt 1 jump, -, -, l 3 l 2 : … Ενδιάμεσος κώδικας για το stmt 2 l 3 : … if (x > y) a = x; else a = y; 1: great, x, y, 4 2: assign, false, -, t 1 3: jump, -, -, 5 4: assign, true, -, t 1 5, if, t 1, true, 7 6: jump, -, -, 9 7: assign, a, -, x 8: jump, -, -, 10 9: assign, a, -, y 10: … expr quads (1 – 4) if quads (5 – 6) stmt 1 quad stmt 2 quad
Εντολή if – else (II) if_stmt: TK_IF ‘(‘ cond ‘)’ { c 1 } stmt TK_ELSE {c 2} stmt {c 3} c 1: $3. truelist = emit(“if”, $3. place, “true”, L 1); $3. falselist = emit(“jump”, “-”, L 2) backpatch($3. truelist, nextquad()); c 2: $5. nextlist = emit(“jump”, “-”, L 3 ) backpatch($3. falselist, nextquad()); c 3: backpatch($5. nextlist, nextquad());
Εντολή while (I) while (expr) stmt l 1 : … while (a > b) a = a + 1; Ενδιάμεσος κώδικας για το expr if, texpr, true, l 2 jump, -, -, l 3 l 2 : … Ενδιάμεσος κώδικας για το stmt jump, -, -, l 1 l 3 : … 1: great, a, b, 4 2: assign, false, -, t 1 3: jump, -, -, 5 4: assign, true, -, t 1 5, if, t 1, true, 7 6: jump, -, -, 10 7: add, a, 1, t 2 8: assign, a, t 2 9: jump, -, -, 1 10: … expr quads (1 – 4) while quads (5 – 6, 9) stmt quads (5 – 6)
Εντολή while (II) while_stmt: TK_WHILE { c 1 } ‘(‘ cond ‘)’ { c 2 } stmt { c 3 } c 1: l = nextquad(); c 2: $3. truelist = emit(“if”, $3. place, “true”, L 1); $3. falselist = emit(“jump”, “-”, L 2) backpatch($3. truelist, nextquad()); c 3: emit(“jump”, “-”, l) backpatch($3. falselist, nextquad())
Συναρτήσεις (II) f(a) n = f(a) 1: param, a, V, 2: call, f, 1, - 1: param, a, V, 2: param, t 1, RET, 3: call, f, 1, RET 4: assign, t 1, -, n 1: param, a, V, 2: call, f, 1, t 1 3: assign, t 1, -, n
- Slides: 15