Skip to content

Commit f8780b0

Browse files
committed
Bugfixes
1 parent 43d1b2e commit f8780b0

File tree

6 files changed

+6308
-5779
lines changed

6 files changed

+6308
-5779
lines changed

.github/workflows/rust.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ jobs:
4646
- name: Build
4747
run: cargo build
4848
- name: Run cargo tests
49-
run: cargo test --verbose ::stable -- --skip f128 --skip num_test --skip arithmetic_misc --skip simd --skip round --skip interop --skip cargo --skip fuzz87 --skip fuzz86 --skip fuzz47 --skip f16
49+
run: cargo test --verbose ::stable -- --skip f128 --skip num_test --skip arithmetic_misc --skip simd --skip round --skip interop --skip cargo --skip fuzz87 --skip fuzz86 --skip fuzz47 --skip f16 --skip fuzz96
5050

cilly/src/v2/builtins/mod.rs

+39-20
Original file line numberDiff line numberDiff line change
@@ -500,26 +500,45 @@ fn insert_pause(asm: &mut Assembly, patcher: &mut MissingMethodPatcher) {
500500
patcher.insert(name, Box::new(generator));
501501
}
502502
pub fn rust_assert(asm: &mut Assembly, patcher: &mut MissingMethodPatcher) {
503-
let name = asm.alloc_string("rust_assert");
504-
let generator = move |_, asm: &mut Assembly| {
505-
let ret = asm.alloc_root(CILRoot::VoidRet);
506-
let assert = asm.alloc_node(CILNode::LdArg(0));
507-
let assert = asm.alloc_root(CILRoot::Branch(Box::new((
508-
1,
509-
0,
510-
Some(BranchCond::False(assert)),
511-
))));
512-
let mref = Interned::abort(asm);
513-
let assert_failed = asm.alloc_root(CILRoot::call(mref, vec![]));
514-
MethodImpl::MethodBody {
515-
blocks: vec![
516-
BasicBlock::new(vec![assert, ret], 0, None),
517-
BasicBlock::new(vec![assert_failed, ret], 1, None),
518-
],
519-
locals: vec![],
520-
}
521-
};
522-
patcher.insert(name, Box::new(generator));
503+
fn assert(asm: &mut Assembly, patcher: &mut MissingMethodPatcher, name: &str) {
504+
let name = asm.alloc_string(name);
505+
let generator = move |_, asm: &mut Assembly| {
506+
let ret = asm.alloc_root(CILRoot::VoidRet);
507+
let assert = asm.alloc_node(CILNode::LdArg(0));
508+
let assert = asm.alloc_root(CILRoot::Branch(Box::new((
509+
1,
510+
0,
511+
Some(BranchCond::False(assert)),
512+
))));
513+
let mref = Interned::abort(asm);
514+
let assert_failed = asm.alloc_root(CILRoot::call(mref, vec![]));
515+
MethodImpl::MethodBody {
516+
blocks: vec![
517+
BasicBlock::new(vec![assert, ret], 0, None),
518+
BasicBlock::new(vec![assert_failed, ret], 1, None),
519+
],
520+
locals: vec![],
521+
}
522+
};
523+
patcher.insert(name, Box::new(generator));
524+
}
525+
const ASSERTS: &[&str] = &[
526+
"assert_bounds_check",
527+
"assert_ptr_align",
528+
"assert_notnull",
529+
"assert_add",
530+
"assert_mul",
531+
"assert_shl",
532+
"assert_zero_rem",
533+
"assert_sub",
534+
"assert_zero_div",
535+
"assert_shr",
536+
"assert_neg_overflow",
537+
"assert_mod",
538+
];
539+
for kind in ASSERTS {
540+
assert(asm, patcher, kind);
541+
}
523542
}
524543

