Add parsing of 16-bit signed integers

This commit is contained in:
Andrew Brown
2019-09-18 13:45:26 -07:00
parent ba393afd4d
commit c932f9b2b5
2 changed files with 21 additions and 1 deletions

View File

@@ -22,6 +22,12 @@ impl IntoBytes for u8 {
} }
} }
impl IntoBytes for i16 {
fn into_bytes(self) -> Vec<u8> {
self.to_le_bytes().to_vec()
}
}
impl IntoBytes for i32 { impl IntoBytes for i32 {
fn into_bytes(self) -> Vec<u8> { fn into_bytes(self) -> Vec<u8> {
self.to_le_bytes().to_vec() self.to_le_bytes().to_vec()
@@ -429,6 +435,7 @@ impl FromIterator<bool> for V128Imm {
} }
construct_uimm128_from_iterator_of!(u8, 16); construct_uimm128_from_iterator_of!(u8, 16);
construct_uimm128_from_iterator_of!(i16, 8);
construct_uimm128_from_iterator_of!(i32, 4); construct_uimm128_from_iterator_of!(i32, 4);
construct_uimm128_from_iterator_of!(Ieee32, 4); construct_uimm128_from_iterator_of!(Ieee32, 4);
construct_uimm128_from_iterator_of!(Imm64, 2); construct_uimm128_from_iterator_of!(Imm64, 2);

View File

@@ -665,6 +665,19 @@ impl<'a> Parser<'a> {
} }
} }
// Match and consume a signed 16-bit immediate.
fn match_imm16(&mut self, err_msg: &str) -> ParseResult<i16> {
if let Some(Token::Integer(text)) = self.token() {
self.consume();
// Lexer just gives us raw text that looks like an integer.
// Parse it as a i16 to check for overflow and other issues.
text.parse()
.map_err(|_| self.error("expected i16 decimal immediate"))
} else {
err!(self.loc, err_msg)
}
}
// Match and consume an i32 immediate. // Match and consume an i32 immediate.
// This is used for stack argument byte offsets. // This is used for stack argument byte offsets.
fn match_imm32(&mut self, err_msg: &str) -> ParseResult<i32> { fn match_imm32(&mut self, err_msg: &str) -> ParseResult<i32> {
@@ -855,7 +868,7 @@ impl<'a> Parser<'a> {
} else { } else {
let uimm128 = match ty.lane_type() { let uimm128 = match ty.lane_type() {
I8 => consume!(ty, self.match_uimm8("Expected an 8-bit unsigned integer")), I8 => consume!(ty, self.match_uimm8("Expected an 8-bit unsigned integer")),
I16 => unimplemented!(), // TODO no 16-bit match yet I16 => consume!(ty, self.match_imm16("Expected a 16-bit integer")),
I32 => consume!(ty, self.match_imm32("Expected a 32-bit integer")), I32 => consume!(ty, self.match_imm32("Expected a 32-bit integer")),
I64 => consume!(ty, self.match_imm64("Expected a 64-bit integer")), I64 => consume!(ty, self.match_imm64("Expected a 64-bit integer")),
F32 => consume!(ty, self.match_ieee32("Expected a 32-bit float...")), F32 => consume!(ty, self.match_ieee32("Expected a 32-bit float...")),