Skip to content

Commit

Permalink
[move-lang] Remove ability to declare assign values to named addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Zakian committed Aug 6, 2021
1 parent 05f533e commit 10e9181
Show file tree
Hide file tree
Showing 98 changed files with 659 additions and 643 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions language/benchmarks/src/move_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn compile_modules() -> Vec<CompiledModule> {
src_files.push(MOVE_BENCH_SRC_PATH.to_str().unwrap().to_owned());
let (_files, compiled_units) = Compiler::new(&src_files, &[])
.set_flags(Flags::empty().set_sources_shadow_deps(false))
.set_named_address_values(move_stdlib::move_stdlib_named_addresses())
.build_and_report()
.expect("Error compiling...");
compiled_units
Expand Down
10 changes: 0 additions & 10 deletions language/diem-framework/modules/CoreAddresses.move
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
address DiemFramework = 0x1;
address Std = 0x1;

address DiemRoot = 0xA550C18;
address CurrencyInfo = 0xA550C18;

address TreasuryCompliance = 0xB1E55ED;

address VMReserved = 0x0;

/// Module providing well-known addresses and related logic.
///
/// > Note: this module currently defines zero-argument functions like `Self::DIEM_ROOT_ADDRESS()` using capitalization
Expand Down
19 changes: 18 additions & 1 deletion language/diem-framework/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use move_binary_format::{access::ModuleAccess, file_format::CompiledModule};
use move_command_line_common::files::{
extension_equals, find_filenames, MOVE_COMPILED_EXTENSION, MOVE_EXTENSION,
};
use move_lang::{compiled_unit::CompiledUnit, Compiler};
use move_lang::{compiled_unit::CompiledUnit, shared::AddressBytes, Compiler};
use once_cell::sync::Lazy;
use sha2::{Digest, Sha256};
use std::{
Expand Down Expand Up @@ -50,6 +50,22 @@ pub fn diem_stdlib_files() -> Vec<String> {
files
}

// TODO: This will be declared in the package once those are in
pub fn diem_framework_named_addresses() -> BTreeMap<String, AddressBytes> {
let mapping = [
("Std", "0x1"),
("DiemFramework", "0x1"),
("DiemRoot", "0xA550C18"),
("CurrencyInfo", "0xA550C18"),
("TreasuryCompliance", "0xB1E55ED"),
("VMReserved", "0x0"),
];
mapping
.iter()
.map(|(name, addr)| (name.to_string(), AddressBytes::parse_str(addr).unwrap()))
.collect()
}

pub fn stdlib_bytecode_files() -> Vec<String> {
let path = path_in_crate(COMPILED_OUTPUT_PATH);
let names = diem_stdlib_files();
Expand Down Expand Up @@ -88,6 +104,7 @@ pub fn stdlib_bytecode_files() -> Vec<String> {

pub(crate) fn build_stdlib() -> BTreeMap<String, CompiledModule> {
let (_files, compiled_units) = Compiler::new(&diem_stdlib_files(), &[])
.set_named_address_values(diem_framework_named_addresses())
.build_and_report()
.unwrap();
let mut modules = BTreeMap::new();
Expand Down
10 changes: 9 additions & 1 deletion language/diem-framework/src/release.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{path_in_crate, save_binary};
use crate::{diem_framework_named_addresses, path_in_crate, save_binary};
use log::LevelFilter;
use move_binary_format::{compatibility::Compatibility, normalized::Module, CompiledModule};
use move_command_line_common::files::{
Expand Down Expand Up @@ -118,6 +118,7 @@ fn generate_module_docs(output_path: impl AsRef<Path>, with_diagram: bool) {
crate::diem_stdlib_files_no_dependencies().as_slice(),
vec![move_stdlib::move_stdlib_modules_full_path()],
with_diagram,
diem_framework_named_addresses(),
)
}

Expand Down Expand Up @@ -186,6 +187,7 @@ fn generate_script_docs(
crate::diem_stdlib_modules_full_path(),
],
with_diagram,
diem_framework_named_addresses(),
)
}

Expand All @@ -203,6 +205,9 @@ fn generate_script_abis(
crate::diem_stdlib_modules_full_path(),
],
verbosity_level: LevelFilter::Warn,
move_named_address_values: move_prover::cli::named_addresses_for_options(
&diem_framework_named_addresses(),
),
run_abigen: true,
abigen: abigen::AbigenOptions {
output_directory: output_path.to_string_lossy().to_string(),
Expand Down Expand Up @@ -252,6 +257,9 @@ fn build_error_code_map(output_path: impl AsRef<Path>) {
let options = move_prover::cli::Options {
move_sources: crate::diem_stdlib_files(),
move_deps: vec![],
move_named_address_values: move_prover::cli::named_addresses_for_options(
&diem_framework_named_addresses(),
),
verbosity_level: LevelFilter::Warn,
run_errmapgen: true,
errmapgen: errmapgen::ErrmapOptions {
Expand Down
4 changes: 3 additions & 1 deletion language/diem-framework/tests/move_unit_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use diem_framework::diem_framework_named_addresses;
use diem_vm::natives::diem_natives;
use move_unit_test::UnitTestingConfig;

move_unit_test::register_move_unit_tests!(
UnitTestingConfig::default_with_bound(Some(100_000)),
UnitTestingConfig::default_with_bound(Some(100_000))
.with_named_addresses(diem_framework_named_addresses()),
".",
r".*\.move$",
&move_stdlib::move_stdlib_modules_full_path(),
Expand Down
1 change: 1 addition & 0 deletions language/diem-tools/transaction-replay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ fn compile_move_script(file_path: &str) -> Result<Vec<u8>> {
let targets = &vec![cur_path];
let (files, units_or_diags) = Compiler::new(targets, &diem_framework::diem_stdlib_files())
.set_flags(Flags::empty().set_sources_shadow_deps(false))
.set_named_address_values(diem_framework::diem_framework_named_addresses())
.build()?;
let unit = match units_or_diags {
Err(diags) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn compile_script(source_file_str: String) -> Vec<u8> {
let (_files, mut compiled_program) =
Compiler::new(&[source_file_str], &diem_framework::diem_stdlib_files())
.set_flags(Flags::empty().set_sources_shadow_deps(false))
.set_named_address_values(diem_framework::diem_framework_named_addresses())
.build_and_report()
.unwrap();
let mut script_bytes = vec![];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use anyhow::{bail, Result};
use diem_framework::diem_framework_named_addresses;
use diem_types::account_address::AccountAddress as DiemAddress;
use functional_tests::{
compiler::{Compiler, ScriptOrModule},
Expand Down Expand Up @@ -43,6 +44,7 @@ impl<'a> MoveSourceCompiler<'a> {
)> {
let (files, comments_and_compiler_res) = MoveCompiler::new(targets, &self.deps)
.set_pre_compiled_lib(self.pre_compiled_deps)
.set_named_address_values(diem_framework_named_addresses())
.run::<PASS_COMPILATION>()?;
match comments_and_compiler_res {
Err(diags) => Ok((files, Err(diags))),
Expand Down Expand Up @@ -131,6 +133,7 @@ static DIEM_PRECOMPILED_STDLIB: Lazy<FullyCompiledProgram> = Lazy::new(|| {
&diem_framework::diem_stdlib_files(),
None,
Flags::empty().set_sources_shadow_deps(false),
diem_framework_named_addresses(),
)
.unwrap();
match program_res {
Expand Down
13 changes: 12 additions & 1 deletion language/move-lang/src/bin/move-build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use move_lang::{
command_line::{self as cli},
shared::Flags,
shared::{self, verify_and_create_named_address_mapping, AddressBytes, Flags},
};
use structopt::*;

Expand Down Expand Up @@ -41,6 +41,15 @@ pub struct Options {
)]
pub emit_source_map: bool,

/// Named address mapping
#[structopt(
name = "NAMED_ADDRESSES",
short = "a",
long = "addresses",
parse(try_from_str = shared::parse_named_address)
)]
pub named_addresses: Vec<(String, AddressBytes)>,

#[structopt(flatten)]
pub flags: Flags,
}
Expand All @@ -52,12 +61,14 @@ pub fn main() -> anyhow::Result<()> {
out_dir,
emit_source_map,
flags,
named_addresses,
} = Options::from_args();

let interface_files_dir = format!("{}/generated_interface_files", out_dir);
let (files, compiled_units) = move_lang::Compiler::new(&source_files, &dependencies)
.set_interface_files_dir(interface_files_dir)
.set_flags(flags)
.set_named_address_values(verify_and_create_named_address_mapping(named_addresses)?)
.build_and_report()?;
move_lang::output_compiled_units(emit_source_map, files, compiled_units, &out_dir)
}
13 changes: 12 additions & 1 deletion language/move-lang/src/bin/move-check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use move_lang::{
command_line::{self as cli},
shared::Flags,
shared::{self, verify_and_create_named_address_mapping, AddressBytes, Flags},
};
use structopt::*;

Expand Down Expand Up @@ -36,6 +36,15 @@ pub struct Options {
)]
pub out_dir: Option<String>,

/// Named address mapping
#[structopt(
name = "NAMED_ADDRESSES",
short = "a",
long = "addresses",
parse(try_from_str = shared::parse_named_address)
)]
pub named_addresses: Vec<(String, AddressBytes)>,

#[structopt(flatten)]
pub flags: Flags,
}
Expand All @@ -46,10 +55,12 @@ pub fn main() -> anyhow::Result<()> {
dependencies,
out_dir,
flags,
named_addresses,
} = Options::from_args();

let _files = move_lang::Compiler::new(&source_files, &dependencies)
.set_interface_files_dir_opt(out_dir)
.set_named_address_values(verify_and_create_named_address_mapping(named_addresses)?)
.set_flags(flags)
.check_and_report()?;
Ok(())
Expand Down
13 changes: 2 additions & 11 deletions language/move-lang/src/cfgir/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
BaseType, Command, Command_, FunctionSignature, Label, SingleType, StructDefinition,
},
parser::ast::{ConstantName, FunctionName, StructName, Var, Visibility},
shared::{ast_debug::*, unique_map::UniqueMap, AddressBytes, Name},
shared::{ast_debug::*, unique_map::UniqueMap},
};
use move_core_types::value::MoveValue;
use move_ir_types::location::*;
Expand All @@ -21,8 +21,6 @@ use std::collections::{BTreeMap, BTreeSet, VecDeque};

#[derive(Debug, Clone)]
pub struct Program {
// Map of known named address values. Not all addresses will be present
pub addresses: UniqueMap<Name, AddressBytes>,
pub modules: UniqueMap<ModuleIdent, ModuleDefinition>,
pub scripts: BTreeMap<String, Script>,
}
Expand Down Expand Up @@ -180,14 +178,7 @@ fn remap_labels_cmd(remapping: &BTreeMap<Label, Label>, sp!(_, cmd_): &mut Comma

impl AstDebug for Program {
fn ast_debug(&self, w: &mut AstWriter) {
let Program {
addresses,
modules,
scripts,
} = self;
for (_, addr, bytes) in addresses {
w.writeln(&format!("address {} = {};", addr, bytes));
}
let Program { modules, scripts } = self;

for (m, mdef) in modules.key_cloned_iter() {
w.write(&format!("module {}", m));
Expand Down
27 changes: 8 additions & 19 deletions language/move-lang/src/cfgir/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
expansion::ast::{AbilitySet, ModuleIdent, Value, Value_},
hlir::ast::{self as H, Label},
parser::ast::{ConstantName, FunctionName, StructName, Var},
shared::{unique_map::UniqueMap, AddressBytes, CompilationEnv, Name},
shared::{unique_map::UniqueMap, CompilationEnv},
FullyCompiledProgram,
};
use cfgir::ast::LoopInfo;
Expand All @@ -28,7 +28,6 @@ use std::{

struct Context<'env> {
env: &'env mut CompilationEnv,
addresses: &'env UniqueMap<Name, AddressBytes>,
struct_declared_abilities: UniqueMap<ModuleIdent, UniqueMap<StructName, AbilitySet>>,
start: Option<Label>,
loop_begin: Option<Label>,
Expand All @@ -46,7 +45,6 @@ impl<'env> Context<'env> {
pub fn new(
env: &'env mut CompilationEnv,
pre_compiled_lib: Option<&FullyCompiledProgram>,
addresses: &'env UniqueMap<Name, AddressBytes>,
modules: &UniqueMap<ModuleIdent, H::ModuleDefinition>,
) -> Self {
let all_modules = modules.key_cloned_iter().chain(
Expand All @@ -68,7 +66,6 @@ impl<'env> Context<'env> {
.unwrap();
Context {
env,
addresses,
struct_declared_abilities,
next_label: None,
loop_begin: None,
Expand Down Expand Up @@ -159,21 +156,16 @@ pub fn program(
prog: H::Program,
) -> G::Program {
let H::Program {
addresses,
modules: hmodules,
scripts: hscripts,
} = prog;

let mut context = Context::new(compilation_env, pre_compiled_lib, &addresses, &hmodules);
let mut context = Context::new(compilation_env, pre_compiled_lib, &hmodules);

let modules = modules(&mut context, hmodules);
let scripts = scripts(&mut context, hscripts);

G::Program {
addresses,
modules,
scripts,
}
G::Program { modules, scripts }
}

fn modules(
Expand Down Expand Up @@ -363,18 +355,15 @@ fn move_value_from_exp(context: &mut Context, e: H::Exp) -> Option<MoveValue> {
}
}

fn move_value_from_value(context: &mut Context, sp!(loc, v_): Value) -> Option<MoveValue> {
fn move_value_from_value(context: &mut Context, sp!(_, v_): Value) -> Option<MoveValue> {
use MoveValue as MV;
use Value_ as V;
Some(match v_ {
V::InferredNum(_) => panic!("ICE inferred num should have been expanded"),
V::Address(a) => match a.into_addr_bytes(context.addresses, loc, "address value") {
Ok(bytes) => MV::Address(MoveAddress::new(bytes.into_bytes())),
Err(diag) => {
context.env.add_diag(diag);
return None;
}
},
V::Address(a) => MV::Address(MoveAddress::new(
a.into_addr_bytes(context.env.named_address_mapping())
.into_bytes(),
)),
V::U8(u) => MV::U8(u),
V::U64(u) => MV::U64(u),
V::U128(u) => MV::U128(u),
Expand Down
Loading

0 comments on commit 10e9181

Please sign in to comment.