header.rs 11.2 KB
Newer Older
qinsoon's avatar
qinsoon committed
1 2
#![allow(dead_code)]

3 4 5 6 7 8 9 10 11 12 13 14 15
/// * use 1 word  (64bits)  header

/// * header is before an object reference

/// * for fix-sized types
///   MSB 1 bit - is object start
///       1 bit - trace bit
///       1 bit - is fix-sized (set for fix-sized types)
///       1 bit - is reference map encoded?
///       ... (unused)
///       16 bits -

/// fix-sized with reference map
qinsoon's avatar
qinsoon committed
16
/// | start? | trace? | fix? | ref map? | size (28bits)      | reference map (32bits) |
17 18 19 20 21 22 23 24 25 26
///                      1        1

/// fix-sized with ID
/// | start? | trace? | fix? | ref map? | (unused bits)  ... | gc type ID    (32bits) |
///                      1        0

/// var-sized
/// | start? | trace? | fix? | hybrid length (29bits ~ 500M) | gc type ID    (32bits) |
///                      0

27
use common::gctype::GCType;
qinsoon's avatar
qinsoon committed
28
use objectmodel;
29 30 31 32 33 34
use utils::ByteSize;
use utils::ByteOffset;
use utils::bit_utils;

use utils::{Address, ObjectReference};
use utils::POINTER_SIZE;
35
use utils::LOG_POINTER_SIZE;
36 37 38 39 40 41 42 43 44

pub const OBJECT_HEADER_SIZE   : ByteSize   = 8;
pub const OBJECT_HEADER_OFFSET : ByteOffset = - (OBJECT_HEADER_SIZE as ByteOffset);

pub const BIT_IS_OBJ_START  : usize = 63;
pub const BIT_IS_TRACED     : usize = 62;
pub const BIT_IS_FIX_SIZE   : usize = 61;
pub const BIT_HAS_REF_MAP   : usize = 60;

45
pub const REF_MAP_LENGTH    : usize = 32;
qinsoon's avatar
qinsoon committed
46

47 48
pub const MASK_REF_MAP      : u64 = 0xFFFFFFFFu64;
pub const MASK_GCTYPE_ID    : u64 = 0xFFFFFFFFu64;
qinsoon's avatar
qinsoon committed
49

50 51 52
pub const MASK_HYBRID_LENGTH: u64 = 0x1FFFFFFF00000000u64;
pub const SHR_HYBRID_LENGTH : usize = 32;

qinsoon's avatar
qinsoon committed
53 54 55
pub const MASK_OBJ_SIZE     : u64   = 0x0FFFFFFF00000000u64;
pub const SHR_OBJ_SIZE      : usize = 32;

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
pub fn gen_gctype_encode(ty: &GCType) -> u64 {
    let mut ret = 0u64;

    if ty.repeat_refs.is_some() {
        // var sized
        let len = ty.repeat_refs.as_ref().unwrap().count;

        // encode length
        ret = ret | (( (len as u64) << SHR_HYBRID_LENGTH) & MASK_HYBRID_LENGTH);
        // encode gc id
        ret = ret | (ty.id as u64);
    } else {
        // fix sized
        ret = ret | (1 << BIT_IS_FIX_SIZE);

        // encode ref map?
        if ty.size < REF_MAP_LENGTH * POINTER_SIZE {
            // encode ref map
            let offsets = ty.gen_ref_offsets();
            let mut ref_map = 0;

            for offset in offsets {
                ref_map = ref_map | (1 << (offset >> LOG_POINTER_SIZE));
            }

            ret = ret | (ref_map & MASK_REF_MAP);
qinsoon's avatar
qinsoon committed
82 83 84

            // encode size
            ret = ret | (((ty.size as u64) << SHR_OBJ_SIZE) & MASK_OBJ_SIZE);
85 86 87 88 89 90 91 92
        } else {
            ret = ret | (ty.id as u64);
        }
    }

    ret
}

93 94 95 96 97 98 99 100
#[allow(unused_variables)]
pub fn print_object(obj: Address) {
    let mut cursor = obj;
    trace!("OBJECT 0x{:x}", obj);

    let hdr = unsafe {cursor.offset(OBJECT_HEADER_OFFSET).load::<u64>()};

    trace!("- is object start? {}", header_is_object_start(hdr));
qinsoon's avatar
qinsoon committed
101
    trace!("- is traced? {}", header_is_traced(hdr, objectmodel::load_mark_state()));
102 103 104 105 106 107 108 109
    if header_is_fix_size(hdr) {
        trace!("- is fix sized? true");
        if header_has_ref_map(hdr) {
            trace!("- has ref map: {:b}", header_get_ref_map(hdr));
        } else {
            trace!("- has type ID: {}", header_get_gctype_id(hdr));
        }
    } else {
110
        trace!("- more info about hybrid, not implemented");
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
    }

    trace!("0x{:x} | val: 0x{:15x} | hdr: {:b}",
    cursor, unsafe{cursor.load::<u64>()}, hdr);
    cursor = cursor.plus(POINTER_SIZE);
    trace!("0x{:x} | val: 0x{:15x}",
    cursor, unsafe{cursor.load::<u64>()});

    cursor = cursor.plus(POINTER_SIZE);
    trace!("0x{:x} | val: 0x{:15x}",
    cursor, unsafe{cursor.load::<u64>()});

    cursor = cursor.plus(POINTER_SIZE);
    trace!("0x{:x} | val: 0x{:15x}",
    cursor, unsafe{cursor.load::<u64>()});

    cursor = cursor.plus(POINTER_SIZE);
    trace!("0x{:x} | val: 0x{:15x}",
    cursor, unsafe{cursor.load::<u64>()});

    cursor = cursor.plus(POINTER_SIZE);
    trace!("0x{:x} | val: 0x{:15x}",
    cursor, unsafe{cursor.load::<u64>()});
}

