From 872cf6d06f4d77637b4627fdc583bab79ee2372f Mon Sep 17 00:00:00 2001 From: johannst Date: Mon, 27 Feb 2023 22:52:40 +0000 Subject: deploy: 6486b862edc2750dba83848f62d6c9f3d4c6d3c2 --- src/juicebox_asm/label.rs.html | 146 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 src/juicebox_asm/label.rs.html (limited to 'src/juicebox_asm/label.rs.html') diff --git a/src/juicebox_asm/label.rs.html b/src/juicebox_asm/label.rs.html new file mode 100644 index 0000000..cf7e7ae --- /dev/null +++ b/src/juicebox_asm/label.rs.html @@ -0,0 +1,146 @@ +label.rs - source
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
+
use std::collections::HashSet;
+
+/// A label which is used as target for jump instructions.
+///
+/// ```rust
+/// use juicebox_asm::prelude::*;
+///
+/// let mut lbl = Label::new();
+/// let mut asm = Asm::new();
+///
+/// // Skip the mov instruction.
+/// asm.jmp(&mut lbl);
+/// asm.mov(Reg64::rax, Reg64::rax);
+/// asm.bind(&mut lbl);
+/// ```
+///
+/// # Panics
+///
+/// Panics if the label is dropped while not yet bound, or having unresolved relocations.
+/// This is mainly a safety-guard to detect wrong usage.
+pub struct Label {
+    /// Location of the label. Will be set after the label is bound, else None.
+    location: Option<usize>,
+
+    /// Offsets that must be patched with the label location.
+    offsets: HashSet<usize>,
+}
+
+impl Label {
+    /// Create a new `unbound` [Label].
+    pub fn new() -> Label {
+        Label {
+            location: None,
+            offsets: HashSet::new(),
+        }
+    }
+
+    /// Bind the label to the `location`.
+    pub(crate) fn bind(&mut self, loc: usize) {
+        // A label can only be bound once!
+        assert!(!self.is_bound());
+
+        self.location = Some(loc);
+    }
+
+    /// Record an offset that must be patched with the label location.
+    pub(crate) fn record_offset(&mut self, off: usize) {
+        self.offsets.insert(off);
+    }
+
+    pub(crate) fn location(&self) -> Option<usize> {
+        self.location
+    }
+
+    pub(crate) fn offsets_mut(&mut self) -> &mut HashSet<usize> {
+        &mut self.offsets
+    }
+
+    /// Check whether the label is bound to a location.
+    const fn is_bound(&self) -> bool {
+        self.location.is_some()
+    }
+}
+
+impl Drop for Label {
+    fn drop(&mut self) {
+        // Ensure the label was bound when it is dropped.
+        assert!(self.is_bound());
+        // Ensure all offsets have been patched when the label is dropped.
+        assert!(self.offsets.is_empty());
+    }
+}
+
+
\ No newline at end of file -- cgit v1.2.3