message: Refactor top-level function logic
diff: @@ -288,23 +288,38 @@ fn compile_inst(
  }
  impl<M: Module> Translator<M> {
  - fn compile_func(&mut self, func: bril::Function) -> cranelift_module::FuncId {
  - // Build function signature.
  - let sig = tr_sig(&func);
  -
  - // TODO Probably move to a separate function.
  - let func_id = self
  + fn declare_func(&mut self, func: &bril::Function) -> cranelift_module::FuncId {
  + let sig = tr_sig(func);
  + self
  .module
  .declare_function(&func.name, cranelift_module::Linkage::Export, &sig)
  - .unwrap();
  + .unwrap()
  + }
  - // Create the function.
  + fn enter_func(&mut self, func: &bril::Function, func_id: cranelift_module::FuncId) {
  + let sig = tr_sig(func);
  self.context.func =
  ir::Function::with_name_signature(ir::ExternalName::user(0, func_id.as_u32()), sig);
  - let mut fn_builder_ctx = FunctionBuilderContext::new();
  + }
  - // Build the function body.
  - {
  + fn finish_func(&mut self, func_id: cranelift_module::FuncId) {
  + // Verify and print.
  + // TODO Make both optional...
  + let flags = settings::Flags::new(settings::builder());
  + let res = verify_function(&self.context.func, &flags);
  + println!("{}", self.context.func.display());
  + if let Err(errors) = res {
  + panic!("{}", errors);
  + }
  +
  + // Add to the module.
  + self.module
  + .define_function(func_id, &mut self.context)
  + .unwrap();
  + }
  +
  + fn emit_func(&mut self, func: bril::Function) {
  + let mut fn_builder_ctx = FunctionBuilderContext::new();
  let mut builder = FunctionBuilder::new(&mut self.context.func, &mut fn_builder_ctx);
  // Declare runtime functions.
  @@ -371,28 +386,16 @@ impl<M: Module> Translator<M> {
  builder.finalize();
  }
  - // Verify and print.
  - let flags = settings::Flags::new(settings::builder());
  - let res = verify_function(&self.context.func, &flags);
  - println!("{}", self.context.func.display());
  - if let Err(errors) = res {
  - panic!("{}", errors);
  - }
  -
  - // Add to the module.
  - // TODO Move to a separate function?
  - self.module
  - .define_function(func_id, &mut self.context)
  - .unwrap();
  -
  - func_id
  - }
  -
  fn compile_prog(&mut self, prog: bril::Program) {
  for func in prog.functions {
  - let name = func.name.to_owned();
  - let id = self.compile_func(func);
  - self.funcs.insert(name, id);
  + // Declare the function.
  + let id = self.declare_func(&func);
  + self.funcs.insert(func.name.to_owned(), id);
  +
  + // Define the function.
  + self.enter_func(&func, id);
  + self.emit_func(func);
  + self.finish_func(id);
  }
  }
  }
  