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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use crate::cdsl::ast::{Apply, Expr, Literal, VarPool};
use crate::cdsl::encodings::{Encoding, EncodingBuilder};
use crate::cdsl::instructions::{
    Bindable, BoundInstruction, InstSpec, InstructionPredicateNode, InstructionPredicateRegistry,
};
use crate::cdsl::recipes::{EncodingRecipeNumber, Recipes};
use crate::cdsl::settings::SettingGroup;

use crate::shared::types::Bool::B1;
use crate::shared::types::Float::{F32, F64};
use crate::shared::types::Int::{I16, I32, I64, I8};
use crate::shared::types::Reference::{R32, R64};
use crate::shared::Definitions as SharedDefinitions;

use super::recipes::RecipeGroup;

pub(crate) struct PerCpuModeEncodings<'defs> {
    pub inst_pred_reg: InstructionPredicateRegistry,
    pub enc32: Vec<Encoding>,
    pub enc64: Vec<Encoding>,
    recipes: &'defs Recipes,
}

impl<'defs> PerCpuModeEncodings<'defs> {
    fn new(recipes: &'defs Recipes) -> Self {
        Self {
            inst_pred_reg: InstructionPredicateRegistry::new(),
            enc32: Vec::new(),
            enc64: Vec::new(),
            recipes,
        }
    }
    fn enc(
        &self,
        inst: impl Into<InstSpec>,
        recipe: EncodingRecipeNumber,
        bits: u16,
    ) -> EncodingBuilder {
        EncodingBuilder::new(inst.into(), recipe, bits)
    }
    fn add32(&mut self, encoding: EncodingBuilder) {
        self.enc32
            .push(encoding.build(self.recipes, &mut self.inst_pred_reg));
    }
    fn add64(&mut self, encoding: EncodingBuilder) {
        self.enc64
            .push(encoding.build(self.recipes, &mut self.inst_pred_reg));
    }
}

// The low 7 bits of a RISC-V instruction is the base opcode. All 32-bit instructions have 11 as
// the two low bits, with bits 6:2 determining the base opcode.
//
// Encbits for the 32-bit recipes are opcode[6:2] | (funct3 << 5) | ...
// The functions below encode the encbits.

fn load_bits(funct3: u16) -> u16 {
    assert!(funct3 <= 0b111);
    funct3 << 5
}

fn store_bits(funct3: u16) -> u16 {
    assert!(funct3 <= 0b111);
    0b01000 | (funct3 << 5)
}

fn branch_bits(funct3: u16) -> u16 {
    assert!(funct3 <= 0b111);
    0b11000 | (funct3 << 5)
}

fn jalr_bits() -> u16 {
    // This was previously accepting an argument funct3 of 3 bits and used the following formula:
    //0b11001 | (funct3 << 5)
    0b11001
}

fn jal_bits() -> u16 {
    0b11011
}

fn opimm_bits(funct3: u16, funct7: u16) -> u16 {
    assert!(funct3 <= 0b111);
    0b00100 | (funct3 << 5) | (funct7 << 8)
}

fn opimm32_bits(funct3: u16, funct7: u16) -> u16 {
    assert!(funct3 <= 0b111);
    0b00110 | (funct3 << 5) | (funct7 << 8)
}

fn op_bits(funct3: u16, funct7: u16) -> u16 {
    assert!(funct3 <= 0b111);
    assert!(funct7 <= 0b111_1111);
    0b01100 | (funct3 << 5) | (funct7 << 8)
}

fn op32_bits(funct3: u16, funct7: u16) -> u16 {
    assert!(funct3 <= 0b111);
    assert!(funct7 <= 0b111_1111);
    0b01110 | (funct3 << 5) | (funct7 << 8)
}

fn lui_bits() -> u16 {
    0b01101
}

