PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/hack/src/hackc/hhbc/dump_opcodes.rs

https://github.com/soitun/hiphop-php
Rust | 70 lines | 52 code | 12 blank | 6 comment | 4 complexity | 9b5dfd6bc6868bbdb662e7ff1b2bd659 MD5 | raw file
  1. // Copyright (c) Facebook, Inc. and its affiliates.
  2. //
  3. // This source code is licensed under the MIT license found in the
  4. // LICENSE file in the "hack" directory of this source tree.
  5. use anyhow::Result;
  6. use hhbc_gen::OpcodeData;
  7. use quote::quote;
  8. use std::{
  9. fs::File,
  10. io::Write,
  11. path::PathBuf,
  12. process::{Command, Stdio},
  13. };
  14. use structopt::StructOpt;
  15. #[derive(StructOpt)]
  16. #[structopt(no_version)]
  17. struct Opts {
  18. #[structopt(short = "o", long = "out")]
  19. output: Option<PathBuf>,
  20. #[structopt(long)]
  21. no_format: bool,
  22. }
  23. fn main() -> Result<()> {
  24. let opts = Opts::from_args();
  25. let opcode_data: Vec<OpcodeData> = hhbc_gen::opcode_data().to_vec();
  26. let input = quote!(
  27. #[emit_opcodes_macro::emit_opcodes]
  28. #[derive(Clone, Debug, Targets)]
  29. #[repr(C)]
  30. pub enum Opcode<'arena> {
  31. // This is filled in by the emit_opcodes macro. It can be printed using the
  32. // "//hphp/hack/src/hackc/hhbc:dump-opcodes" binary.
  33. }
  34. );
  35. let opcodes = emit_opcodes::emit_opcodes(input.clone(), &opcode_data)?;
  36. let targets = emit_opcodes::emit_impl_targets(input, &opcode_data)?;
  37. let output = format!("{}\n\n{}", opcodes, targets);
  38. if opts.no_format {
  39. if let Some(out) = opts.output.as_ref() {
  40. std::fs::write(out, output)?;
  41. } else {
  42. println!("{}", output);
  43. }
  44. return Ok(());
  45. }
  46. let mut child = Command::new("rustfmt");
  47. child.args(["--emit", "stdout"]);
  48. child.stdin(Stdio::piped());
  49. if let Some(out) = opts.output.as_ref() {
  50. use std::os::unix::io::{FromRawFd, IntoRawFd};
  51. let file = File::create(out).expect("couldn't create output file");
  52. child.stdout(unsafe { Stdio::from_raw_fd(file.into_raw_fd()) });
  53. }
  54. let mut child = child.spawn()?;
  55. child.stdin.as_mut().unwrap().write_all(output.as_bytes())?;
  56. child.wait()?;
  57. Ok(())
  58. }