]> git.lizzy.rs Git - rust.git/blobdiff - src/pretty_clif.rs
Use the new cranelift-module interface
[rust.git] / src / pretty_clif.rs
index 64346c423c8af6732cbf459f13101b2790679183..e99bec45a5efed5e7ddb88b7b63d78d4ae62d43d 100644 (file)
@@ -1,3 +1,57 @@
+//! This module provides the [CommentWriter] which makes it possible
+//! to add comments to the written cranelift ir.
+//!
+//! # Example
+//!
+//! ```clif
+//! test compile
+//! target x86_64
+//!
+//! function u0:0(i64, i64, i64) system_v {
+//! ; symbol _ZN119_$LT$example..IsNotEmpty$u20$as$u20$mini_core..FnOnce$LT$$LP$$RF$$u27$a$u20$$RF$$u27$b$u20$$u5b$u16$u5d$$C$$RP$$GT$$GT$9call_once17he85059d5e6a760a0E
+//! ; instance Instance { def: Item(DefId(0/0:29 ~ example[8787]::{{impl}}[0]::call_once[0])), substs: [ReErased, ReErased] }
+//! ; sig ([IsNotEmpty, (&&[u16],)]; c_variadic: false)->(u8, u8)
+//!
+//! ; ssa {_2: NOT_SSA, _4: NOT_SSA, _0: NOT_SSA, _3: (empty), _1: NOT_SSA}
+//! ; msg   loc.idx    param    pass mode            ssa flags  ty
+//! ; ret    _0      = v0       ByRef                NOT_SSA    (u8, u8)
+//! ; arg    _1      = v1       ByRef                NOT_SSA    IsNotEmpty
+//! ; arg    _2.0    = v2       ByVal(types::I64)    NOT_SSA    &&[u16]
+//!
+//!     ss0 = explicit_slot 0 ; _1: IsNotEmpty size=0 align=1,8
+//!     ss1 = explicit_slot 8 ; _2: (&&[u16],) size=8 align=8,8
+//!     ss2 = explicit_slot 8 ; _4: (&&[u16],) size=8 align=8,8
+//!     sig0 = (i64, i64, i64) system_v
+//!     sig1 = (i64, i64, i64) system_v
+//!     fn0 = colocated u0:6 sig1 ; Instance { def: Item(DefId(0/0:31 ~ example[8787]::{{impl}}[1]::call_mut[0])), substs: [ReErased, ReErased] }
+//!
+//! block0(v0: i64, v1: i64, v2: i64):
+//!     v3 = stack_addr.i64 ss0
+//!     v4 = stack_addr.i64 ss1
+//!     store v2, v4
+//!     v5 = stack_addr.i64 ss2
+//!     jump block1
+//!
+//! block1:
+//!     nop
+//! ; _3 = &mut _1
+//! ; _4 = _2
+//!     v6 = load.i64 v4
+//!     store v6, v5
+//! ;
+//! ; _0 = const mini_core::FnMut::call_mut(move _3, move _4)
+//!     v7 = load.i64 v5
+//!     call fn0(v0, v3, v7)
+//!     jump block2
+//!
+//! block2:
+//!     nop
+//! ;
+//! ; return
+//!     return
+//! }
+//! ```
+
 use std::fmt;
 
 use cranelift_codegen::{
 
 use crate::prelude::*;
 
-/// This module provides the [CommentWriter] which makes it possible
-/// to add comments to the written cranelift ir.
-///
-/// # Example
-///
-/// ```clif
-/// test compile
-/// target x86_64
-///
-/// function u0:0(i64, i64, i64) system_v {
-/// ; symbol _ZN119_$LT$example..IsNotEmpty$u20$as$u20$mini_core..FnOnce$LT$$LP$$RF$$u27$a$u20$$RF$$u27$b$u20$$u5b$u16$u5d$$C$$RP$$GT$$GT$9call_once17he85059d5e6a760a0E
-/// ; instance Instance { def: Item(DefId(0/0:29 ~ example[8787]::{{impl}}[0]::call_once[0])), substs: [ReErased, ReErased] }
-/// ; sig ([IsNotEmpty, (&&[u16],)]; c_variadic: false)->(u8, u8)
-///
-/// ; ssa {_2: NOT_SSA, _4: NOT_SSA, _0: NOT_SSA, _3: (empty), _1: NOT_SSA}
-/// ; msg   loc.idx    param    pass mode            ssa flags  ty
-/// ; ret    _0      = v0       ByRef                NOT_SSA    (u8, u8)
-/// ; arg    _1      = v1       ByRef                NOT_SSA    IsNotEmpty
-/// ; arg    _2.0    = v2       ByVal(types::I64)    NOT_SSA    &&[u16]
-///
-///     ss0 = explicit_slot 0 ; _1: IsNotEmpty size=0 align=1,8
-///     ss1 = explicit_slot 8 ; _2: (&&[u16],) size=8 align=8,8
-///     ss2 = explicit_slot 8 ; _4: (&&[u16],) size=8 align=8,8
-///     sig0 = (i64, i64, i64) system_v
-///     sig1 = (i64, i64, i64) system_v
-///     fn0 = colocated u0:6 sig1 ; Instance { def: Item(DefId(0/0:31 ~ example[8787]::{{impl}}[1]::call_mut[0])), substs: [ReErased, ReErased] }
-///
-/// block0(v0: i64, v1: i64, v2: i64):
-///     v3 = stack_addr.i64 ss0
-///     v4 = stack_addr.i64 ss1
-///     store v2, v4
-///     v5 = stack_addr.i64 ss2
-///     jump block1
-///
-/// block1:
-///     nop
-/// ; _3 = &mut _1
-/// ; _4 = _2
-///     v6 = load.i64 v4
-///     store v6, v5
-/// ;
-/// ; _0 = const mini_core::FnMut::call_mut(move _3, move _4)
-///     v7 = load.i64 v5
-///     call fn0(v0, v3, v7)
-///     jump block2
-///
-/// block2:
-///     nop
-/// ;
-/// ; return
-///     return
-/// }
-/// ```
-
 #[derive(Debug)]
 pub(crate) struct CommentWriter {
     global_comments: Vec<String>,
@@ -186,7 +186,7 @@ fn write_instruction(
 }
 
 #[cfg(debug_assertions)]
-impl<B: Backend + 'static> FunctionCx<'_, '_, B> {
+impl<M: Module> FunctionCx<'_, '_, M> {
     pub(crate) fn add_global_comment<S: Into<String>>(&mut self, comment: S) {
         self.clif_comments.add_global_comment(comment);
     }
@@ -196,7 +196,7 @@ pub(crate) fn add_comment<S: Into<String> + AsRef<str>, E: Into<AnyEntity>>(
         entity: E,
         comment: S,
     ) {
-       self.clif_comments.add_comment(entity, comment);
+        self.clif_comments.add_comment(entity, comment);
     }
 }
 
@@ -210,7 +210,13 @@ pub(crate) fn write_clif_file<'tcx>(
 ) {
     use std::io::Write;
 
-    if !cfg!(debug_assertions) && !tcx.sess.opts.output_types.contains_key(&OutputType::LlvmAssembly) {
+    if !cfg!(debug_assertions)
+        && !tcx
+            .sess
+            .opts
+            .output_types
+            .contains_key(&OutputType::LlvmAssembly)
+    {
         return;
     }
 
@@ -243,23 +249,22 @@ pub(crate) fn write_clif_file<'tcx>(
     )
     .unwrap();
 
-    match ::std::fs::File::create(clif_file_name) {
-        Ok(mut file) => {
-            let target_triple = crate::target_triple(tcx.sess);
-            writeln!(file, "test compile").unwrap();
-            writeln!(file, "set is_pic").unwrap();
-            writeln!(file, "set enable_simd").unwrap();
-            writeln!(file, "target {} haswell", target_triple).unwrap();
-            writeln!(file, "").unwrap();
-            file.write(clif.as_bytes()).unwrap();
-        }
-        Err(e) => {
-            tcx.sess.warn(&format!("err opening clif file: {:?}", e));
-        }
+    let res: std::io::Result<()> = try {
+        let mut file = std::fs::File::create(clif_file_name)?;
+        let target_triple = crate::target_triple(tcx.sess);
+        writeln!(file, "test compile")?;
+        writeln!(file, "set is_pic")?;
+        writeln!(file, "set enable_simd")?;
+        writeln!(file, "target {} haswell", target_triple)?;
+        writeln!(file, "")?;
+        file.write(clif.as_bytes())?;
+    };
+    if let Err(err) = res {
+        tcx.sess.warn(&format!("err writing clif file: {}", err));
     }
 }
 
-impl<B: Backend + 'static> fmt::Debug for FunctionCx<'_, '_, B> {
+impl<M: Module> fmt::Debug for FunctionCx<'_, '_, M> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         writeln!(f, "{:?}", self.instance.substs)?;
         writeln!(f, "{:?}", self.local_map)?;