Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
wpt967 committed Sep 17, 2024
1 parent 8508bd6 commit bded209
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 93 deletions.
3 changes: 0 additions & 3 deletions crates/llvm-context/src/polkavm/context/debug_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ impl<'ctx> DebugInfo<'ctx> {
is_optimized,
);

self.builder
.create_lexical_block(function.as_debug_info_scope(), file, line_num, column);

Ok(function)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ where
context.set_code_type(CodeType::Deploy);

if let Some(dinfo) = context.debug_info() {
let di_builder = dinfo.builder();
let line_num: u32 = 0;
let column: u32 = 0;
let func_name: &str = runtime::FUNCTION_DEPLOY_CODE;
Expand All @@ -85,13 +86,30 @@ where
false,
Some(inkwell::debug_info::DIFlagsConstants::PUBLIC),
)?;
dinfo.push_scope(di_func_scope.as_debug_info_scope());
let _ = dinfo.push_scope(di_func_scope.as_debug_info_scope());
let func_value = context
.current_function()
.borrow()
.declaration()
.function_value();
let _ = func_value.set_subprogram(di_func_scope);

let lexical_scope = di_builder.create_lexical_block(
di_func_scope.as_debug_info_scope(),
dinfo.compilation_unit().get_file(),
line_num,
column,
).as_debug_info_scope();
let _ = dinfo.push_scope(lexical_scope);

let di_loc = di_builder.create_debug_location(
context.llvm(),
line_num,
0,
lexical_scope,
None,
);
context.builder().set_current_debug_location(di_loc)
}

self.inner.into_llvm(context)?;
Expand All @@ -111,6 +129,7 @@ where

if let Some(dinfo) = context.debug_info() {
let _ = dinfo.pop_scope();
let _ = dinfo.pop_scope();
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ where
context.set_basic_block(context.current_function().borrow().entry_block());

if let Some(dinfo) = context.debug_info() {
let di_builder = dinfo.builder();
let line_num: u32 = 0;
let column: u32 = 0;
let func_name: &str = runtime::FUNCTION_ENTRY;
Expand All @@ -276,7 +277,27 @@ where
false,
Some(inkwell::debug_info::DIFlagsConstants::PUBLIC),
)?;
dinfo.push_scope(di_func_scope.as_debug_info_scope());
let _ = dinfo.push_scope(di_func_scope.as_debug_info_scope());
let func_value = context
.current_function()
.borrow()
.declaration()
.function_value();
let _ = func_value.set_subprogram(di_func_scope);

let lexical_scope = di_builder
.create_lexical_block(
di_func_scope.as_debug_info_scope(),
dinfo.compilation_unit().get_file(),
line_num,
column,
)
.as_debug_info_scope();
let _ = dinfo.push_scope(lexical_scope);

let di_loc =
di_builder.create_debug_location(context.llvm(), line_num, 0, lexical_scope, None);
context.builder().set_current_debug_location(di_loc)
}

Self::initialize_globals(context)?;
Expand All @@ -289,6 +310,7 @@ where

if let Some(dinfo) = context.debug_info() {
let _ = dinfo.pop_scope();
let _ = dinfo.pop_scope();
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ where
context.set_basic_block(context.current_function().borrow().entry_block());
context.set_code_type(CodeType::Runtime);
if let Some(dinfo) = context.debug_info() {
let di_builder = dinfo.builder();
let line_num: u32 = 0;
let column: u32 = 0;
let func_name: &str = runtime::FUNCTION_RUNTIME_CODE;
Expand All @@ -84,13 +85,30 @@ where
false,
Some(inkwell::debug_info::DIFlagsConstants::PUBLIC),
)?;
dinfo.push_scope(di_func_scope.as_debug_info_scope());
let _ = dinfo.push_scope(di_func_scope.as_debug_info_scope());
let func_value = context
.current_function()
.borrow()
.declaration()
.function_value();
let _ = func_value.set_subprogram(di_func_scope);

let lexical_scope = di_builder.create_lexical_block(
di_func_scope.as_debug_info_scope(),
dinfo.compilation_unit().get_file(),
line_num,
column,
).as_debug_info_scope();
let _ = dinfo.push_scope(lexical_scope);

let di_loc = di_builder.create_debug_location(
context.llvm(),
line_num,
0,
lexical_scope,
None,
);
context.builder().set_current_debug_location(di_loc)
}