pub(crate) fn define<'defs>(
    shared_defs: &'defs SharedDefinitions,
    isa_settings: &SettingGroup,
    recipes: &'defs RecipeGroup,
) -> PerCpuModeEncodings<'defs> {
    // Instructions shorthands.
    let shared = &shared_defs.instructions;

    let band = shared.by_name("band");
    let band_imm = shared.by_name("band_imm");
    let bor = shared.by_name("bor");
    let bor_imm = shared.by_name("bor_imm");
    let br_icmp = shared.by_name("br_icmp");
    let brz = shared.by_name("brz");
    let brnz = shared.by_name("brnz");
    let bxor = shared.by_name("bxor");
    let bxor_imm = shared.by_name("bxor_imm");
    let call = shared.by_name("call");
    let call_indirect = shared.by_name("call_indirect");
    let copy = shared.by_name("copy");
    let copy_nop = shared.by_name("copy_nop");
    let copy_to_ssa = shared.by_name("copy_to_ssa");
    let fill = shared.by_name("fill");
    let fill_nop = shared.by_name("fill_nop");
    let iadd = shared.by_name("iadd");
    let iadd_imm = shared.by_name("iadd_imm");
    let iconst = shared.by_name("iconst");
    let icmp = shared.by_name("icmp");
    let icmp_imm = shared.by_name("icmp_imm");
    let imul = shared.by_name("imul");
    let ishl = shared.by_name("ishl");
    let ishl_imm = shared.by_name("ishl_imm");
    let isub = shared.by_name("isub");
    let jump = shared.by_name("jump");
    let regmove = shared.by_name("regmove");
    let spill = shared.by_name("spill");
    let sshr = shared.by_name("sshr");
    let sshr_imm = shared.by_name("sshr_imm");
    let ushr = shared.by_name("ushr");
    let ushr_imm = shared.by_name("ushr_imm");
    let return_ = shared.by_name("return");

    // Recipes shorthands, prefixed with r_.
    let r_copytossa = recipes.by_name("copytossa");
    let r_fillnull = recipes.by_name("fillnull");
    let r_icall = recipes.by_name("Icall");
    let r_icopy = recipes.by_name("Icopy");
    let r_ii = recipes.by_name("Ii");
    let r_iicmp = recipes.by_name("Iicmp");
    let r_iret = recipes.by_name("Iret");
    let r_irmov = recipes.by_name("Irmov");
    let r_iz = recipes.by_name("Iz");
    let r_gp_sp = recipes.by_name("GPsp");
    let r_gp_fi = recipes.by_name("GPfi");
    let r_r = recipes.by_name("R");
    let r_ricmp = recipes.by_name("Ricmp");
    let r_rshamt = recipes.by_name("Rshamt");
    let r_sb = recipes.by_name("SB");
    let r_sb_zero = recipes.by_name("SBzero");
    let r_stacknull = recipes.by_name("stacknull");
    let r_u = recipes.by_name("U");
    let r_uj = recipes.by_name("UJ");
    let r_uj_call = recipes.by_name("UJcall");

    // Predicates shorthands.
    let use_m = isa_settings.predicate_by_name("use_m");

    // Definitions.
    let mut e = PerCpuModeEncodings::new(&recipes.recipes);

    // Basic arithmetic binary instructions are encoded in an R-type instruction.
    for &(inst, inst_imm, f3, f7) in &[
        (iadd, Some(iadd_imm), 0b000, 0b000_0000),
        (isub, None, 0b000, 0b010_0000),
        (bxor, Some(bxor_imm), 0b100, 0b000_0000),
        (bor, Some(bor_imm), 0b110, 0b000_0000),
        (band, Some(band_imm), 0b111, 0b000_0000),
    ] {
        e.add32(e.enc(inst.bind(I32), r_r, op_bits(f3, f7)));
        e.add64(e.enc(inst.bind(I64), r_r, op_bits(f3, f7)));

        // Immediate versions for add/xor/or/and.
        if let Some(inst_imm) = inst_imm {
            e.add32(e.enc(inst_imm.bind(I32), r_ii, opimm_bits(f3, 0)));
            e.add64(e.enc(inst_imm.bind(I64), r_ii, opimm_bits(f3, 0)));
        }
    }

    // 32-bit ops in RV64.
    e.add64(e.enc(iadd.bind(I32), r_r, op32_bits(0b000, 0b000_0000)));
    e.add64(e.enc(isub.bind(I32), r_r, op32_bits(0b000, 0b010_0000)));
    // There are no andiw/oriw/xoriw variations.
    e.add64(e.enc(iadd_imm.bind(I32), r_ii, opimm32_bits(0b000, 0)));

    // Use iadd_imm with %x0 to materialize constants.
    e.add32(e.enc(iconst.bind(I32), r_iz, opimm_bits(0b0, 0)));
    e.add64(e.enc(iconst.bind(I32), r_iz, opimm_bits(0b0, 0)));
    e.add64(e.enc(iconst.bind(I64), r_iz, opimm_bits(0b0, 0)));

    // Dynamic shifts have the same masking semantics as the clif base instructions.
    for &(inst, inst_imm, f3, f7) in &[
        (ishl, ishl_imm, 0b1, 0b0),
        (ushr, ushr_imm, 0b101, 0b0),
        (sshr, sshr_imm, 0b101, 0b10_0000),
    ] {
        e.add32(e.enc(inst.bind(I32).bind(I32), r_r, op_bits(f3, f7)));
        e.add64(e.enc(inst.bind(I64).bind(I64), r_r, op_bits(f3, f7)));
        e.add64(e.enc(inst.bind(I32).bind(I32), r_r, op32_bits(f3, f7)));
        // Allow i32 shift amounts in 64-bit shifts.
        e.add64(e.enc(inst.bind(I64).bind(I32), r_r, op_bits(f3, f7)));
        e.add64(e.enc(inst.bind(I32).bind(I64), r_r, op32_bits(f3, f7)));

        // Immediate shifts.
        e.add32(e.enc(inst_imm.bind(I32), r_rshamt, opimm_bits(f3, f7)));
        e.add64(e.enc(inst_imm.bind(I64), r_rshamt, opimm_bits(f3, f7)));
        e.add64(e.enc(inst_imm.bind(I32), r_rshamt, opimm32_bits(f3, f7)));
    }

    // Signed and unsigned integer 'less than'. There are no 'w' variants for comparing 32-bit
    // numbers in RV64.
    {
        let mut var_pool = VarPool::new();

        // Helper that creates an instruction predicate for an instruction in the icmp family.
        let mut icmp_instp = |bound_inst: &BoundInstruction,
                              intcc_field: &'static str|
         -> InstructionPredicateNode {
            let x = var_pool.create("x");
            let y = var_pool.create("y");
            let cc = Literal::enumerator_for(&shared_defs.imm.intcc, intcc_field);
            Apply::new(
                bound_inst.clone().into(),
                vec![Expr::Literal(cc), Expr::Var(x), Expr::Var(y)],
            )
            .inst_predicate(&var_pool)
            .unwrap()
        };

        let icmp_i32 = icmp.bind(I32);
        let icmp_i64 = icmp.bind(I64);
        e.add32(
            e.enc(icmp_i32.clone(), r_ricmp, op_bits(0b010, 0b000_0000))
                .inst_predicate(icmp_instp(&icmp_i32, "slt")),
        );
        e.add64(
            e.enc(icmp_i64.clone(), r_ricmp, op_bits(0b010, 0b000_0000))
                .inst_predicate(icmp_instp(&icmp_i64, "slt")),
        );

        e.add32(
            e.enc(icmp_i32.clone(), r_ricmp, op_bits(0b011, 0b000_0000))
                .inst_predicate(icmp_instp(&icmp_i32, "ult")),
        );
        e.add64(
            e.enc(icmp_i64.clone(), r_ricmp, op_bits(0b011, 0b000_0000))
                .inst_predicate(icmp_instp(&icmp_i64, "ult")),
        );

        // Immediate variants.
        let icmp_i32 = icmp_imm.bind(I32);
        let icmp_i64 = icmp_imm.bind(I64);
        e.add32(
            e.enc(icmp_i32.clone(), r_iicmp, opimm_bits(0b010, 0))
                .inst_predicate(icmp_instp(&icmp_i32, "slt")),
        );
        e.add64(
            e.enc(icmp_i64.clone(), r_iicmp, opimm_bits(0b010, 0))
                .inst_predicate(icmp_instp(&icmp_i64, "slt")),
        );

        e.add32(
            e.enc(icmp_i32.clone(), r_iicmp, opimm_bits(0b011, 0))
                .inst_predicate(icmp_instp(&icmp_i32, "ult")),
        );
        e.add64(
            e.enc(icmp_i64.clone(), r_iicmp, opimm_bits(0b011, 0))
                .inst_predicate(icmp_instp(&icmp_i64, "ult")),
        );
    }

    // Integer constants with the low 12 bits clear are materialized by lui.
    e.add32(e.enc(iconst.bind(I32), r_u, lui_bits()));
    e.add64(e.enc(iconst.bind(I32), r_u, lui_bits()));
    e.add64(e.enc(iconst.bind(I64), r_u, lui_bits()));

    // "M" Standard Extension for Integer Multiplication and Division.
    // Gated by the `use_m` flag.
    e.add32(
        e.enc(imul.bind(I32), r_r, op_bits(0b000, 0b0000_0001))
            .isa_predicate(use_m),
    );
    e.add64(
        e.enc(imul.bind(I64), r_r, op_bits(0b000, 0b0000_0001))
            .isa_predicate(use_m),
    );
    e.add64(
        e.enc(imul.bind(I32), r_r, op32_bits(0b000, 0b0000_0001))
            .isa_predicate(use_m),
    );

    // Control flow.

    // Unconditional branches.
    e.add32(e.enc(jump, r_uj, jal_bits()));
    e.add64(e.enc(jump, r_uj, jal_bits()));
    e.add32(e.enc(call, r_uj_call, jal_bits()));
    e.add64(e.enc(call, r_uj_call, jal_bits()));

    // Conditional branches.
    {
        let mut var_pool = VarPool::new();

        // Helper that creates an instruction predicate for an instruction in the icmp family.
        let mut br_icmp_instp = |bound_inst: &BoundInstruction,
                                 intcc_field: &'static str|
         -> InstructionPredicateNode {
            let x = var_pool.create("x");
            let y = var_pool.create("y");
            let dest = var_pool.create("dest");
            let args = var_pool.create("args");
            let cc = Literal::enumerator_for(&shared_defs.imm.intcc, intcc_field);
            Apply::new(
                bound_inst.clone().into(),
                vec![
                    Expr::Literal(cc),
                    Expr::Var(x),
                    Expr::Var(y),
                    Expr::Var(dest),
                    Expr::Var(args),
                ],
            )
            .inst_predicate(&var_pool)
            .unwrap()
        };

        let br_icmp_i32 = br_icmp.bind(I32);
        let br_icmp_i64 = br_icmp.bind(I64);
        for &(cond, f3) in &[
            ("eq", 0b000),
            ("ne", 0b001),
            ("slt", 0b100),
            ("sge", 0b101),
            ("ult", 0b110),
            ("uge", 0b111),
        ] {
            e.add32(
                e.enc(br_icmp_i32.clone(), r_sb, branch_bits(f3))
                    .inst_predicate(br_icmp_instp(&br_icmp_i32, cond)),
            );
            e.add64(
                e.enc(br_icmp_i64.clone(), r_sb, branch_bits(f3))
                    .inst_predicate(br_icmp_instp(&br_icmp_i64, cond)),
            );
        }
    }

    for &(inst, f3) in &[(brz, 0b000), (brnz, 0b001)] {
        e.add32(e.enc(inst.bind(I32), r_sb_zero, branch_bits(f3)));
        e.add64(e.enc(inst.bind(I64), r_sb_zero, branch_bits(f3)));
        e.add32(e.enc(inst.bind(B1), r_sb_zero, branch_bits(f3)));
        e.add64(e.enc(inst.bind(B1), r_sb_zero, branch_bits(f3)));
    }

    // Returns are a special case of jalr_bits using %x1 to hold the return address.
    // The return address is provided by a special-purpose `link` return value that
    // is added by legalize_signature().
    e.add32(e.enc(return_, r_iret, jalr_bits()));
    e.add64(e.enc(return_, r_iret, jalr_bits()));
    e.add32(e.enc(call_indirect.bind(I32), r_icall, jalr_bits()));
    e.add64(e.enc(call_indirect.bind(I64), r_icall, jalr_bits()));

    // Spill and fill.
    e.add32(e.enc(spill.bind(I32), r_gp_sp, store_bits(0b010)));
    e.add64(e.enc(spill.bind(I32), r_gp_sp, store_bits(0b010)));
    e.add64(e.enc(spill.bind(I64), r_gp_sp, store_bits(0b011)));
    e.add32(e.enc(fill.bind(I32), r_gp_fi, load_bits(0b010)));
    e.add64(e.enc(fill.bind(I32), r_gp_fi, load_bits(0b010)));
    e.add64(e.enc(fill.bind(I64), r_gp_fi, load_bits(0b011)));

    // No-op fills, created by late-stage redundant-fill removal.
    for &ty in &[I64, I32] {
        e.add64(e.enc(fill_nop.bind(ty), r_fillnull, 0));
        e.add32(e.enc(fill_nop.bind(ty), r_fillnull, 0));
    }
    e.add64(e.enc(fill_nop.bind(B1), r_fillnull, 0));
    e.add32(e.enc(fill_nop.bind(B1), r_fillnull, 0));

    // Register copies.
    e.add32(e.enc(copy.bind(I32), r_icopy, opimm_bits(0b000, 0)));
    e.add64(e.enc(copy.bind(I64), r_icopy, opimm_bits(0b000, 0)));
    e.add64(e.enc(copy.bind(I32), r_icopy, opimm32_bits(0b000, 0)));

    e.add32(e.enc(regmove.bind(I32), r_irmov, opimm_bits(0b000, 0)));
    e.add64(e.enc(regmove.bind(I64), r_irmov, opimm_bits(0b000, 0)));
    e.add64(e.enc(regmove.bind(I32), r_irmov, opimm32_bits(0b000, 0)));

    e.add32(e.enc(copy.bind(B1), r_icopy, opimm_bits(0b000, 0)));
    e.add64(e.enc(copy.bind(B1), r_icopy, opimm_bits(0b000, 0)));
    e.add32(e.enc(regmove.bind(B1), r_irmov, opimm_bits(0b000, 0)));
    e.add64(e.enc(regmove.bind(B1), r_irmov, opimm_bits(0b000, 0)));

    // Stack-slot-to-the-same-stack-slot copy, which is guaranteed to turn
    // into a no-op.
    // The same encoding is generated for both the 64- and 32-bit architectures.
    for &ty in &[I64, I32, I16, I8] {
        e.add32(e.enc(copy_nop.bind(ty), r_stacknull, 0));
        e.add64(e.enc(copy_nop.bind(ty), r_stacknull, 0));
    }
    for &ty in &[F64, F32] {
        e.add32(e.enc(copy_nop.bind(ty), r_stacknull, 0));
        e.add64(e.enc(copy_nop.bind(ty), r_stacknull, 0));
    }

    // Copy-to-SSA
    e.add32(e.enc(copy_to_ssa.bind(I32), r_copytossa, opimm_bits(0b000, 0)));
    e.add64(e.enc(copy_to_ssa.bind(I64), r_copytossa, opimm_bits(0b000, 0)));
    e.add64(e.enc(copy_to_ssa.bind(I32), r_copytossa, opimm32_bits(0b000, 0)));
    e.add32(e.enc(copy_to_ssa.bind(B1), r_copytossa, opimm_bits(0b000, 0)));
    e.add64(e.enc(copy_to_ssa.bind(B1), r_copytossa, opimm_bits(0b000, 0)));
    e.add32(e.enc(copy_to_ssa.bind(R32), r_copytossa, opimm_bits(0b000, 0)));
    e.add64(e.enc(copy_to_ssa.bind(R64), r_copytossa, opimm_bits(0b000, 0)));

    e
}