From 3f61731a1b31a31ee452295bf4caa60c0d06a32b Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Sat, 30 May 2026 18:37:46 +0700 Subject: [PATCH] asm: add const literal builder helpers Add ProgramBuilder::const_str and ProgramBuilder::const_bytes as small convenience wrappers around the existing constant-pool interner. The helpers keep lowering code from spelling out Const::Str and Const::Bytes directly while preserving the same deduplication behavior. Add coverage that the helpers intern with constant(Const::...) and round-trip through Program. --- execution_tape/src/asm.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/execution_tape/src/asm.rs b/execution_tape/src/asm.rs index f97d1dd..9acaa9f 100644 --- a/execution_tape/src/asm.rs +++ b/execution_tape/src/asm.rs @@ -457,6 +457,16 @@ impl ProgramBuilder { id } + /// Interns a UTF-8 string constant and returns its [`ConstId`]. + pub fn const_str(&mut self, value: &str) -> ConstId { + self.constant(Const::Str(value.into())) + } + + /// Interns a byte string constant and returns its [`ConstId`]. + pub fn const_bytes(&mut self, value: &[u8]) -> ConstId { + self.constant(Const::Bytes(value.into())) + } + /// Returns a mutable reference to the type table. pub fn types_mut(&mut self) -> &mut TypeTableDef { &mut self.types @@ -2170,6 +2180,23 @@ mod tests { assert_eq!(e0, e1); } + #[test] + fn program_builder_const_literal_helpers_intern_constants() { + let mut pb = ProgramBuilder::new(); + + let text = pb.const_str("hello"); + let text_again = pb.constant(Const::Str("hello".into())); + assert_eq!(text, text_again); + + let bytes = pb.const_bytes(&[1, 2, 3]); + let bytes_again = pb.constant(Const::Bytes(vec![1, 2, 3])); + assert_eq!(bytes, bytes_again); + + let p = pb.build(); + assert_eq!(p.const_str(text).unwrap(), "hello"); + assert_eq!(p.const_bytes(bytes).unwrap(), &[1, 2, 3]); + } + #[test] fn program_builder_call_sig_interns_and_builds_program_table() { let mut pb = ProgramBuilder::new();