| 17 | use wasmtime_environ::demangle_function_name; |
| 18 | |
| 19 | pub fn generate( |
| 20 | config: &wasmtime::Config, |
| 21 | target: Option<&str>, |
| 22 | clif_dir: Option<&Path>, |
| 23 | wasm: &[u8], |
| 24 | dest: &mut dyn Write, |
| 25 | ) -> Result<()> { |
| 26 | let target = match target { |
| 27 | None => target_lexicon::Triple::host(), |
| 28 | Some(target) => target_lexicon::Triple::from_str(target)?, |
| 29 | }; |
| 30 | |
| 31 | let wat = annotate_wat(wasm)?; |
| 32 | let wat_json = serde_json::to_string(&wat)?; |
| 33 | let asm = annotate_asm(config, &target, wasm)?; |
| 34 | let asm_json = serde_json::to_string(&asm)?; |
| 35 | let clif_json = clif_dir |
| 36 | .map::<wasmtime::Result<String>, _>(|clif_dir| { |
| 37 | let clif = annotate_clif(clif_dir, &asm)?; |
| 38 | Ok(serde_json::to_string(&clif)?) |
| 39 | }) |
| 40 | .transpose()?; |
| 41 | |
| 42 | let index_css = include_str!("./index.css"); |
| 43 | let index_js = include_str!("./index.js"); |
| 44 | |
| 45 | write!( |
| 46 | dest, |
| 47 | r#" |
| 48 | <!DOCTYPE html> |
| 49 | <html> |
| 50 | <head> |
| 51 | <title>Wasmtime Compiler Explorer</title> |
| 52 | <style> |
| 53 | {index_css} |
| 54 | </style> |
| 55 | </head> |
| 56 | <body class="hbox"> |
| 57 | <pre id="wat"></pre> |
| 58 | "# |
| 59 | )?; |
| 60 | if clif_json.is_some() { |
| 61 | write!(dest, r#"<div id="clif"></div>"#)?; |
| 62 | } |
| 63 | write!( |
| 64 | dest, |
| 65 | r#" |
| 66 | <div id="asm"></div> |
| 67 | <script> |
| 68 | window.WAT = {wat_json}; |
| 69 | "# |
| 70 | )?; |
| 71 | if let Some(clif_json) = clif_json { |
| 72 | write!( |
| 73 | dest, |
| 74 | r#" |
| 75 | window.CLIF = {clif_json}; |
| 76 | "# |