Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[move] add return locations to source map #19885

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ pub struct FunctionSourceMap {
/// The names of the parameters to the function.
pub parameters: Vec<SourceName>,

/// The locations of the return values
pub returns: Vec<Loc>,

/// The index into the vector is the local's index. The corresponding `(Identifier, Location)` tuple
/// is the name and location of the local.
pub locals: Vec<SourceName>,
Expand Down Expand Up @@ -206,6 +209,7 @@ impl FunctionSourceMap {
definition_location,
type_parameters: Vec::new(),
parameters: Vec::new(),
returns: Vec::new(),
locals: Vec::new(),
code_map: BTreeMap::new(),
is_native,
Expand Down Expand Up @@ -252,6 +256,10 @@ impl FunctionSourceMap {
self.parameters.push(name)
}

/// add the locations of return values
pub fn add_return_mapping(&mut self, loc: Loc) {
self.returns.push(loc);
}
/// Recall that we are using a segment tree. We therefore lookup the location for the code
/// offset by performing a range query for the largest number less than or equal to the code
/// offset passed in.
Expand Down Expand Up @@ -451,6 +459,18 @@ impl SourceMap {
Ok(())
}

pub fn add_return_mapping(
&mut self,
fdef_idx: FunctionDefinitionIndex,
loc: Loc,
) -> Result<()> {
let func_entry = self.function_map.get_mut(&fdef_idx.0).ok_or_else(|| {
format_err!("Tried to add return mapping to undefined function index")
})?;
func_entry.add_return_mapping(loc);
Ok(())
}

pub fn get_parameter_or_local_name(
&self,
fdef_idx: FunctionDefinitionIndex,
Expand Down
18 changes: 18 additions & 0 deletions external-crates/move/crates/move-ir-to-bytecode/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ macro_rules! record_src_loc {
.source_map
.add_parameter_mapping($context.current_function_definition_index(), source_name)?;
}};
(return_: $context:expr, $_type:expr) => {{
$context
.source_map
.add_return_mapping($context.current_function_definition_index(), $_type.loc)?;
}};
(field: $context:expr, $idx: expr, $field:expr) => {{
$context
.source_map
Expand Down Expand Up @@ -882,6 +887,7 @@ fn compile_function_body_impl(
context,
m,
ast_function.signature.formals,
ast_function.signature.return_type,
locals,
code,
)?)
Expand All @@ -898,6 +904,7 @@ fn compile_function_body_impl(
context,
m,
ast_function.signature.formals,
ast_function.signature.return_type,
locals,
code,
)?)
Expand All @@ -907,6 +914,9 @@ fn compile_function_body_impl(
for (var, _) in ast_function.signature.formals.into_iter() {
record_src_loc!(parameter: context, var)
}
for _type in ast_function.signature.return_type.into_iter() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Same thing here on using a suffix _ for this variable name.

record_src_loc!(return_: context, _type)
}
None
}
})
Expand Down Expand Up @@ -954,6 +964,7 @@ fn compile_function_body(
context: &mut Context,
type_parameters: HashMap<TypeVar_, TypeParameterIndex>,
formals: Vec<(Var, Type)>,
return_type: Vec<Type>,
locals: Vec<(Var, Type)>,
blocks: Vec<Block>,
) -> Result<CodeUnit> {
Expand All @@ -964,6 +975,9 @@ fn compile_function_body(
record_src_loc!(parameter: context, var);
}

for _type in return_type {
record_src_loc!(return_: context, _type);
}
let mut locals_signature = Signature(vec![]);
for (var_, t) in locals {
let sig = compile_type(context, function_frame.type_parameters(), &t)?;
Expand Down Expand Up @@ -1716,6 +1730,7 @@ fn compile_function_body_bytecode(
context: &mut Context,
type_parameters: HashMap<TypeVar_, TypeParameterIndex>,
formals: Vec<(Var, Type)>,
return_type: Vec<Type>,
locals: Vec<(Var, Type)>,
blocks: BytecodeBlocks,
) -> Result<CodeUnit> {
Expand All @@ -1726,6 +1741,9 @@ fn compile_function_body_bytecode(
function_frame.define_local(&var.value, sig.clone())?;
record_src_loc!(parameter: context, var);
}
for _type in return_type {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto here on naming.

record_src_loc!(return_: context, _type);
}
for (var_, t) in locals {
let sig = compile_type(context, function_frame.type_parameters(), &t)?;
function_frame.define_local(&var_.value, sig.clone())?;
Expand Down
Loading