#[inline(always)]
pub fn mark_as_traced(obj: ObjectReference, mark_state: u8) {
    unsafe {
        let hdr_addr = obj.to_address().offset(OBJECT_HEADER_OFFSET);
        hdr_addr.store(bit_utils::set_nth_bit_u64(hdr_addr.load::<u64>(), BIT_IS_TRACED, mark_state));
    }
}

#[inline(always)]
pub fn mark_as_untraced(addr: Address, mark_state: u8) {
    unsafe {
        let hdr_addr = addr.offset(OBJECT_HEADER_OFFSET);
        hdr_addr.store(bit_utils::set_nth_bit_u64(hdr_addr.load::<u64>(), BIT_IS_TRACED, mark_state ^ 1));
    }
}

#[inline(always)]
pub fn is_traced(obj: ObjectReference, mark_state: u8) -> bool {
    unsafe {
        let hdr = obj.to_address().offset(OBJECT_HEADER_OFFSET).load::<u64>();
qinsoon's avatar
qinsoon committed
156
        bit_utils::test_nth_bit_u64(hdr, BIT_IS_TRACED, mark_state)
157 158 159 160 161
    }
}

#[inline(always)]
pub fn header_is_object_start(hdr: u64) -> bool {
qinsoon's avatar
qinsoon committed
162
    bit_utils::test_nth_bit_u64(hdr, BIT_IS_OBJ_START, 1u8)
163 164 165 166
}

#[inline(always)]
pub fn header_is_fix_size(hdr: u64) -> bool {
qinsoon's avatar
qinsoon committed
167
    bit_utils::test_nth_bit_u64(hdr, BIT_IS_FIX_SIZE, 1u8)
168 169 170
}

#[inline(always)]
qinsoon's avatar
qinsoon committed
171 172
pub fn header_is_traced(hdr: u64, mark_state: u8) -> bool {
    bit_utils::test_nth_bit_u64(hdr, BIT_IS_TRACED, mark_state)
173 174 175 176
}

#[inline(always)]
pub fn header_has_ref_map(hdr: u64) -> bool {
qinsoon's avatar
qinsoon committed
177
    bit_utils::test_nth_bit_u64(hdr, BIT_HAS_REF_MAP, 1u8)
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
}

#[inline(always)]
pub fn header_get_ref_map(hdr: u64) -> u32 {
    (hdr & MASK_REF_MAP) as u32
}

#[inline(always)]
pub fn header_get_hybrid_length(hdr: u64) -> u32 {
    ((hdr & MASK_HYBRID_LENGTH) >> SHR_HYBRID_LENGTH) as u32
}

#[inline(always)]
pub fn header_get_gctype_id(hdr: u64) -> u32 {
    (hdr & MASK_GCTYPE_ID) as u32
}

qinsoon's avatar
qinsoon committed
195 196 197 198 199
#[inline(always)]
pub fn header_get_object_size(hdr: u64) -> u32 {
    ((hdr & MASK_OBJ_SIZE) >> SHR_OBJ_SIZE) as u32
}

200 201 202
#[cfg(test)]
mod tests {
    use super::*;
203 204 205
    use common::gctype::*;
    use utils::POINTER_SIZE;
    use std::sync::Arc;
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

    #[test]
    fn fixsize_header_refmap() {
        let hdr_bin = 0b10110000_00000000_00000000_00000000_00000000_00000000_00000000_00000011u64;
        let hdr_hex = 0xb000000000000003u64;

        println!("");
        println!("binary: {:b}", hdr_bin);
        println!("hex   : {:b}", hdr_hex);

        assert_eq!(hdr_bin, hdr_hex);

        let hdr = hdr_hex;

        assert!(header_is_object_start(hdr));
qinsoon's avatar
qinsoon committed
221
        assert!(!header_is_traced(hdr, 1u8));
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        assert!(header_is_fix_size(hdr));
        assert!(header_has_ref_map(hdr));

        assert_eq!(header_get_ref_map(hdr), 0b0011);
    }