self.inner.into_llvm(context)?;
Expand All @@ -110,6 +128,7 @@ where

if let Some(dinfo) = context.debug_info() {
let _ = dinfo.pop_scope();
let _ = dinfo.pop_scope();
}

Ok(())
Expand Down
38 changes: 17 additions & 21 deletions crates/solidity/src/yul/parser/statement/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,30 +115,28 @@ where
mut self,
context: &mut revive_llvm_context::PolkaVMContext<D>,
) -> anyhow::Result<()> {
if let Some(dinfo) = context.debug_info() {
let di_parent_scope = dinfo
.top_scope()
.expect("expected a debug-info scope")
.clone();
let line_num: u32 = std::cmp::min(self.location.line, u32::MAX as usize) as u32;
let di_loc = dinfo.builder().create_debug_location(
context.llvm(),
line_num,
0,
di_parent_scope,
None,
);
context.builder().set_current_debug_location(di_loc)
};

let value = match self.initializer.into_llvm(context)? {
Some(value) => value,
None => return Ok(()),
};

if self.bindings.len() == 1 {
if let Some(dinfo) = context.debug_info() {
let di_builder = dinfo.builder();
let di_parent_scope = dinfo
.top_scope()
.expect("expected a debug-info scope")
.clone();
let line_num: u32 = std::cmp::min(self.location.line, u32::MAX as usize) as u32;

let di_loc = di_builder.create_debug_location(
context.llvm(),
line_num,
0,
di_parent_scope,
None,
);
context.builder().set_current_debug_location(di_loc)
};

let identifier = self.bindings.remove(0);
let pointer = context
.current_function()
Expand All @@ -162,14 +160,12 @@ where

for (index, binding) in self.bindings.into_iter().enumerate() {
if let Some(dinfo) = context.debug_info() {
let di_builder = dinfo.builder();
let di_parent_scope = dinfo
.top_scope()
.expect("expected a debug-info scope")
.clone();
let line_num: u32 = std::cmp::min(self.location.line, u32::MAX as usize) as u32;

let di_loc = di_builder.create_debug_location(
let di_loc = dinfo.builder().create_debug_location(
context.llvm(),
line_num,
0,
Expand Down
54 changes: 35 additions & 19 deletions crates/solidity/src/yul/parser/statement/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,25 @@ where
let current_function = context.current_function().borrow().name().to_owned();
let current_block = context.basic_block();

let mut functions = Vec::with_capacity(self.statements.len());
let mut local_statements = Vec::with_capacity(self.statements.len());

for statement in self.statements.into_iter() {
match statement {
Statement::FunctionDefinition(mut statement) => {
statement.declare(context)?;
functions.push(statement);
}
statement => local_statements.push(statement),
}
}

for function in functions.into_iter() {
function.into_llvm(context)?;
}

context.set_current_function(current_function.as_str())?;

if let Some(dinfo) = context.debug_info() {
let di_builder = dinfo.builder();
let di_parent_scope = dinfo
Expand All @@ -160,27 +179,24 @@ where
context.builder().set_current_debug_location(di_loc);
}

let mut functions = Vec::with_capacity(self.statements.len());
let mut local_statements = Vec::with_capacity(self.statements.len());

for statement in self.statements.into_iter() {
match statement {
Statement::FunctionDefinition(mut statement) => {
statement.declare(context)?;
functions.push(statement);
}
statement => local_statements.push(statement),
}
}

for function in functions.into_iter() {
function.into_llvm(context)?;
}

context.set_current_function(current_function.as_str())?;
context.set_basic_block(current_block);

for statement in local_statements.into_iter() {
if let Some(dinfo) = context.debug_info() {
let di_block_scope = dinfo
.top_scope()
.expect("expected a debug-info scope")
.clone();
let line_num: u32 =
std::cmp::min(statement.location().line, u32::MAX as usize) as u32;
let di_loc = dinfo.builder().create_debug_location(
context.llvm(),
line_num,
0,
di_block_scope,
None,
);
context.builder().set_current_debug_location(di_loc);
}
if context.basic_block().get_terminator().is_some() {
break;
}
Expand Down
88 changes: 52 additions & 36 deletions crates/solidity/src/yul/parser/statement/function_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,45 +267,10 @@ where
context: &mut revive_llvm_context::PolkaVMContext<'ctx, D>,
) -> anyhow::Result<()> {
context.set_current_function(self.identifier.as_str())?;
let r#return = context.current_function().borrow().r#return();

context.set_basic_block(context.current_function().borrow().entry_block());
match r#return {
revive_llvm_context::PolkaVMFunctionReturn::None => {}
revive_llvm_context::PolkaVMFunctionReturn::Primitive { pointer } => {
let identifier = self.result.pop().expect("Always exists");

let r#type = identifier.r#type.unwrap_or_default();
context.build_store(pointer, r#type.into_llvm(context).const_zero())?;
context
.current_function()
.borrow_mut()
.insert_stack_pointer(identifier.inner, pointer);
}
revive_llvm_context::PolkaVMFunctionReturn::Compound { pointer, .. } => {
for (index, identifier) in self.result.into_iter().enumerate() {
let r#type = identifier.r#type.unwrap_or_default().into_llvm(context);
let pointer = context.build_gep(
pointer,
&[
context.word_const(0),
context
.integer_type(revive_common::BIT_LENGTH_X32)
.const_int(index as u64, false),
],
context.word_type(),
format!("return_{index}_gep_pointer").as_str(),
);
context.build_store(pointer, r#type.const_zero())?;
context
.current_function()
.borrow_mut()
.insert_stack_pointer(identifier.inner.clone(), pointer);
}
}
};

if let Some(dinfo) = context.debug_info() {
let di_builder = dinfo.builder();
let line_num: u32 = std::cmp::min(self.location.line, u32::MAX as usize) as u32;
let column: u32 = std::cmp::min(self.location.column, u32::MAX as usize) as u32;
let func_value = context
Expand Down Expand Up @@ -338,8 +303,58 @@ where
)?;
dinfo.push_scope(di_func_scope.as_debug_info_scope());
let _ = func_value.set_subprogram(di_func_scope);

let lexical_scope = di_builder
.create_lexical_block(
di_func_scope.as_debug_info_scope(),
dinfo.compilation_unit().get_file(),
line_num,
column,
)
.as_debug_info_scope();
let _ = dinfo.push_scope(lexical_scope);

let di_loc =
di_builder.create_debug_location(context.llvm(), line_num, 0, lexical_scope, None);
context.builder().set_current_debug_location(di_loc)
}

let r#return = context.current_function().borrow().r#return();
match r#return {
revive_llvm_context::PolkaVMFunctionReturn::None => {}
revive_llvm_context::PolkaVMFunctionReturn::Primitive { pointer } => {
let identifier = self.result.pop().expect("Always exists");

let r#type = identifier.r#type.unwrap_or_default();
context.build_store(pointer, r#type.into_llvm(context).const_zero())?;
context
.current_function()
.borrow_mut()
.insert_stack_pointer(identifier.inner, pointer);
}
revive_llvm_context::PolkaVMFunctionReturn::Compound { pointer, .. } => {
for (index, identifier) in self.result.into_iter().enumerate() {
let r#type = identifier.r#type.unwrap_or_default().into_llvm(context);
let pointer = context.build_gep(
pointer,
&[
context.word_const(0),
context
.integer_type(revive_common::BIT_LENGTH_X32)
.const_int(index as u64, false),
],
context.word_type(),
format!("return_{index}_gep_pointer").as_str(),
);
context.build_store(pointer, r#type.const_zero())?;
context
.current_function()
.borrow_mut()
.insert_stack_pointer(identifier.inner.clone(), pointer);
}
}
};

let argument_types: Vec<_> = self
.arguments
.iter()
Expand Down Expand Up @@ -407,6 +422,7 @@ where

if let Some(dinfo) = context.debug_info() {
let _ = dinfo.pop_scope();
let _ = dinfo.pop_scope();
}

Ok(())
Expand Down
Loading

0 comments on commit bded209

Please sign in to comment.