Skip to content

Commit c932f9b

Browse files
committed
Add parsing of 16-bit signed integers
1 parent ba393af commit c932f9b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

cranelift/codegen/src/ir/immediates.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ impl IntoBytes for u8 {
2222
}
2323
}
2424

25+
impl IntoBytes for i16 {
26+
fn into_bytes(self) -> Vec<u8> {
27+
self.to_le_bytes().to_vec()
28+
}
29+
}
30+
2531
impl IntoBytes for i32 {
2632
fn into_bytes(self) -> Vec<u8> {
2733
self.to_le_bytes().to_vec()
@@ -429,6 +435,7 @@ impl FromIterator<bool> for V128Imm {
429435
}
430436

431437
construct_uimm128_from_iterator_of!(u8, 16);
438+
construct_uimm128_from_iterator_of!(i16, 8);
432439
construct_uimm128_from_iterator_of!(i32, 4);
433440
construct_uimm128_from_iterator_of!(Ieee32, 4);
434441
construct_uimm128_from_iterator_of!(Imm64, 2);

cranelift/reader/src/parser.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,19 @@ impl<'a> Parser<'a> {
665665
}
666666
}
667667

668+
// Match and consume a signed 16-bit immediate.
669+
fn match_imm16(&mut self, err_msg: &str) -> ParseResult<i16> {
670+
if let Some(Token::Integer(text)) = self.token() {
671+
self.consume();
672+
// Lexer just gives us raw text that looks like an integer.
673+
// Parse it as a i16 to check for overflow and other issues.
674+
text.parse()
675+
.map_err(|_| self.error("expected i16 decimal immediate"))
676+
} else {
677+
err!(self.loc, err_msg)
678+
}
679+
}
680+
668681
// Match and consume an i32 immediate.
669682
// This is used for stack argument byte offsets.
670683
fn match_imm32(&mut self, err_msg: &str) -> ParseResult<i32> {
@@ -855,7 +868,7 @@ impl<'a> Parser<'a> {
855868
} else {
856869
let uimm128 = match ty.lane_type() {
857870
I8 => consume!(ty, self.match_uimm8("Expected an 8-bit unsigned integer")),
858-
I16 => unimplemented!(), // TODO no 16-bit match yet
871+
I16 => consume!(ty, self.match_imm16("Expected a 16-bit integer")),
859872
I32 => consume!(ty, self.match_imm32("Expected a 32-bit integer")),
860873
I64 => consume!(ty, self.match_imm64("Expected a 64-bit integer")),
861874
F32 => consume!(ty, self.match_ieee32("Expected a 32-bit float...")),

0 commit comments

Comments
 (0)