Appearance
| 1 | @@IF.dotnet() | |
| 2 | ||
| 3 | namespace IR.Emitter is | |
| 4 | use System.Reflection.Metadata.ILOpCode | |
| 5 | ||
| 6 | use IR.Values.Value | |
| 7 | use IR.Values.Visitor | |
| 8 | ||
| 9 | // A value only the CIL lowering makes, which writes its own | |
| 10 | // instructions: an instruction, label or branch that structured | |
| 11 | // control flow is lowered to, or the boundary of a protected region. | |
| 12 | class CilValue: Value abstract is | |
| 13 | init() is | |
| 14 | super.init() | |
| 15 | si | |
| 16 | ||
| 17 | accept(visitor: Visitor) is | |
| 18 | if let lowering: CIL_LOWERING = visitor then | |
| 19 | gen(lowering.context) | |
| 20 | else | |
| 21 | super.accept(visitor) | |
| 22 | fi | |
| 23 | si | |
| 24 | ||
| 25 | gen(context: CIL_CONTEXT) | |
| 26 | si | |
| 27 | ||
| 28 | // A single operand-less IL instruction. | |
| 29 | class INSTRUCTION: CilValue is | |
| 30 | op_code: ILOpCode | |
| 31 | ||
| 32 | init(op_code: ILOpCode) is | |
| 33 | super.init() | |
| 34 | ||
| 35 | self.op_code = op_code | |
| 36 | si | |
| 37 | ||
| 38 | gen(context: IR.Emitter.CIL_CONTEXT) is | |
| 39 | context.current_srm_body_emitter!.op(op_code) | |
| 40 | si | |
| 41 | ||
| 42 | to_string() -> string => "{op_code}" | |
| 43 | si | |
| 44 | ||
| 45 | // Marks the position a branch names. | |
| 46 | class MARK_LABEL: CilValue is | |
| 47 | label: IR.LABEL | |
| 48 | ||
| 49 | init(label: IR.LABEL) is | |
| 50 | super.init() | |
| 51 | ||
| 52 | self.label = label | |
| 53 | si | |
| 54 | ||
| 55 | gen(context: IR.Emitter.CIL_CONTEXT) is | |
| 56 | let body = context.current_srm_body_emitter! | |
| 57 | body.mark_label(label) | |
| 58 | si | |
| 59 | ||
| 60 | to_string() -> string => "{label}:" | |
| 61 | si | |
| 62 | ||
| 63 | // A branch to a label. | |
| 64 | // | |
| 65 | // The long-form opcode is not the final encoding: the encoder | |
| 66 | // narrows it to the short form once the eventual offset is known. | |
| 67 | class BRANCH_TO: CilValue is | |
| 68 | op_code: ILOpCode | |
| 69 | label: IR.LABEL | |
| 70 | ||
| 71 | init(op_code: ILOpCode, label: IR.LABEL) is | |
| 72 | super.init() | |
| 73 | ||
| 74 | self.op_code = op_code | |
| 75 | self.label = label | |
| 76 | si | |
| 77 | ||
| 78 | gen(context: IR.Emitter.CIL_CONTEXT) is | |
| 79 | context.current_srm_body_emitter!.branch(op_code, label) | |
| 80 | si | |
| 81 | ||
| 82 | to_string() -> string => "{op_code} {label}" | |
| 83 | si | |
| 84 | si |