From 4a4ce53c7e47b93ccb171bdaaac3833d45dd7ef8 Mon Sep 17 00:00:00 2001 From: Paul Schoenfelder Date: Sat, 16 Sep 2023 15:15:59 -0400 Subject: [PATCH] feat: implement formatting of data segments, globals --- hir/src/module.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/hir/src/module.rs b/hir/src/module.rs index e3102a717..079c9bc12 100644 --- a/hir/src/module.rs +++ b/hir/src/module.rs @@ -63,11 +63,48 @@ pub struct Module { } impl fmt::Display for Module { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use std::fmt::Write; + if self.is_kernel { write!(f, "kernel {}\n", &self.name)?; } else { write!(f, "module {}\n", &self.name)?; } + + if !self.segments.is_empty() { + f.write_char('\n')?; + f.write_str("memory {\n")?; + for segment in self.segments.iter() { + let data = self.globals.get_constant(segment.init); + writeln!( + f, + " segment @{:#x} x {} = {};", + segment.offset, segment.size, data + )?; + } + f.write_str("}\n\n")?; + } + + if !self.globals.is_empty() { + for global in self.globals.iter() { + write!( + f, + "global {} {} : {}", + global.linkage, global.name, global.ty + )?; + match global.init { + Some(init) => { + let data = self.globals.get_constant(init); + writeln!(f, " = {} {{ id = {} }};", data, global.id())?; + } + None => { + writeln!(f, " {{ id = {} }};", global.id())?; + } + } + } + writeln!(f)?; + } + let mut external_functions = BTreeMap::::default(); for function in self.functions.iter() { for import in function.dfg.imports() {