1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use std::collections::HashMap;

use crate::cdsl::instructions::InstructionPredicate;
use crate::cdsl::recipes::{EncodingRecipeBuilder, EncodingRecipeNumber, Recipes, Stack};
use crate::cdsl::regs::IsaRegs;
use crate::shared::Definitions as SharedDefinitions;

/// An helper to create recipes and use them when defining the RISCV encodings.
pub(crate) struct RecipeGroup {
    /// The actualy list of recipes explicitly created in this file.
    pub recipes: Recipes,

    /// Provides fast lookup from a name to an encoding recipe.
    name_to_recipe: HashMap<String, EncodingRecipeNumber>,
}

impl RecipeGroup {
    fn new() -> Self {
        Self {
            recipes: Recipes::new(),
            name_to_recipe: HashMap::new(),
        }
    }

    fn push(&mut self, builder: EncodingRecipeBuilder) {
        assert!(
            self.name_to_recipe.get(&builder.name).is_none(),
            format!("riscv recipe '{}' created twice", builder.name)
        );
        let name = builder.name.clone();
        let number = self.recipes.push(builder.build());
        self.name_to_recipe.insert(name, number);
    }

    pub fn by_name(&self, name: &str) -> EncodingRecipeNumber {
        *self
            .name_to_recipe
            .get(name)
            .unwrap_or_else(|| panic!("unknown riscv recipe name {}", name))
    }

    pub fn collect(self) -> Recipes {
        self.recipes
    }
}

