pub struct Offset32(_);
Expand description

32-bit signed immediate offset.

This is used to encode an immediate offset for load/store instructions. All supported ISAs have a maximum load/store offset that fits in an i32.

Implementations§

Create a new Offset32 representing the signed number x.

Examples found in repository?
src/isa/x64/lower/isle.rs (line 620)
619
620
621
    fn zero_offset(&mut self) -> Offset32 {
        Offset32::new(0)
    }
More examples
Hide additional examples
src/ir/immediates.rs (line 401)
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
    pub fn try_from_i64(x: i64) -> Option<Self> {
        let x = i32::try_from(x).ok()?;
        Some(Self::new(x))
    }

    /// Add in the signed number `x` if possible.
    pub fn try_add_i64(self, x: i64) -> Option<Self> {
        let x = i32::try_from(x).ok()?;
        let ret = self.0.checked_add(x)?;
        Some(Self::new(ret))
    }
}

impl From<Offset32> for i32 {
    fn from(val: Offset32) -> i32 {
        val.0
    }
}

impl From<Offset32> for i64 {
    fn from(val: Offset32) -> i64 {
        i64::from(val.0)
    }
}

impl From<i32> for Offset32 {
    fn from(x: i32) -> Self {
        Self(x)
    }
}

impl From<u8> for Offset32 {
    fn from(val: u8) -> Offset32 {
        Self(val.into())
    }
}

impl Display for Offset32 {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        // 0 displays as an empty offset.
        if self.0 == 0 {
            return Ok(());
        }

        // Always include a sign.
        write!(f, "{}", if self.0 < 0 { '-' } else { '+' })?;

        let val = i64::from(self.0).abs();
        if val < 10_000 {
            write!(f, "{}", val)
        } else {
            write_hex(val as u64, f)
        }
    }
}

impl FromStr for Offset32 {
    type Err = &'static str;

    // Parse a decimal or hexadecimal `Offset32`, formatted as above.
    fn from_str(s: &str) -> Result<Self, &'static str> {
        if !(s.starts_with('-') || s.starts_with('+')) {
            return Err("Offset must begin with sign");
        }
        parse_i64(s).and_then(|x| {
            if i64::from(i32::MIN) <= x && x <= i64::from(i32::MAX) {
                Ok(Self::new(x as i32))
            } else {
                Err("Offset out of range")
            }
        })
    }
src/legalizer/heap.rs (line 42)
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
pub fn expand_heap_load(
    inst: ir::Inst,
    func: &mut ir::Function,
    cfg: &mut ControlFlowGraph,
    isa: &dyn TargetIsa,
    heap_imm: ir::HeapImm,
    index: ir::Value,
) {
    let HeapImmData {
        flags,
        heap,
        offset,
    } = func.dfg.heap_imms[heap_imm];

    let result_ty = func.dfg.ctrl_typevar(inst);
    let access_size = result_ty.bytes();
    let access_size = u8::try_from(access_size).unwrap();

    let mut pos = FuncCursor::new(func).at_inst(inst);
    pos.use_srcloc(inst);

    let addr =
        bounds_check_and_compute_addr(&mut pos, cfg, isa, heap, index, offset.into(), access_size);

    pos.func
        .dfg
        .replace(inst)
        .load(result_ty, flags, addr, Offset32::new(0));
}

/// Expand a `heap_store` instruction according to the definition of the heap.
pub fn expand_heap_store(
    inst: ir::Inst,
    func: &mut ir::Function,
    cfg: &mut ControlFlowGraph,
    isa: &dyn TargetIsa,
    heap_imm: ir::HeapImm,
    index: ir::Value,
    value: ir::Value,
) {
    let HeapImmData {
        flags,
        heap,
        offset,
    } = func.dfg.heap_imms[heap_imm];

    let store_ty = func.dfg.value_type(value);
    let access_size = u8::try_from(store_ty.bytes()).unwrap();

    let mut pos = FuncCursor::new(func).at_inst(inst);
    pos.use_srcloc(inst);

    let addr =
        bounds_check_and_compute_addr(&mut pos, cfg, isa, heap, index, offset.into(), access_size);

    pos.func
        .dfg
        .replace(inst)
        .store(flags, value, addr, Offset32::new(0));
}
src/legalizer/table.rs (line 93)
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
fn compute_addr(
    inst: ir::Inst,
    table: ir::Table,
    addr_ty: ir::Type,
    mut index: ir::Value,
    index_ty: ir::Type,
    element_offset: Offset32,
    func: &mut ir::Function,
    spectre_oob_cmp: Option<(ir::Value, ir::Value)>,
) {
    let mut pos = FuncCursor::new(func).at_inst(inst);
    pos.use_srcloc(inst);

    // Convert `index` to `addr_ty`.
    if index_ty != addr_ty {
        index = pos.ins().uextend(addr_ty, index);
    }

    // Add the table base address base
    let base_gv = pos.func.tables[table].base_gv;
    let base = pos.ins().global_value(addr_ty, base_gv);

    let element_size = pos.func.tables[table].element_size;
    let mut offset;
    let element_size: u64 = element_size.into();
    if element_size == 1 {
        offset = index;
    } else if element_size.is_power_of_two() {
        offset = pos
            .ins()
            .ishl_imm(index, i64::from(element_size.trailing_zeros()));
    } else {
        offset = pos.ins().imul_imm(index, element_size as i64);
    }

    let element_addr = if element_offset == Offset32::new(0) {
        pos.ins().iadd(base, offset)
    } else {
        let imm: i64 = element_offset.into();
        offset = pos.ins().iadd(base, offset);
        pos.ins().iadd_imm(offset, imm)
    };

    let element_addr = if let Some((index, bound)) = spectre_oob_cmp {
        let cond = pos
            .ins()
            .icmp(IntCC::UnsignedGreaterThanOrEqual, index, bound);
        // If out-of-bounds, choose the table base on the misspeculation path.
        pos.ins().select_spectre_guard(cond, base, element_addr)
    } else {
        element_addr
    };
    let new_inst = pos.func.dfg.value_def(element_addr).inst().unwrap();

    pos.func.dfg.replace_with_aliases(inst, new_inst);
    pos.remove_inst();
}

Create a new Offset32 representing the signed number x if possible.

Add in the signed number x if possible.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.