mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-10 20:19:07 +00:00
aml: Use the shortest possible form to generate integers
Per ACPI Machine Language (AML) Specification, all integers encodings are equally accepted; hence one can always the shortest possible forms. BUG=None TEST=./tools/presubmit Change-Id: I7f1802607e173d41d1ae9ba26a036e9bfcd95121 Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3878738 Tested-by: Victor Ding <victording@chromium.org> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org> Commit-Queue: Daniel Verkamp <dverkamp@chromium.org> Auto-Submit: Victor Ding <victording@chromium.org>
This commit is contained in:
parent
ef7e9109da
commit
df24522dcf
1 changed files with 96 additions and 25 deletions
|
@ -163,36 +163,54 @@ pub type Byte = u8;
|
||||||
|
|
||||||
impl Aml for Byte {
|
impl Aml for Byte {
|
||||||
fn to_aml_bytes(&self, bytes: &mut Vec<u8>) {
|
fn to_aml_bytes(&self, bytes: &mut Vec<u8>) {
|
||||||
|
match *self {
|
||||||
|
0 => ZERO.to_aml_bytes(bytes),
|
||||||
|
1 => ONE.to_aml_bytes(bytes),
|
||||||
|
_ => {
|
||||||
bytes.push(BYTEPREFIX);
|
bytes.push(BYTEPREFIX);
|
||||||
bytes.push(*self);
|
bytes.push(*self);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Word = u16;
|
pub type Word = u16;
|
||||||
|
|
||||||
impl Aml for Word {
|
impl Aml for Word {
|
||||||
fn to_aml_bytes(&self, bytes: &mut Vec<u8>) {
|
fn to_aml_bytes(&self, bytes: &mut Vec<u8>) {
|
||||||
|
if *self <= Byte::max_value().into() {
|
||||||
|
(*self as Byte).to_aml_bytes(bytes);
|
||||||
|
} else {
|
||||||
bytes.push(WORDPREFIX);
|
bytes.push(WORDPREFIX);
|
||||||
bytes.append(&mut self.to_le_bytes().to_vec());
|
bytes.append(&mut self.to_le_bytes().to_vec());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type DWord = u32;
|
pub type DWord = u32;
|
||||||
|
|
||||||
impl Aml for DWord {
|
impl Aml for DWord {
|
||||||
fn to_aml_bytes(&self, bytes: &mut Vec<u8>) {
|
fn to_aml_bytes(&self, bytes: &mut Vec<u8>) {
|
||||||
|
if *self <= Word::max_value().into() {
|
||||||
|
(*self as Word).to_aml_bytes(bytes);
|
||||||
|
} else {
|
||||||
bytes.push(DWORDPREFIX);
|
bytes.push(DWORDPREFIX);
|
||||||
bytes.append(&mut self.to_le_bytes().to_vec());
|
bytes.append(&mut self.to_le_bytes().to_vec());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type QWord = u64;
|
pub type QWord = u64;
|
||||||
|
|
||||||
impl Aml for QWord {
|
impl Aml for QWord {
|
||||||
fn to_aml_bytes(&self, bytes: &mut Vec<u8>) {
|
fn to_aml_bytes(&self, bytes: &mut Vec<u8>) {
|
||||||
|
if *self <= DWord::max_value().into() {
|
||||||
|
(*self as DWord).to_aml_bytes(bytes);
|
||||||
|
} else {
|
||||||
bytes.push(QWORDPREFIX);
|
bytes.push(QWORDPREFIX);
|
||||||
bytes.append(&mut self.to_le_bytes().to_vec());
|
bytes.append(&mut self.to_le_bytes().to_vec());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Name object. bytes represents the raw AML data for it.
|
/// Name object. bytes represents the raw AML data for it.
|
||||||
|
@ -347,27 +365,16 @@ impl Aml for EISAName {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_integer(v: usize, bytes: &mut Vec<u8>) {
|
|
||||||
if v == 0_usize {
|
|
||||||
ZERO.to_aml_bytes(bytes);
|
|
||||||
} else if v == 1_usize {
|
|
||||||
ONE.to_aml_bytes(bytes);
|
|
||||||
} else if v <= u8::max_value().into() {
|
|
||||||
(v as u8).to_aml_bytes(bytes);
|
|
||||||
} else if v <= u16::max_value().into() {
|
|
||||||
(v as u16).to_aml_bytes(bytes);
|
|
||||||
} else if v <= u32::max_value() as usize {
|
|
||||||
(v as u32).to_aml_bytes(bytes);
|
|
||||||
} else {
|
|
||||||
(v as u64).to_aml_bytes(bytes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type Usize = usize;
|
pub type Usize = usize;
|
||||||
|
|
||||||
impl Aml for Usize {
|
impl Aml for Usize {
|
||||||
fn to_aml_bytes(&self, bytes: &mut Vec<u8>) {
|
fn to_aml_bytes(&self, bytes: &mut Vec<u8>) {
|
||||||
create_integer(*self, bytes);
|
#[cfg(target_pointer_width = "16")]
|
||||||
|
(*self as u16).to_aml_bytes(bytes);
|
||||||
|
#[cfg(target_pointer_width = "32")]
|
||||||
|
(*self as u32).to_aml_bytes(bytes);
|
||||||
|
#[cfg(target_pointer_width = "64")]
|
||||||
|
(*self as u64).to_aml_bytes(bytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1739,6 +1746,70 @@ mod tests {
|
||||||
0xdeca_fbad_deca_fbadu64.to_aml_bytes(&mut aml);
|
0xdeca_fbad_deca_fbadu64.to_aml_bytes(&mut aml);
|
||||||
assert_eq!(aml, [0x0e, 0xad, 0xfb, 0xca, 0xde, 0xad, 0xfb, 0xca, 0xde]);
|
assert_eq!(aml, [0x0e, 0xad, 0xfb, 0xca, 0xde, 0xad, 0xfb, 0xca, 0xde]);
|
||||||
aml.clear();
|
aml.clear();
|
||||||
|
|
||||||
|
// u8
|
||||||
|
0x00_u8.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x00]);
|
||||||
|
aml.clear();
|
||||||
|
0x01_u8.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x01]);
|
||||||
|
aml.clear();
|
||||||
|
0x86_u8.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x0a, 0x86]);
|
||||||
|
aml.clear();
|
||||||
|
|
||||||
|
// u16
|
||||||
|
0x00_u16.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x00]);
|
||||||
|
aml.clear();
|
||||||
|
0x01_u16.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x01]);
|
||||||
|
aml.clear();
|
||||||
|
0x86_u16.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x0a, 0x86]);
|
||||||
|
aml.clear();
|
||||||
|
0xF00D_u16.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x0b, 0x0d, 0xf0]);
|
||||||
|
aml.clear();
|
||||||
|
|
||||||
|
// u32
|
||||||
|
0x00_u32.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x00]);
|
||||||
|
aml.clear();
|
||||||
|
0x01_u32.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x01]);
|
||||||
|
aml.clear();
|
||||||
|
0x86_u32.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x0a, 0x86]);
|
||||||
|
aml.clear();
|
||||||
|
0xF00D_u32.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x0b, 0x0d, 0xf0]);
|
||||||
|
aml.clear();
|
||||||
|
0xDECAF_u32.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x0c, 0xaf, 0xec, 0x0d, 0x00]);
|
||||||
|
aml.clear();
|
||||||
|
|
||||||
|
// u64
|
||||||
|
0x00_u64.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x00]);
|
||||||
|
aml.clear();
|
||||||
|
0x01_u64.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x01]);
|
||||||
|
aml.clear();
|
||||||
|
0x86_u64.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x0a, 0x86]);
|
||||||
|
aml.clear();
|
||||||
|
0xF00D_u64.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x0b, 0x0d, 0xf0]);
|
||||||
|
aml.clear();
|
||||||
|
0xDECAF_u64.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x0c, 0xaf, 0xec, 0x0d, 0x00]);
|
||||||
|
aml.clear();
|
||||||
|
0xDECAFC0FFEE_u64.to_aml_bytes(&mut aml);
|
||||||
|
assert_eq!(aml, [0x0e, 0xee, 0xff, 0xc0, 0xaf, 0xec, 0x0d, 0x00, 0x00]);
|
||||||
|
aml.clear();
|
||||||
|
|
||||||
|
// usize
|
||||||
0x00_usize.to_aml_bytes(&mut aml);
|
0x00_usize.to_aml_bytes(&mut aml);
|
||||||
assert_eq!(aml, [0x00]);
|
assert_eq!(aml, [0x00]);
|
||||||
aml.clear();
|
aml.clear();
|
||||||
|
|
Loading…
Reference in a new issue