pub(crate) fn define(shared_defs: &SharedDefinitions, regs: &IsaRegs) -> RecipeGroup {
    let formats = &shared_defs.formats;

    // Register classes shorthands.
    let gpr = regs.class_by_name("GPR");

    // Definitions.
    let mut recipes = RecipeGroup::new();

    // R-type 32-bit instructions: These are mostly binary arithmetic instructions.
    // The encbits are `opcode[6:2] | (funct3 << 5) | (funct7 << 8)
    recipes.push(
        EncodingRecipeBuilder::new("R", &formats.binary, 4)
            .operands_in(vec![gpr, gpr])
            .operands_out(vec![gpr])
            .emit("put_r(bits, in_reg0, in_reg1, out_reg0, sink);"),
    );

    // R-type with an immediate shift amount instead of rs2.
    recipes.push(
        EncodingRecipeBuilder::new("Rshamt", &formats.binary_imm, 4)
            .operands_in(vec![gpr])
            .operands_out(vec![gpr])
            .emit("put_rshamt(bits, in_reg0, imm.into(), out_reg0, sink);"),
    );

    // R-type encoding of an integer comparison.
    recipes.push(
        EncodingRecipeBuilder::new("Ricmp", &formats.int_compare, 4)
            .operands_in(vec![gpr, gpr])
            .operands_out(vec![gpr])
            .emit("put_r(bits, in_reg0, in_reg1, out_reg0, sink);"),
    );

    recipes.push(
        EncodingRecipeBuilder::new("Ii", &formats.binary_imm, 4)
            .operands_in(vec![gpr])
            .operands_out(vec![gpr])
            .inst_predicate(InstructionPredicate::new_is_signed_int(
                &*formats.binary_imm,
                "imm",
                12,
                0,
            ))
            .emit("put_i(bits, in_reg0, imm.into(), out_reg0, sink);"),
    );

    // I-type instruction with a hardcoded %x0 rs1.
    recipes.push(
        EncodingRecipeBuilder::new("Iz", &formats.unary_imm, 4)
            .operands_out(vec![gpr])
            .inst_predicate(InstructionPredicate::new_is_signed_int(
                &formats.unary_imm,
                "imm",
                12,
                0,
            ))
            .emit("put_i(bits, 0, imm.into(), out_reg0, sink);"),
    );

    // I-type encoding of an integer comparison.
    recipes.push(
        EncodingRecipeBuilder::new("Iicmp", &formats.int_compare_imm, 4)
            .operands_in(vec![gpr])
            .operands_out(vec![gpr])
            .inst_predicate(InstructionPredicate::new_is_signed_int(
                &formats.int_compare_imm,
                "imm",
                12,
                0,
            ))
            .emit("put_i(bits, in_reg0, imm.into(), out_reg0, sink);"),
    );

    // I-type encoding for `jalr` as a return instruction. We won't use the immediate offset.  The
    // variable return values are not encoded.
    recipes.push(
        EncodingRecipeBuilder::new("Iret", &formats.multiary, 4).emit(
            r#"
                    // Return instructions are always a jalr to %x1.
                    // The return address is provided as a special-purpose link argument.
                    put_i(
                        bits,
                        1, // rs1 = %x1
                        0, // no offset.
                        0, // rd = %x0: no address written.
                        sink,
                    );
                "#,
        ),
    );

    // I-type encoding for `jalr` as a call_indirect.
    recipes.push(
        EncodingRecipeBuilder::new("Icall", &formats.call_indirect, 4)
            .operands_in(vec![gpr])
            .emit(
                r#"
                    // call_indirect instructions are jalr with rd=%x1.
                    put_i(
                        bits,
                        in_reg0,
                        0, // no offset.
                        1, // rd = %x1: link register.
                        sink,
                    );
                "#,
            ),
    );

    // Copy of a GPR is implemented as addi x, 0.
    recipes.push(
        EncodingRecipeBuilder::new("Icopy", &formats.unary, 4)
            .operands_in(vec![gpr])
            .operands_out(vec![gpr])
            .emit("put_i(bits, in_reg0, 0, out_reg0, sink);"),
    );

    // Same for a GPR regmove.
    recipes.push(
        EncodingRecipeBuilder::new("Irmov", &formats.reg_move, 4)
            .operands_in(vec![gpr])
            .emit("put_i(bits, src, 0, dst, sink);"),
    );

    // Same for copy-to-SSA -- GPR regmove.
    recipes.push(
        EncodingRecipeBuilder::new("copytossa", &formats.copy_to_ssa, 4)
            // No operands_in to mention, because a source register is specified directly.
            .operands_out(vec![gpr])
            .emit("put_i(bits, src, 0, out_reg0, sink);"),
    );

    // U-type instructions have a 20-bit immediate that targets bits 12-31.
    recipes.push(
        EncodingRecipeBuilder::new("U", &formats.unary_imm, 4)
            .operands_out(vec![gpr])
            .inst_predicate(InstructionPredicate::new_is_signed_int(
                &formats.unary_imm,
                "imm",
                32,
                12,
            ))
            .emit("put_u(bits, imm.into(), out_reg0, sink);"),
    );

    // UJ-type unconditional branch instructions.
    recipes.push(
        EncodingRecipeBuilder::new("UJ", &formats.jump, 4)
            .branch_range((0, 21))
            .emit(
                r#"
                    let dest = i64::from(func.offsets[destination]);
                    let disp = dest - i64::from(sink.offset());
                    put_uj(bits, disp, 0, sink);
                "#,
            ),
    );

    recipes.push(EncodingRecipeBuilder::new("UJcall", &formats.call, 4).emit(
        r#"
                    sink.reloc_external(Reloc::RiscvCall,
                                        &func.dfg.ext_funcs[func_ref].name,
                                        0);
                    // rd=%x1 is the standard link register.
                    put_uj(bits, 0, 1, sink);
                "#,
    ));

    // SB-type branch instructions.
    recipes.push(
        EncodingRecipeBuilder::new("SB", &formats.branch_icmp, 4)
            .operands_in(vec![gpr, gpr])
            .branch_range((0, 13))
            .emit(
                r#"
                    let dest = i64::from(func.offsets[destination]);
                    let disp = dest - i64::from(sink.offset());
                    put_sb(bits, disp, in_reg0, in_reg1, sink);
                "#,
            ),
    );

    // SB-type branch instruction with rs2 fixed to zero.
    recipes.push(
        EncodingRecipeBuilder::new("SBzero", &formats.branch, 4)
            .operands_in(vec![gpr])
            .branch_range((0, 13))
            .emit(
                r#"
                    let dest = i64::from(func.offsets[destination]);
                    let disp = dest - i64::from(sink.offset());
                    put_sb(bits, disp, in_reg0, 0, sink);
                "#,
            ),
    );

    // Spill of a GPR.
    recipes.push(
        EncodingRecipeBuilder::new("GPsp", &formats.unary, 4)
            .operands_in(vec![gpr])
            .operands_out(vec![Stack::new(gpr)])
            .emit("unimplemented!();"),
    );

    // Fill of a GPR.
    recipes.push(
        EncodingRecipeBuilder::new("GPfi", &formats.unary, 4)
            .operands_in(vec![Stack::new(gpr)])
            .operands_out(vec![gpr])
            .emit("unimplemented!();"),
    );

    // Stack-slot to same stack-slot copy, which is guaranteed to turn into a no-op.
    recipes.push(
        EncodingRecipeBuilder::new("stacknull", &formats.unary, 0)
            .operands_in(vec![Stack::new(gpr)])
            .operands_out(vec![Stack::new(gpr)])
            .emit(""),
    );

    // No-op fills, created by late-stage redundant-fill removal.
    recipes.push(
        EncodingRecipeBuilder::new("fillnull", &formats.unary, 0)
            .operands_in(vec![Stack::new(gpr)])
            .operands_out(vec![gpr])
            .clobbers_flags(false)
            .emit(""),
    );

    recipes
}