Skip to content
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
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
too-many-arguments-threshold = 13
cognitive-complexity-threshold = 37
disallowed-names = ["binary"]
42 changes: 21 additions & 21 deletions src/bin/solang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,13 @@ fn contract_results(

let context = inkwell::context::Context::create();

let binary = resolved_contract.binary(ns, &context, opt, contract_no);
let bin = resolved_contract.binary(ns, &context, opt, contract_no);

if save_intermediates(&binary, compiler_output) {
if save_intermediates(&bin, compiler_output) {
return;
}

let code = binary.code(Generate::Linked).expect("llvm build");
let code = bin.code(Generate::Linked).expect("llvm build");

#[cfg(feature = "wasm_opt")]
if let Some(level) = opt.wasm_opt.filter(|_| ns.target.is_polkadot() && verbose) {
Expand All @@ -429,7 +429,7 @@ fn contract_results(

if std_json {
json_contracts.insert(
binary.name,
bin.name,
JsonContract {
abi: abi::ethereum::gen_abi(contract_no, ns),
ewasm: Some(EwasmContract {
Expand All @@ -441,7 +441,7 @@ fn contract_results(
} else {
let bin_filename = output_file(
compiler_output,
&binary.name,
&bin.name,
ns.target.file_extension(),
false,
);
Expand All @@ -450,7 +450,7 @@ fn contract_results(
eprintln!(
"info: Saving binary {} for contract {}",
bin_filename.display(),
binary.name
bin.name
);
}

Expand All @@ -460,13 +460,13 @@ fn contract_results(

let (metadata, meta_ext) =
abi::generate_abi(contract_no, ns, &code, verbose, default_authors, version);
let meta_filename = output_file(compiler_output, &binary.name, meta_ext, true);
let meta_filename = output_file(compiler_output, &bin.name, meta_ext, true);

if verbose {
eprintln!(
"info: Saving metadata {} for contract {}",
meta_filename.display(),
binary.name
bin.name
);
}

Expand All @@ -476,60 +476,60 @@ fn contract_results(
}

fn save_intermediates(
binary: &solang::emit::binary::Binary,
bin: &solang::emit::binary::Binary,
compiler_output: &CompilerOutput,
) -> bool {
let verbose = compiler_output.verbose;

match compiler_output.emit.as_deref() {
Some("llvm-ir") => {
let llvm_filename = output_file(compiler_output, &binary.name, "ll", false);
let llvm_filename = output_file(compiler_output, &bin.name, "ll", false);

if verbose {
eprintln!(
"info: Saving LLVM IR {} for contract {}",
llvm_filename.display(),
binary.name
bin.name
);
}

binary.dump_llvm(&llvm_filename).unwrap();
bin.dump_llvm(&llvm_filename).unwrap();

true
}

Some("llvm-bc") => {
let bc_filename = output_file(compiler_output, &binary.name, "bc", false);
let bc_filename = output_file(compiler_output, &bin.name, "bc", false);

if verbose {
eprintln!(
"info: Saving LLVM BC {} for contract {}",
bc_filename.display(),
binary.name
bin.name
);
}

binary.bitcode(&bc_filename);
bin.bitcode(&bc_filename);

true
}

Some("object") => {
let obj = match binary.code(Generate::Object) {
let obj = match bin.code(Generate::Object) {
Ok(o) => o,
Err(s) => {
println!("error: {s}");
exit(1);
}
};

let obj_filename = output_file(compiler_output, &binary.name, "o", false);
let obj_filename = output_file(compiler_output, &bin.name, "o", false);

if verbose {
eprintln!(
"info: Saving Object {} for contract {}",
obj_filename.display(),
binary.name
bin.name
);
}

Expand All @@ -538,21 +538,21 @@ fn save_intermediates(
true
}
Some("asm") => {
let obj = match binary.code(Generate::Assembly) {
let obj = match bin.code(Generate::Assembly) {
Ok(o) => o,
Err(s) => {
println!("error: {s}");
exit(1);
}
};

let obj_filename = output_file(compiler_output, &binary.name, "asm", false);
let obj_filename = output_file(compiler_output, &bin.name, "asm", false);

if verbose {
eprintln!(
"info: Saving Assembly {} for contract {}",
obj_filename.display(),
binary.name
bin.name
);
}

Expand Down
26 changes: 12 additions & 14 deletions src/emit/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2170,32 +2170,31 @@ pub(super) fn expression<'a, T: TargetRuntime<'a> + ?Sized>(

pub(super) fn compare_address<'a, T: TargetRuntime<'a> + ?Sized>(
target: &T,
binary: &Binary<'a>,
bin: &Binary<'a>,
left: &Expression,
right: &Expression,
op: inkwell::IntPredicate,
vartab: &HashMap<usize, Variable<'a>>,
function: FunctionValue<'a>,
ns: &Namespace,
) -> IntValue<'a> {
let l = expression(target, binary, left, vartab, function, ns).into_array_value();
let r = expression(target, binary, right, vartab, function, ns).into_array_value();
let l = expression(target, bin, left, vartab, function, ns).into_array_value();
let r = expression(target, bin, right, vartab, function, ns).into_array_value();

let left = binary.build_alloca(function, binary.address_type(ns), "left");
let right = binary.build_alloca(function, binary.address_type(ns), "right");
let left = bin.build_alloca(function, bin.address_type(ns), "left");
let right = bin.build_alloca(function, bin.address_type(ns), "right");

binary.builder.build_store(left, l).unwrap();
binary.builder.build_store(right, r).unwrap();
bin.builder.build_store(left, l).unwrap();
bin.builder.build_store(right, r).unwrap();

let res = binary
let res = bin
.builder
.build_call(
binary.module.get_function("__memcmp_ord").unwrap(),
bin.module.get_function("__memcmp_ord").unwrap(),
&[
left.into(),
right.into(),
binary
.context
bin.context
.i32_type()
.const_int(ns.address_length as u64, false)
.into(),
Expand All @@ -2208,9 +2207,8 @@ pub(super) fn compare_address<'a, T: TargetRuntime<'a> + ?Sized>(
.unwrap()
.into_int_value();

binary
.builder
.build_int_compare(op, res, binary.context.i32_type().const_zero(), "")
bin.builder
.build_int_compare(op, res, bin.context.i32_type().const_zero(), "")
.unwrap()
}

Expand Down
50 changes: 21 additions & 29 deletions src/emit/loop_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ pub struct LoopBuilder<'a> {
impl<'a> LoopBuilder<'a> {
/// Create a new loop. This creates the basic blocks and inserts a branch to start of the loop at
/// the current location. This function should be called first.
pub fn new(binary: &Binary<'a>, function: FunctionValue<'a>) -> Self {
let entry_block = binary.builder.get_insert_block().unwrap();
let condition_block = binary.context.append_basic_block(function, "cond");
let body_block = binary.context.append_basic_block(function, "body");
let done_block = binary.context.append_basic_block(function, "done");
pub fn new(bin: &Binary<'a>, function: FunctionValue<'a>) -> Self {
let entry_block = bin.builder.get_insert_block().unwrap();
let condition_block = bin.context.append_basic_block(function, "cond");
let body_block = bin.context.append_basic_block(function, "body");
let done_block = bin.context.append_basic_block(function, "done");

binary
.builder
bin.builder
.build_unconditional_branch(condition_block)
.unwrap();

binary.builder.position_at_end(condition_block);
bin.builder.position_at_end(condition_block);

LoopBuilder {
phis: HashMap::new(),
Expand All @@ -51,12 +50,12 @@ impl<'a> LoopBuilder<'a> {
/// must be given.
pub fn add_loop_phi<T: BasicType<'a>>(
&mut self,
binary: &Binary<'a>,
bin: &Binary<'a>,
name: &'static str,
ty: T,
initial_value: BasicValueEnum<'a>,
) -> BasicValueEnum<'a> {
let phi = binary.builder.build_phi(ty, name).unwrap();
let phi = bin.builder.build_phi(ty, name).unwrap();

phi.add_incoming(&[(&initial_value, self.entry_block)]);

Expand All @@ -70,35 +69,29 @@ impl<'a> LoopBuilder<'a> {
/// builds the condition and then jumps to do the body; the return value is in the index
/// which can be used in the body. The body of the loop can be inserted after calling this
/// function.
pub fn over(
&mut self,
binary: &Binary<'a>,
from: IntValue<'a>,
to: IntValue<'a>,
) -> IntValue<'a> {
pub fn over(&mut self, bin: &Binary<'a>, from: IntValue<'a>, to: IntValue<'a>) -> IntValue<'a> {
let loop_ty = from.get_type();
let loop_phi = binary.builder.build_phi(loop_ty, "index").unwrap();
let loop_phi = bin.builder.build_phi(loop_ty, "index").unwrap();

let loop_var = loop_phi.as_basic_value().into_int_value();

let next = binary
let next = bin
.builder
.build_int_add(loop_var, loop_ty.const_int(1, false), "next_index")
.unwrap();

let comp = binary
let comp = bin
.builder
.build_int_compare(IntPredicate::ULT, loop_var, to, "loop_cond")
.unwrap();

binary
.builder
bin.builder
.build_conditional_branch(comp, self.body_block, self.done_block)
.unwrap();

loop_phi.add_incoming(&[(&from, self.entry_block)]);

binary.builder.position_at_end(self.body_block);
bin.builder.position_at_end(self.body_block);

self.loop_phi = Some(loop_phi);
self.next_index = Some(next);
Expand All @@ -109,29 +102,28 @@ impl<'a> LoopBuilder<'a> {
/// Use this function to set the loop phis to their values at the end of the body
pub fn set_loop_phi_value(
&self,
binary: &Binary<'a>,
bin: &Binary<'a>,
name: &'static str,
value: BasicValueEnum<'a>,
) {
let block = binary.builder.get_insert_block().unwrap();
let block = bin.builder.get_insert_block().unwrap();

self.phis[name].add_incoming(&[(&value, block)]);
}

/// Call this once the body of the loop has been generated. This will close the loop
/// and ensure the exit block has been reached.
pub fn finish(&self, binary: &Binary<'a>) {
let block = binary.builder.get_insert_block().unwrap();
pub fn finish(&self, bin: &Binary<'a>) {
let block = bin.builder.get_insert_block().unwrap();

let loop_phi = self.loop_phi.unwrap();

loop_phi.add_incoming(&[(self.next_index.as_ref().unwrap(), block)]);

binary
.builder
bin.builder
.build_unconditional_branch(self.condition_block)
.unwrap();

binary.builder.position_at_end(self.done_block);
bin.builder.position_at_end(self.done_block);
}
}
16 changes: 8 additions & 8 deletions src/emit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub trait TargetRuntime<'a> {

fn storage_load(
&self,
binary: &Binary<'a>,
bin: &Binary<'a>,
ty: &ast::Type,
slot: &mut IntValue<'a>,
function: FunctionValue<'a>,
Expand All @@ -91,7 +91,7 @@ pub trait TargetRuntime<'a> {
/// Recursively store a type to storage
fn storage_store(
&self,
binary: &Binary<'a>,
bin: &Binary<'a>,
ty: &ast::Type,
existing: bool,
slot: &mut IntValue<'a>,
Expand Down Expand Up @@ -229,7 +229,7 @@ pub trait TargetRuntime<'a> {

fn builtin_function(
&self,
binary: &Binary<'a>,
bin: &Binary<'a>,
function: FunctionValue<'a>,
builtin_func: &Function,
args: &[BasicMetadataValueEnum<'a>],
Expand Down Expand Up @@ -293,10 +293,10 @@ pub trait TargetRuntime<'a> {
fn return_data<'b>(&self, bin: &Binary<'b>, function: FunctionValue<'b>) -> PointerValue<'b>;

/// Return the value we received
fn value_transferred<'b>(&self, binary: &Binary<'b>, ns: &Namespace) -> IntValue<'b>;
fn value_transferred<'b>(&self, bin: &Binary<'b>, ns: &Namespace) -> IntValue<'b>;

/// Terminate execution, destroy bin and send remaining funds to addr
fn selfdestruct<'b>(&self, binary: &Binary<'b>, addr: ArrayValue<'b>, ns: &Namespace);
fn selfdestruct<'b>(&self, bin: &Binary<'b>, addr: ArrayValue<'b>, ns: &Namespace);

/// Crypto Hash
fn hash<'b>(
Expand All @@ -321,7 +321,7 @@ pub trait TargetRuntime<'a> {
/// Return ABI encoded data
fn return_abi_data<'b>(
&self,
binary: &Binary<'b>,
bin: &Binary<'b>,
data: PointerValue<'b>,
data_len: BasicValueEnum<'b>,
);
Expand Down Expand Up @@ -385,8 +385,8 @@ impl ast::Contract {
self.code
.get_or_init(move || {
let context = inkwell::context::Context::create();
let binary = self.binary(ns, &context, opt, contract_no);
binary.code(Generate::Linked).expect("llvm build")
let bin = self.binary(ns, &context, opt, contract_no);
bin.code(Generate::Linked).expect("llvm build")
})
.to_vec()
}
Expand Down
Loading
Loading