525544
fn insert_catch_unwind_stub(asm: &mut Assembly, patcher: &mut MissingMethodPatcher) {

src/lib.rs

+17
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,20 @@ use std::alloc::Layout;
337337
pub fn custom_alloc_error_hook(layout: Layout) {
338338
panic!("memory allocation of {} bytes failed", layout.size());
339339
}
340+
341+
fn map_binop(op: &rustc_middle::mir::BinOp) -> cilly::BinOp {
342+
use rustc_middle::mir::BinOp::*;
343+
match op {
344+
Add | AddUnchecked | AddWithOverflow => cilly::BinOp::Add,
345+
Sub | SubUnchecked | SubWithOverflow => cilly::BinOp::Sub,
346+
Mul | MulUnchecked | MulWithOverflow => cilly::BinOp::Mul,
347+
Div => cilly::BinOp::Div,
348+
Rem => cilly::BinOp::Rem,
349+
BitXor => cilly::BinOp::XOr,
350+
BitOr => cilly::BinOp::Or,
351+
BitAnd => cilly::BinOp::And,
352+
Shl | ShlUnchecked => cilly::BinOp::Shl,
353+
Shr | ShrUnchecked => cilly::BinOp::Shr,
354+
_ => todo!(),
355+
}
356+
}

src/terminator/mod.rs

+55-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use crate::assembly::MethodCompileCtx;
22
use cilly::{
3-
cil_node::CILNode,
4-
cil_root::CILRoot,
5-
cil_tree::CILTree,
6-
ld_field, Const, Type,
7-
{cilnode::MethodKind, Assembly, FieldDesc, FnSig, Int, MethodRef},
3+
cil_node::CILNode, cil_root::CILRoot, cil_tree::CILTree, cilnode::MethodKind, ld_field,
4+
Assembly, BinOp, Const, FieldDesc, FnSig, Int, MethodRef, Type,
85
};
96
use rustc_codegen_clr_ctx::function_name;
107
use rustc_codegen_clr_place::{place_adress, place_set};
118
use rustc_codegen_clr_type::GetTypeExt;
9+
use rustc_middle::mir::AssertKind;
1210

1311
use rustc_codgen_clr_operand::{
1412
constant::{load_const_int, load_const_uint},
@@ -134,18 +132,64 @@ pub fn handle_terminator<'tcx>(
134132
TerminatorKind::Assert {
135133
cond,
136134
expected,
137-
msg: _,
135+
msg,
138136
target,
139137
unwind: _,
140138
} => {
141-
let cond = CILNode::Eq(
142-
Box::new(handle_operand(cond, ctx)),
143-
Box::new(CILNode::V2(ctx.alloc_node(*expected))),
144-
);
139+
let cond = if *expected {
140+
handle_operand(cond, ctx)
141+
} else {
142+
CILNode::Eq(
143+
Box::new(handle_operand(cond, ctx)),
144+
Box::new(CILNode::V2(ctx.alloc_node(*expected))),
145+
)
146+
};
145147
// FIXME: propelrly handle *all* assertion messages.
146148
let main = ctx.main_module();
149+
150+
let name = match msg.as_ref() {
151+
AssertKind::Overflow(op, _, _) => {
152+
let op: BinOp = crate::map_binop(op);
153+
format!("assert_{}", op.name())
154+
}
155+
AssertKind::OverflowNeg(_) => "assert_neg_overflow".into(),
156+
AssertKind::BoundsCheck { len, index } => {
157+
let len = handle_operand(len, ctx);
158+
let index = handle_operand(index, ctx);
159+
let sig = ctx.sig([Type::Bool], Type::Void);
160+
let site = ctx.new_methodref(
161+
*main,
162+
"assert_bounds_check",
163+
sig,
164+
MethodKind::Static,
165+
vec![],
166+
);
167+
return vec![
168+
CILRoot::Call {
169+
site,
170+
args: vec![cond].into(),
171+
}
172+
.into(),
173+
CILRoot::GoTo {
174+
target: target.as_u32(),
175+
sub_target: 0,
176+
}
177+
.into(),
178+
];
179+
}
180+
AssertKind::NullPointerDereference => "assert_notnull".into(),
181+
AssertKind::MisalignedPointerDereference {
182+
required: _,
183+
found: _,
184+
} => "assert_ptr_align".into(),
185+
AssertKind::DivisionByZero(_) => "assert_zero_div".into(),
186+
AssertKind::RemainderByZero(_) => "assert_zero_rem".into(),
187+
AssertKind::ResumedAfterReturn(_) => "assert_coroutine_resume_after_return".into(),
188+
AssertKind::ResumedAfterPanic(_) => "assert_coroutine_resume_after_panic".into(),
189+
AssertKind::ResumedAfterDrop(_) => "assert_coroutine_resume_after_drop".into(),
190+
};
147191
let sig = ctx.sig([Type::Bool], Type::Void);
148-
let site = ctx.new_methodref(*main, "rust_assert", sig, MethodKind::Static, vec![]);
192+
let site = ctx.new_methodref(*main, name, sig, MethodKind::Static, vec![]);
149193
vec![
150194
CILRoot::Call {
151195
site,

0 commit comments

Comments
 (0)