Skip to content

Commit

Permalink
Metadata parsing and indexing improvements (#2581)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jul 20, 2023
1 parent a98ae05 commit 3cb54c7
Show file tree
Hide file tree
Showing 488 changed files with 260,379 additions and 260,054 deletions.
20 changes: 19 additions & 1 deletion crates/libs/metadata/src/blob.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use std::convert::*;

pub struct Blob<'a> {
pub file: usize,
Expand All @@ -18,6 +17,7 @@ impl<'a> Blob<'a> {
pub fn new(file: usize, slice: &'a [u8]) -> Self {
Self { file, slice }
}

pub fn peek_usize(&self) -> (usize, usize) {
if self[0] & 0x80 == 0 {
(self[0] as usize, 1)
Expand All @@ -27,11 +27,13 @@ impl<'a> Blob<'a> {
((((self[0] & 0x1F) as usize) << 24) | (self[1] as usize) << 16 | (self[2] as usize) << 8 | self[3] as usize, 4)
}
}

pub fn read_usize(&mut self) -> usize {
let (value, offset) = self.peek_usize();
self.offset(offset);
value
}

pub fn read_expected(&mut self, expected: usize) -> bool {
let (value, offset) = self.peek_usize();
if value == expected {
Expand All @@ -41,6 +43,7 @@ impl<'a> Blob<'a> {
false
}
}

pub fn read_modifiers(&mut self) -> Vec<TypeDefOrRef> {
let mut mods = vec![];
loop {
Expand All @@ -54,12 +57,14 @@ impl<'a> Blob<'a> {
}
mods
}

pub fn read_str(&mut self) -> &str {
let len = self.read_usize();
let value = unsafe { std::str::from_utf8_unchecked(&self.slice[..len]) };
self.offset(len);
value
}

pub fn read_string(self) -> String {
let slice = self.slice;
if slice.as_ptr().align_offset(std::mem::align_of::<u16>()) > 0 {
Expand All @@ -70,6 +75,7 @@ impl<'a> Blob<'a> {
String::from_utf16_lossy(slice)
}
}

pub fn read_bool(&mut self) -> bool {
// A bool is specified as "a single byte with value 0 (false) or 1 (true)".
match self.read_u8() {
Expand All @@ -78,56 +84,67 @@ impl<'a> Blob<'a> {
_ => panic!("Illegal bool value"),
}
}

pub fn read_i8(&mut self) -> i8 {
let value = i8::from_le_bytes(self[..1].try_into().unwrap());
self.offset(1);
value
}

pub fn read_u8(&mut self) -> u8 {
let value = u8::from_le_bytes(self[..1].try_into().unwrap());
self.offset(1);
value
}

pub fn read_i16(&mut self) -> i16 {
let value = i16::from_le_bytes(self[..2].try_into().unwrap());
self.offset(2);
value
}

pub fn read_u16(&mut self) -> u16 {
let value = u16::from_le_bytes(self[..2].try_into().unwrap());
self.offset(2);
value
}

pub fn read_i32(&mut self) -> i32 {
let value = i32::from_le_bytes(self[..4].try_into().unwrap());
self.offset(4);
value
}

pub fn read_u32(&mut self) -> u32 {
let value = u32::from_le_bytes(self[..4].try_into().unwrap());
self.offset(4);
value
}

pub fn read_i64(&mut self) -> i64 {
let value = i64::from_le_bytes(self[..8].try_into().unwrap());
self.offset(8);
value
}

pub fn read_u64(&mut self) -> u64 {
let value = u64::from_le_bytes(self[..8].try_into().unwrap());
self.offset(8);
value
}

pub fn read_f32(&mut self) -> f32 {
let value = f32::from_le_bytes(self[..4].try_into().unwrap());
self.offset(4);
value
}

pub fn read_f64(&mut self) -> f64 {
let value = f64::from_le_bytes(self[..8].try_into().unwrap());
self.offset(8);
value
}

pub fn read_integer(&mut self, ty: Type) -> Value {
match ty {
Type::I8 => Value::I8(self.read_i8()),
Expand All @@ -141,6 +158,7 @@ impl<'a> Blob<'a> {
_ => panic!("Type is not an integer"),
}
}

fn offset(&mut self, offset: usize) {
self.slice = &self.slice[offset..];
}
Expand Down
20 changes: 10 additions & 10 deletions crates/libs/metadata/src/codes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

pub(crate) trait Decode {
pub trait Decode {
fn decode(file: usize, code: usize) -> Self;
}

Expand All @@ -12,7 +12,7 @@ impl Decode for AttributeType {
fn decode(file: usize, code: usize) -> Self {
let (kind, row) = (code & ((1 << 3) - 1), (code >> 3) - 1);
match kind {
3 => Self::MemberRef(MemberRef(Row::new(row, TABLE_MEMBERREF, file))),
3 => Self::MemberRef(MemberRef(Row::new(row, file))),
rest => unimplemented!("{rest:?}"),
}
}
Expand Down Expand Up @@ -80,7 +80,7 @@ impl Decode for MemberRefParent {
fn decode(file: usize, code: usize) -> Self {
let (kind, row) = (code & ((1 << 3) - 1), (code >> 3) - 1);
match kind {
1 => Self::TypeRef(TypeRef(Row::new(row, TABLE_TYPEREF, file))),
1 => Self::TypeRef(TypeRef(Row::new(row, file))),
rest => unimplemented!("{rest:?}"),
}
}
Expand All @@ -97,9 +97,9 @@ impl Decode for TypeDefOrRef {
fn decode(file: usize, code: usize) -> Self {
let (kind, row) = (code & ((1 << 2) - 1), (code >> 2) - 1);
match kind {
0 => Self::TypeDef(TypeDef(Row::new(row, TABLE_TYPEDEF, file))),
1 => Self::TypeRef(TypeRef(Row::new(row, TABLE_TYPEREF, file))),
2 => Self::TypeSpec(TypeSpec(Row::new(row, TABLE_TYPESPEC, file))),
0 => Self::TypeDef(TypeDef(Row::new(row, file))),
1 => Self::TypeRef(TypeRef(Row::new(row, file))),
2 => Self::TypeSpec(TypeSpec(Row::new(row, file))),
rest => unimplemented!("{rest:?}"),
}
}
Expand Down Expand Up @@ -128,10 +128,10 @@ impl Decode for ResolutionScope {
fn decode(file: usize, code: usize) -> Self {
let (kind, row) = (code & ((1 << 2) - 1), (code >> 2) - 1);
match kind {
0 => Self::Module(Module(Row::new(row, TABLE_MODULE, file))),
1 => Self::ModuleRef(ModuleRef(Row::new(row, TABLE_MODULEREF, file))),
2 => Self::AssemblyRef(AssemblyRef(Row::new(row, TABLE_ASSEMBLYREF, file))),
3 => Self::TypeRef(TypeRef(Row::new(row, TABLE_TYPEREF, file))),
0 => Self::Module(Module(Row::new(row, file))),
1 => Self::ModuleRef(ModuleRef(Row::new(row, file))),
2 => Self::AssemblyRef(AssemblyRef(Row::new(row, file))),
3 => Self::TypeRef(TypeRef(Row::new(row, file))),
rest => unimplemented!("{rest:?}"),
}
}
Expand Down
Loading

0 comments on commit 3cb54c7

Please sign in to comment.