Skip to content

Commit 8863a0a

Browse files
committed
Fix LEB128 to work with the stage1
Stage 1 can’t really handle negative 128-bit literals, but an equivalent bit-not is fine
1 parent dc73bed commit 8863a0a

File tree

3 files changed

+44
-47
lines changed

3 files changed

+44
-47
lines changed

src/librbml/leb128.rs

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,53 +58,50 @@ pub fn read_unsigned_leb128(data: &[u8], start_position: usize) -> (u128, usize)
5858
}
5959

6060

61-
pub fn write_signed_leb128(out: &mut Vec<u8>, start_position: usize, value: i128) -> usize {
62-
write_unsigned_leb128(out, start_position, value as u128)
63-
// let mut position = start_position;
64-
// loop {
65-
// let mut byte = (value & 0x7f) as u8;
66-
// value >>= 7;
67-
// let more = !((((value == 0) && ((byte & 0x40) == 0)) ||
68-
// ((value == -1) && ((byte & 0x40) != 0))));
69-
// if more {
70-
// byte |= 0x80; // Mark this byte to show that more bytes will follow.
71-
// }
72-
73-
// write_to_vec(out, &mut position, byte);
74-
75-
// if !more {
76-
// break;
77-
// }
78-
// }
79-
80-
// return position - start_position;
61+
pub fn write_signed_leb128(out: &mut Vec<u8>, start_position: usize, mut value: i128) -> usize {
62+
let mut position = start_position;
63+
loop {
64+
let mut byte = (value & 0x7f) as u8;
65+
value >>= 7;
66+
let more = !((((value == 0) && ((byte & 0x40) == 0)) ||
67+
((value == !0) && ((byte & 0x40) != 0))));
68+
if more {
69+
byte |= 0x80; // Mark this byte to show that more bytes will follow.
70+
}
71+
72+
write_to_vec(out, &mut position, byte);
73+
74+
if !more {
75+
break;
76+
}
77+
}
78+
79+
return position - start_position;
8180
}
8281

8382
pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i128, usize) {
84-
let (l, r) = read_unsigned_leb128(data, start_position);
85-
(l as i128, r)
86-
// let mut result = 0;
87-
// let mut shift = 0;
88-
// let mut position = start_position;
89-
// let mut byte;
90-
91-
// loop {
92-
// byte = data[position];
93-
// position += 1;
94-
// result |= ((byte & 0x7F) as i128) << shift;
95-
// shift += 7;
96-
97-
// if (byte & 0x80) == 0 {
98-
// break;
99-
// }
100-
// }
101-
102-
// if (shift < 64) && ((byte & 0x40) != 0) {
103-
// // sign extend
104-
// result |= -(1 << shift);
105-
// }
106-
107-
// (result, position - start_position)
83+
let mut result = 0;
84+
let mut shift = 0;
85+
let mut position = start_position;
86+
let mut byte;
87+
88+
loop {
89+
byte = data[position];
90+
position += 1;
91+
result |= ((byte & 0x7F) as i128) << shift;
92+
shift += 7;
93+
94+
if (byte & 0x80) == 0 {
95+
break;
96+
}
97+
}
98+
99+
if (shift < 64) && ((byte & 0x40) != 0) {
100+
// sign extend
101+
result |= -(1 << shift);
102+
}
103+
104+
(result, position - start_position)
108105
}
109106

110107
#[test]

src/librustc_i128/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(non_camel_case_types)]
2-
#![feature(i128_type)]
2+
#![cfg_attr(not(stage0), feature(i128_type))]
33

44
#[cfg(stage0)]
55
pub type i128 = i64;

src/libsyntax/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,8 +1203,8 @@ impl IntTy {
12031203
}
12041204

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

0 commit comments

Comments
 (0)