Appearance
| 1 | namespace IR is | |
| 2 | use IO.Std; | |
| 3 | ||
| 4 | use Values; | |
| 5 | ||
| 6 | class LABEL is | |
| 7 | _current_pass: string static; | |
| 8 | _next_id: int static; | |
| 9 | ||
| 10 | pass: string; | |
| 11 | id: int; | |
| 12 | ||
| 13 | init() is | |
| 14 | pass = _current_pass; | |
| 15 | id = _next_id; | |
| 16 | ||
| 17 | _next_id = _next_id + 1; | |
| 18 | si | |
| 19 | ||
| 20 | set_pass(p: string) static is | |
| 21 | _current_pass = p; | |
| 22 | si | |
| 23 | ||
| 24 | reset_id() static is | |
| 25 | _next_id = 0; | |
| 26 | si | |
| 27 | ||
| 28 | to_string() -> string => "L{pass}{id}"; | |
| 29 | si | |
| 30 | ||
| 31 | enum COMPARE is | |
| 32 | NONE, | |
| 33 | EQ, | |
| 34 | GE, | |
| 35 | GEU, | |
| 36 | GT, | |
| 37 | GTU, | |
| 38 | LE, | |
| 39 | LEU, | |
| 40 | LT, | |
| 41 | LTU, | |
| 42 | NE, | |
| 43 | Z, | |
| 44 | NZ | |
| 45 | si | |
| 46 | ||
| 47 | enum BRANCH is | |
| 48 | NONE, | |
| 49 | EQ, | |
| 50 | GE, | |
| 51 | GEU, | |
| 52 | GT, | |
| 53 | GTU, | |
| 54 | LE, | |
| 55 | LEU, | |
| 56 | LT, | |
| 57 | LTU, | |
| 58 | NE, | |
| 59 | Z, | |
| 60 | NZ, | |
| 61 | ALWAYS | |
| 62 | si | |
| 63 | ||
| 64 | class BRANCHER is | |
| 65 | _branch_instructions: Collections.MAP[BRANCH, string]; | |
| 66 | _compare_instructions: Collections.MAP[COMPARE, string]; | |
| 67 | ||
| 68 | init() is | |
| 69 | _branch_instructions = Collections.MAP[BRANCH, string](); | |
| 70 | _compare_instructions = Collections.MAP[COMPARE, string](); | |
| 71 | ||
| 72 | _branch_instructions[BRANCH.EQ] = "beq"; | |
| 73 | _branch_instructions[BRANCH.GE] = "bge"; | |
| 74 | _branch_instructions[BRANCH.GEU] = "bge.un"; | |
| 75 | _branch_instructions[BRANCH.GT] = "bgt"; | |
| 76 | _branch_instructions[BRANCH.GTU] = "bgt.un"; | |
| 77 | _branch_instructions[BRANCH.LE] = "ble"; | |
| 78 | _branch_instructions[BRANCH.LEU] = "ble.un"; | |
| 79 | _branch_instructions[BRANCH.LT] = "blt"; | |
| 80 | _branch_instructions[BRANCH.LTU] = "blt.un"; | |
| 81 | _branch_instructions[BRANCH.NE] = "bne.un"; | |
| 82 | _branch_instructions[BRANCH.Z] = "brfalse"; | |
| 83 | _branch_instructions[BRANCH.NZ] = "brtrue"; | |
| 84 | _branch_instructions[BRANCH.ALWAYS] = "br"; | |
| 85 | ||
| 86 | _compare_instructions[COMPARE.EQ] = "ceq"; | |
| 87 | _compare_instructions[COMPARE.GE] = "cge"; | |
| 88 | _compare_instructions[COMPARE.GEU] = "cge.un"; | |
| 89 | _compare_instructions[COMPARE.GT] = "cgt"; | |
| 90 | _compare_instructions[COMPARE.GTU] = "cgt.un"; | |
| 91 | _compare_instructions[COMPARE.LE] = "cle"; | |
| 92 | _compare_instructions[COMPARE.LEU] = "cle.un"; | |
| 93 | _compare_instructions[COMPARE.LT] = "clt"; | |
| 94 | _compare_instructions[COMPARE.LTU] = "clt.un"; | |
| 95 | _compare_instructions[COMPARE.NE] = "cne.un"; | |
| 96 | _compare_instructions[COMPARE.Z] = "ctrue"; | |
| 97 | _compare_instructions[COMPARE.NZ] = "cfalse"; | |
| 98 | si | |
| 99 | ||
| 100 | get_for(block: BLOCK) -> BLOCK_BRANCHER => | |
| 101 | BLOCK_BRANCHER(block, self); | |
| 102 | ||
| 103 | label(block: BLOCK, l: LABEL, comment: string) is | |
| 104 | block.add("{l}:"); | |
| 105 | si | |
| 106 | ||
| 107 | label(block: BLOCK, l: LABEL) is | |
| 108 | block.add("{l}:"); | |
| 109 | si | |
| 110 | ||
| 111 | leave(block: BLOCK, l: LABEL) is | |
| 112 | block.add("leave {l}"); | |
| 113 | si | |
| 114 | ||
| 115 | branch(block: BLOCK, b: BRANCH, value: Value, l: LABEL, comment: string) is | |
| 116 | block.add(value); | |
| 117 | ||
| 118 | block.add("{get_branch_instruction(b)} {l}"); | |
| 119 | si | |
| 120 | ||
| 121 | branch(block: BLOCK, b: BRANCH, value: Value, l: LABEL) is | |
| 122 | block.add(value); | |
| 123 | ||
| 124 | block.add("{get_branch_instruction(b)} {l}"); | |
| 125 | si | |
| 126 | ||
| 127 | branch(block: BLOCK, b: BRANCH, left: Value, right: Value, l: LABEL, comment: string) is | |
| 128 | block.add(left); | |
| 129 | block.add(right); | |
| 130 | ||
| 131 | block.add("{get_branch_instruction(b)} {l}"); | |
| 132 | si | |
| 133 | ||
| 134 | branch(block: BLOCK, b: BRANCH, left: Value, right: Value, l: LABEL) is | |
| 135 | block.add(left); | |
| 136 | block.add(right); | |
| 137 | ||
| 138 | block.add("{get_branch_instruction(b)} {l}"); | |
| 139 | si | |
| 140 | ||
| 141 | branch(block: BLOCK, l: LABEL, comment: string) is | |
| 142 | block.add("{_branch_instructions[BRANCH.ALWAYS]} {l}"); | |
| 143 | si | |
| 144 | ||
| 145 | branch(block: BLOCK, l: LABEL) is | |
| 146 | block.add("{_branch_instructions[BRANCH.ALWAYS]} {l}"); | |
| 147 | si | |
| 148 | ||
| 149 | get_branch_instruction(b: BRANCH) -> string => | |
| 150 | _branch_instructions[b]; | |
| 151 | si | |
| 152 | ||
| 153 | struct BLOCK_BRANCHER(_block: BLOCK, _brancher: BRANCHER) is | |
| 154 | label(l: LABEL, comment: string) is | |
| 155 | _brancher.label(_block, l, comment); | |
| 156 | si | |
| 157 | ||
| 158 | label(l: LABEL) is | |
| 159 | _brancher.label(_block, l); | |
| 160 | si | |
| 161 | ||
| 162 | leave(l: LABEL) is | |
| 163 | _brancher.leave(_block, l); | |
| 164 | si | |
| 165 | ||
| 166 | branch(b: BRANCH, value: Value, l: LABEL, comment: string) is | |
| 167 | _brancher.branch(_block, b, value, l, comment); | |
| 168 | si | |
| 169 | ||
| 170 | branch(b: BRANCH, value: Value, l: LABEL) is | |
| 171 | _brancher.branch(_block, b, value, l); | |
| 172 | si | |
| 173 | ||
| 174 | branch(b: BRANCH, left: Value, right: Value, l: LABEL, comment: string) is | |
| 175 | _brancher.branch(_block, b, left, right, l, comment); | |
| 176 | si | |
| 177 | ||
| 178 | branch(b: BRANCH, left: Value, right: Value, l: LABEL) is | |
| 179 | _brancher.branch(_block, b, left, right, l); | |
| 180 | si | |
| 181 | ||
| 182 | branch(l: LABEL, comment: string) is | |
| 183 | _brancher.branch(_block, l, comment); | |
| 184 | si | |
| 185 | ||
| 186 | branch(l: LABEL) is | |
| 187 | _brancher.branch(_block, l); | |
| 188 | si | |
| 189 | si | |
| 190 | si |