Fix LEB128 to work with the stage1

Stage 1 can’t really handle negative 128-bit literals, but an equivalent bit-not is fine
This commit is contained in:
Simonas Kazlauskas 2016-08-24 15:58:40 +03:00 committed by est31
parent 4e2b946e65
commit 64de4e2731
3 changed files with 23 additions and 23 deletions

View File

@ -1,5 +1,5 @@
#![allow(non_camel_case_types)]
#![feature(i128_type)]
#![cfg_attr(not(stage0), feature(i128_type))]
#[cfg(stage0)]
pub type i128 = i64;

View File

@ -87,12 +87,14 @@ pub fn write_signed_leb128_to<W>(mut value: i128, mut write: W) -> usize
value >>= 7;
let more = !((((value == 0) && ((byte & 0x40) == 0)) ||
((value == -1) && ((byte & 0x40) != 0))));
if more {
byte |= 0x80; // Mark this byte to show that more bytes will follow.
}
write(position, byte);
position += 1;
if !more {
break;
}
@ -106,30 +108,28 @@ pub fn write_signed_leb128(out: &mut Vec<u8>, start_position: usize, value: i128
#[inline]
pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i128, usize) {
let (l, r) = read_unsigned_leb128(data, start_position);
(l as i128, r)
// let mut result = 0;
// let mut shift = 0;
// let mut position = start_position;
// let mut byte;
let mut result = 0;
let mut shift = 0;
let mut position = start_position;
let mut byte;
// loop {
// byte = data[position];
// position += 1;
// result |= ((byte & 0x7F) as i128) << shift;
// shift += 7;
loop {
byte = data[position];
position += 1;
result |= ((byte & 0x7F) as i128) << shift;
shift += 7;
// if (byte & 0x80) == 0 {
// break;
// }
// }
if (byte & 0x80) == 0 {
break;
}
}
// if (shift < 64) && ((byte & 0x40) != 0) {
// // sign extend
// result |= -(1 << shift);
// }
if (shift < 64) && ((byte & 0x40) != 0) {
// sign extend
result |= -(1 << shift);
}
// (result, position - start_position)
(result, position - start_position)
}
#[test]

View File

@ -1201,8 +1201,8 @@ impl IntTy {
}
pub fn val_to_string(&self, val: i128) -> String {
// cast to a u64 so we can correctly print INT64_MIN. All integral types
// are parsed as u64, so we wouldn't want to print an extra negative
// cast to a u128 so we can correctly print INT128_MIN. All integral types
// are parsed as u128, so we wouldn't want to print an extra negative
// sign.
format!("{}{}", val as u128, self.ty_to_string())
}