Skip to content

Commit

Permalink
add custom native options to move prover backend
Browse files Browse the repository at this point in the history
Closes: diem#439
  • Loading branch information
emmazzz authored and bors-diem committed Mar 6, 2023
1 parent 53b20de commit 2318e61
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
23 changes: 20 additions & 3 deletions language/move-prover/boogie-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ pub fn add_prelude(
templates.push(templ("multiset-theory", MULTISET_ARRAY_THEORY));
templates.push(templ("table-theory", TABLE_ARRAY_THEORY));

let mut tera = Tera::default();
tera.add_raw_templates(templates)?;

let mut context = Context::new();
context.insert("options", options);

Expand Down Expand Up @@ -151,6 +148,26 @@ pub fn add_prelude(
context.insert("std", &std_addr);
context.insert("Ext", &ext_addr);

// If a custom Boogie template is provided, add it as part of the templates and
// add all type instances that use generic functions in the provided modules to the context.
if let Some(custom_native_options) = options.custom_natives.clone() {
templates.push(templ(
"custom-natives",
&std::fs::read(&custom_native_options.template_path).unwrap_or_else(|_| {
panic!(
"cannot read custom template file {}",
&custom_native_options.template_path
)
}),
));
for (module_name, instance_name) in custom_native_options.module_instance_names {
context.insert(instance_name, &filter_native(&module_name));
}
}

let mut tera = Tera::default();
tera.add_raw_templates(templates)?;

let expanded_content = tera.render("prelude", &context)?;
emitln!(writer, &expanded_content);
Ok(())
Expand Down
13 changes: 13 additions & 0 deletions language/move-prover/boogie-backend/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ impl VectorTheory {
}
}

/// Options to define custom native functions to include in generated Boogie file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomNativeOptions {
/// Path to the custom template file.
pub template_path: String,
/// List of (module name, module instance key) tuples, used to generate instantiated
/// versions of generic native functions.
pub module_instance_names: Vec<(String, String)>,
}

/// Boogie options.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
Expand Down Expand Up @@ -105,6 +115,8 @@ pub struct BoogieOptions {
pub vector_theory: VectorTheory,
/// Whether to generate a z3 trace file and where to put it.
pub z3_trace_file: Option<String>,
/// Options to define user-custom native funs.
pub custom_natives: Option<CustomNativeOptions>,
}

impl Default for BoogieOptions {
Expand Down Expand Up @@ -139,6 +151,7 @@ impl Default for BoogieOptions {
hard_timeout_secs: 0,
vector_theory: VectorTheory::BoogieArray,
z3_trace_file: None,
custom_natives: None,
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion language/move-prover/boogie-backend/src/prelude/prelude.bpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ options provided to the prover.
{% include "vector-theory" %}
{% include "multiset-theory" %}
{% include "table-theory" %}

{%- if options.custom_natives -%}
{% include "custom-natives" %}
{%- endif %}

// ============================================================================================
// Primitive Types
Expand Down

0 comments on commit 2318e61

Please sign in to comment.