[go: nahoru, domu]

Skip to content

Commit

Permalink
wip: circular codesize dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Feb 26, 2023
1 parent d463868 commit 40c7b7e
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 45 deletions.
94 changes: 57 additions & 37 deletions huff_codegen/src/irgen/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn statement_gen(
label_indices: &mut LabelIndices,
table_instances: &mut Jumps,
utilized_tables: &mut Vec<TableDefinition>,
circular_codesize_invocations: &mut CircularCodeSizeIndices,
starting_offset: usize,
) -> Result<Vec<(usize, Bytes)>, CodegenError> {
let mut bytes = vec![];
Expand All @@ -36,7 +37,7 @@ pub fn statement_gen(
kind: CodegenErrorKind::InvalidMacroInvocation(mi.macro_name.clone()),
span: mi.span.clone(),
token: None,
})
});
};

tracing::info!(target: "codegen", "FOUND INNER MACRO: {}", ir_macro.name);
Expand All @@ -48,7 +49,7 @@ pub fn statement_gen(
kind: CodegenErrorKind::TestInvocation(ir_macro.name.clone()),
span: ir_macro.span,
token: None,
})
});
}

// If invoked macro is a function (outlined), insert a jump to the function's code and a
Expand Down Expand Up @@ -110,7 +111,7 @@ pub fn statement_gen(
"FAILED TO RECURSE INTO MACRO \"{}\"",
ir_macro.name
);
return Err(e)
return Err(e);
}
};

Expand Down Expand Up @@ -177,36 +178,55 @@ pub fn statement_gen(
),
span: bf.span.clone(),
token: None,
})
};

let res: BytecodeRes = match Codegen::macro_to_bytecode(
ir_macro.clone(),
contract,
scope,
*offset,
mis,
ir_macro.name.eq("CONSTRUCTOR"),
) {
Ok(r) => r,
Err(e) => {
tracing::error!(
target: "codegen",
"FAILED TO RECURSE INTO MACRO \"{}\"",
ir_macro.name
);
return Err(e)
}
});
};

let size = format_even_bytes(format!(
"{:02x}",
(res.bytes.iter().map(|(_, b)| b.0.len()).sum::<usize>() / 2)
));
let push_bytes = format!("{:02x}{size}", 95 + size.len() / 2);
// Special case:
// If the macro provided to __codesize is the current macro, we need to avoid a circular reference
// If this is the case we will store a place holder inside the bytecode and fill it in later when
// we have adequate information about the macros eventual size.
//
// We
// TODO: remove this unwrap / clone
if bf.args[0].name.clone().unwrap() == ir_macro.name {
tracing::debug!(target: "codegen", "CIRCULAR CODESIZE INVOCATION DETECTED INJECTING PLACEHOLDER | macro: {}", ir_macro.name);

// Save the invocation for later
circular_codesize_invocations.insert(*offset);

// Progress offset by placeholder size
*offset += 2;
bytes.push((starting_offset, Bytes(format!("cccc"))));
} else {
// We will still need to recurse to get accurate values
let res: BytecodeRes = match Codegen::macro_to_bytecode(
ir_macro.clone(),
contract,
scope,
*offset,
mis,
ir_macro.name.eq("CONSTRUCTOR"),
) {
Ok(r) => r,
Err(e) => {
tracing::error!(
target: "codegen",
"FAILED TO RECURSE INTO MACRO \"{}\"",
ir_macro.name
);
return Err(e);
}
};

let size = format_even_bytes(format!(
"{:02x}",
(res.bytes.iter().map(|(_, b)| b.0.len()).sum::<usize>() / 2)
));
let push_bytes = format!("{:02x}{size}", 95 + size.len() / 2);

*offset += push_bytes.len() / 2;
bytes.push((starting_offset, Bytes(push_bytes)));
*offset += push_bytes.len() / 2;
bytes.push((starting_offset, Bytes(push_bytes)));
}
}
BuiltinFunctionKind::Tablesize => {
let ir_table = if let Some(t) =
Expand All @@ -225,7 +245,7 @@ pub fn statement_gen(
),
span: bf.span.clone(),
token: None,
})
});
};

let size = bytes32_to_string(&ir_table.size, false);
Expand Down Expand Up @@ -265,7 +285,7 @@ pub fn statement_gen(
),
span: bf.span.clone(),
token: None,
})
});
}
}
BuiltinFunctionKind::FunctionSignature => {
Expand Down Expand Up @@ -324,7 +344,7 @@ pub fn statement_gen(
),
span: bf.span.clone(),
token: None,
})
});
}
}
BuiltinFunctionKind::EventHash => {
Expand Down Expand Up @@ -374,7 +394,7 @@ pub fn statement_gen(
),
span: bf.span.clone(),
token: None,
})
});
}
}
BuiltinFunctionKind::Error => {
Expand All @@ -391,7 +411,7 @@ pub fn statement_gen(
)),
span: bf.span.clone(),
token: None,
})
});
}

if let Some(error) = contract
Expand All @@ -417,7 +437,7 @@ pub fn statement_gen(
),
span: bf.span.clone(),
token: None,
})
});
}
}
BuiltinFunctionKind::RightPad => {
Expand Down Expand Up @@ -550,7 +570,7 @@ pub fn statement_gen(
kind: CodegenErrorKind::InvalidMacroStatement,
span: s.span.clone(),
token: None,
})
});
}
}

Expand Down
Loading

0 comments on commit 40c7b7e

Please sign in to comment.