|
1 | 1 | use crate::assembly::MethodCompileCtx; |
2 | 2 | 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, |
8 | 5 | }; |
9 | 6 | use rustc_codegen_clr_ctx::function_name; |
10 | 7 | use rustc_codegen_clr_place::{place_adress, place_set}; |
11 | 8 | use rustc_codegen_clr_type::GetTypeExt; |
| 9 | +use rustc_middle::mir::AssertKind; |
12 | 10 |
|
13 | 11 | use rustc_codgen_clr_operand::{ |
14 | 12 | constant::{load_const_int, load_const_uint}, |
@@ -134,18 +132,64 @@ pub fn handle_terminator<'tcx>( |
134 | 132 | TerminatorKind::Assert { |
135 | 133 | cond, |
136 | 134 | expected, |
137 | | - msg: _, |
| 135 | + msg, |
138 | 136 | target, |
139 | 137 | unwind: _, |
140 | 138 | } => { |
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 | + }; |
145 | 147 | // FIXME: propelrly handle *all* assertion messages. |
146 | 148 | 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 | + }; |
147 | 191 | 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![]); |
149 | 193 | vec![ |
150 | 194 | CILRoot::Call { |
151 | 195 | site, |
|
0 commit comments