    #[test]
    fn fixsize_header_id() {
        let hdr_bin = 0b10100000_00000000_00000000_00000000_00000000_00000000_00000000_11111111u64;
        let hdr_hex = 0xa0000000000000ffu64;

        println!("");
        println!("binary: {:b}", hdr_bin);
        println!("hex   : {:b}", hdr_hex);

        assert_eq!(hdr_bin, hdr_hex);

        let hdr = hdr_hex;

        assert!(header_is_object_start(hdr));
qinsoon's avatar
qinsoon committed
242
        assert!(!header_is_traced(hdr, 1u8));
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        assert!(header_is_fix_size(hdr));
        assert!(!header_has_ref_map(hdr));

        assert_eq!(header_get_gctype_id(hdr), 0xff);
    }

    #[test]
    fn varsize_header() {
        let hdr_bin = 0b10000000_00000000_00000000_10000000_00000000_00000000_00000000_11111111u64;
        let hdr_hex = 0x80000080000000ffu64;

        println!("");
        println!("binary: {:b}", hdr_bin);
        println!("hex   : {:b}", hdr_hex);

        assert_eq!(hdr_bin, hdr_hex);

        let hdr = hdr_hex;

        assert!(header_is_object_start(hdr));
qinsoon's avatar
qinsoon committed
263
        assert!(!header_is_traced(hdr, 1u8));
264 265 266 267 268
        assert!(!header_is_fix_size(hdr));

        assert_eq!(header_get_hybrid_length(hdr), 128);
        assert_eq!(header_get_gctype_id(hdr), 0xff);
    }
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

    #[test]
    fn gctype_to_encode1() {
        // linked list: struct {ref, int64}
        let a = GCType{
            id: 0,
            size: 16,
            non_repeat_refs: Some(RefPattern::Map{
                offsets: vec![0],
                size: 16
            }),
            repeat_refs    : None
        };
        println!("gctype: {:?}", a);

        let encode = gen_gctype_encode(&a);
        println!("encode: {:64b}", encode);

        assert!(header_is_fix_size(encode));
qinsoon's avatar
qinsoon committed
288
        assert_eq!(header_get_object_size(encode), 16);
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
        assert_eq!(header_get_ref_map(encode), 0b1);
    }

    #[test]
    fn gctype_to_encode2() {
        // doubly linked list: struct {ref, ref, int64, int64}
        let a = GCType{
            id: 0,
            size: 32,
            non_repeat_refs: Some(RefPattern::Map{
                offsets: vec![0, 8],
                size: 32
            }),
            repeat_refs    : None
        };
        println!("gctype: {:?}", a);

        let encode = gen_gctype_encode(&a);
        println!("encode: {:64b}", encode);

        assert!(header_is_fix_size(encode));
qinsoon's avatar
qinsoon committed
310
        assert_eq!(header_get_object_size(encode), 32);
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
        assert_eq!(header_get_ref_map(encode), 0b11);
    }

    #[test]
    fn gctype_to_encode3() {
        // a struct of 64 references
        const N_REF : usize = 64;
        let a = GCType{
            id: 999,
            size: N_REF * POINTER_SIZE,
            non_repeat_refs: Some(RefPattern::Map{
                offsets: (0..N_REF).map(|x| x * POINTER_SIZE).collect(),
                size: N_REF * POINTER_SIZE
            }),
            repeat_refs    : None
        };
        println!("gctype: {:?}", a);

        let encode = gen_gctype_encode(&a);
        println!("encode: {:64b}", encode);

        assert!(header_is_fix_size(encode));
        assert_eq!(header_get_gctype_id(encode), 999);
    }

    #[test]
    fn gctype_to_encode4() {
        // array of struct {ref, int64} with length 10
        let a = GCType {
            id: 1,
            size: 160,
            non_repeat_refs: None,
            repeat_refs    : Some(RepeatingRefPattern {
                pattern: RefPattern::Map{
                    offsets: vec![0],
                    size   : 16
                },
                count  : 10
            }),
        };
        println!("gctype: {:?}", a);

        let encode = gen_gctype_encode(&a);
        println!("encode: {:64b}", encode);

        assert!(!header_is_fix_size(encode));
        assert_eq!(header_get_hybrid_length(encode), 10);
        assert_eq!(header_get_gctype_id(encode), 1);
    }

    #[test]
    fn gctype_to_encode5() {
        // array of struct {ref, int64} with length 10
        let b = GCType {
            id: 1,
            size: 160,
            non_repeat_refs: None,
            repeat_refs    : Some(RepeatingRefPattern {
                pattern: RefPattern::Map{
                    offsets: vec![0],
                    size   : 16
                },
                count  : 10
            }),
        };

        // array(10) of array(10) of struct {ref, int64}
        let a = GCType {
            id: 2,
            size: 1600,
            non_repeat_refs: None,
            repeat_refs    : Some(RepeatingRefPattern {
                pattern: RefPattern::NestedType(vec![Arc::new(b.clone()).clone()]),
                count  : 10
            })
        };
        println!("gctype: {:?}", a);

        let encode = gen_gctype_encode(&a);
        println!("encode: {:64b}", encode);

        assert!(!header_is_fix_size(encode));
        assert_eq!(header_get_hybrid_length(encode), 10);
        assert_eq!(header_get_gctype_id(encode), 2);
    }
396
}