Skip to content

Commit f8cecd3

Browse files
authored
Merge pull request #23 from antoyo/feature/bitcast
Implement bitcast
2 parents 841ffe6 + a53c607 commit f8cecd3

File tree

3 files changed

+26
-123
lines changed

3 files changed

+26
-123
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gcc-test-backend/src/main.rs

Lines changed: 23 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,29 @@ fn main() {
77
/*assert_eq!((0.0 as f64).min(0.0), 0.0);
88
assert!((0.0 as f64).min(0.0).is_sign_positive());
99
assert_eq!((-0.0 as f64).min(-0.0), -0.0);
10-
assert!((-0.0 as f64).min(-0.0).is_sign_negative());
10+
11+
fn is_sign_neg(num: f64) -> bool {
12+
num.to_bits() & 0x8000_0000_0000_0000 != 0
13+
}
14+
//assert!(is_sign_neg(-0.0_f64));
15+
println!("{:b}", (-0.0_f64).to_bits());
16+
let var = 1.2_f64;
17+
println!("{}", 1.2_f64 == var);*/
18+
19+
/*fn my_float() -> f64 {
20+
-0.0
21+
}
22+
23+
let float = my_float();*/
24+
//println!("{}", (-0.0_f64).to_bits());
25+
26+
// FIXME: seems like the asm is calling comisd which seems to convert -0.0 to 0.
27+
assert!((-0.0_f64).to_bits() & 0x8000_0000_0000_0000 != 0);
28+
println!("1");
29+
//println!("{}", float);
30+
//std::process::exit(float as i32)
31+
32+
/*assert!((-0.0 as f64).min(-0.0).is_sign_negative());
1133
assert_eq!((9.0 as f64).min(9.0), 9.0);
1234
assert_eq!((-9.0 as f64).min(0.0), -9.0);
1335
assert_eq!((0.0 as f64).min(9.0), 0.0);
@@ -192,105 +214,4 @@ fn main() {
192214
assert_eq!(r.saturating_pow(2), 4 as i128);
193215
assert_eq!(r.saturating_pow(3), -8 as i128);
194216
assert_eq!(r.saturating_pow(0), 1 as i128);*/
195-
196-
/*let res = 10_i64.checked_div(2);
197-
if res == Some(5) {
198-
println!("1");
199-
}*/
200-
201-
fn equal(num1: &Option<i64>, num2: &Option<i64>) -> bool {
202-
match (num1, num2) {
203-
(Some(num1), Some(num2)) => num1 == num2,
204-
(None, None) => true,
205-
_ => false
206-
}
207-
}
208-
209-
fn equal2(num1: &Option<i128>, num2: &Option<i128>) -> bool {
210-
match (num1, num2) {
211-
(Some(num1), Some(num2)) => num1 == num2,
212-
(None, None) => true,
213-
_ => false
214-
}
215-
}
216-
217-
/*let res2 = Some(10_i64 / 2);
218-
// NOTE: (local variable case) res2 is initialized as:
219-
// first 8 bits: 1
220-
// next 8 bits : 5
221-
//
222-
// (global variable case) Some(5) is initialized as:
223-
// first 8 bits: 1
224-
// next 8 bits : 5
225-
if equal(&res2, &Some(5)) {
226-
//println!("Equal: {}", res2 == Some(5));
227-
std::process::exit(1);
228-
}*/
229-
230-
// TODO: this seems to confirm that align(i128) == 16.
231-
#[repr(C)]
232-
struct int_option {
233-
is_some: bool,
234-
val: i128,
235-
}
236-
237-
fn equal3(int1: &int_option, int2: &int_option) -> bool {
238-
int1.is_some == int2.is_some &&
239-
int1.val == int2.val
240-
}
241-
242-
let res = int_option {
243-
is_some: true,
244-
val: 10 / 2,
245-
};
246-
247-
let expected = int_option {
248-
is_some: true,
249-
val: 5,
250-
};
251-
252-
/*if equal3(&expected, &res) {
253-
std::process::exit(50);
254-
}*/
255-
256-
// FIXME: &Option<i128> does not work while Option<i128> does.
257-
let res2 = Some(10_i128 / 2);
258-
// NOTE: (local variable case) res2 is initialized as:
259-
// first 16 bits: 1 (garbage in the last 8 bits)
260-
// next 8 bits : 5
261-
// next 8 bits : 0
262-
//
263-
// FIXME: maybe there's an alignment problem (i.e. the i128 wants to be aligned on 128-bits?).
264-
//
265-
// (global variable case) Some(5) is initialized as:
266-
// first 8 bits: 1
267-
// next 8 bits : 5
268-
// next 8 bits : 0
269-
//
270-
//let res1 = Some(5); // NOTE: It works when putting the value in a variable.
271-
// NOTE: the case that doesn't work uses a global variable, which might mean we don't
272-
// initialize them correctly.
273-
if equal2(&Some(5), &res2) {
274-
//println!("Equal: {}", res2 == Some(5));
275-
std::process::exit(42);
276-
}
277-
278-
/*let res = 10_i128.checked_div(2);
279-
if res == Some(5) {
280-
println!("2");
281-
}
282-
if let Some(val) = res {
283-
println!("Val: {}", val);
284-
}*/
285-
286-
/*let res = 5_i128.checked_div(0);
287-
if let Some(val) = res {
288-
println!("Val: {}", val);
289-
}
290-
291-
assert!((10 as i128).checked_div(2) == Some(5));
292-
assert!((5 as i128).checked_div(0) == None);*/
293-
294-
/*assert_eq!((10 as i128).checked_div(2), Some(5));
295-
assert_eq!((5 as i128).checked_div(0), None);*/
296217
}

src/consts.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,7 @@ use crate::type_of::LayoutGccExt;
2020

2121
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
2222
pub fn const_bitcast(&self, value: RValue<'gcc>, typ: Type<'gcc>) -> RValue<'gcc> {
23-
self.context.new_cast(None, value, typ)
24-
// FIXME: use a real bitcast.
25-
/*
26-
//println!("bitcast: {:?} -> {:?}", value, dest_ty);
27-
if type_is_pointer(value.get_type()) && type_is_pointer(typ) {
28-
return self.context.new_cast(None, value, typ);
29-
}
30-
31-
let func = self.current_func.borrow().expect("current func");
32-
let variable = func.new_local(None, value.get_type(), "pointerCastVar");
33-
// FIXME: we might not be in a function here, so we cannot create a variable.
34-
// Use a global? Where to init it, though? Maybe where this function is called (static
35-
// creation).
36-
self.current_block.borrow().expect("current block").add_assignment(None, variable, value);
37-
let pointer = variable.get_address(None);
38-
self.context.new_cast(None, pointer, typ.make_pointer())
39-
.dereference(None)
40-
.to_rvalue()
41-
*/
23+
self.context.new_bitcast(None, value, typ)
4224
}
4325
}
4426

0 commit comments

Comments
 (0)