Skip to content

Commit

Permalink
Towards special variable support
Browse files Browse the repository at this point in the history
  • Loading branch information
troelsfr committed Oct 4, 2023
1 parent 9d300b4 commit bdbe558
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 12 deletions.
11 changes: 0 additions & 11 deletions products/bluebell/core/src/evm_bytecode_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,6 @@ impl<'ctx> EvmBytecodeGenerator<'ctx> {

match qualified_name.as_str() {
"String" => {
/*
let ssa_name = match instr
.ssa_name
.clone()
.unwrap()
.qualified_name()
{
Ok(v) => v,
_ => panic!("Could not resolve SSA qualified name"),
};
*/
let payload = data.clone();
evm_block.set_next_rust_position(file!().to_string(), line!() as usize);
let payload = payload[1..payload.len()-1].as_bytes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,22 @@ impl AstConverting for IrEmitter {
});
self.stack.push(StackObject::Instruction(instr));
}
NodeVariableIdentifier::SpecialIdentifier(_identifier) => unimplemented!(),
NodeVariableIdentifier::SpecialIdentifier(identifier) => {
let operation = Operation::ResolveContextResource {
symbol: IrIdentifier::new(
identifier.to_string(),
IrIndentifierKind::ContextResource,
self.current_location(),
),
};
let instr = Box::new(Instruction {
ssa_name: None,
result_type: None,
operation,
source_location: self.current_location(),
});
self.stack.push(StackObject::Instruction(instr));
}
NodeVariableIdentifier::VariableInNamespace(_namespace, _identifier) => {
unimplemented!()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ impl PassExecutor for Operation {
symbol.visit(pass, symbol_table)?;
Ok(TraversalResult::Continue)
}
Operation::ResolveContextResource { symbol } => {
symbol.visit(pass, symbol_table)?;
Ok(TraversalResult::Continue)
}
Operation::Literal { data: _, typename } => {
typename.visit(pass, symbol_table)?;
Ok(TraversalResult::Continue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub enum IrIndentifierKind {
Namespace,
BlockLabel,

ContextResource,

// Storage and reference
VirtualRegister,
VirtualRegisterIntermediate,
Expand Down Expand Up @@ -231,6 +233,9 @@ pub enum Operation {
ResolveSymbol {
symbol: IrIdentifier,
},
ResolveContextResource {
symbol: IrIdentifier,
},
Literal {
data: String,
typename: IrIdentifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ impl SymbolTable {
let _ = ret.declare_type("Uint32");
let _ = ret.declare_type("Uint64");
let _ = ret.declare_type("String");
let _ = ret.declare_type("ByStr32");

let _ = ret.declare_special_variable("_sender", "ByStr32");

ret
}
Expand Down Expand Up @@ -203,6 +206,14 @@ impl SymbolTable {
Ok(symbol.to_string())
}

pub fn declare_special_variable(
&mut self,
name: &str,
typename: &str,
) -> Result<String, String> {
self.declare_type_of(name, typename)
}

pub fn declare_type(&mut self, symbol: &str) -> Result<String, String> {
self.declare_type_of(symbol, symbol)
}
Expand Down
12 changes: 12 additions & 0 deletions products/bluebell/core/src/passes/annotate_base_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,18 @@ impl IrPass for AnnotateBaseTypes {
}
}
}
Operation::ResolveContextResource { symbol } => {
symbol.visit(self, symbol_table)?;
match &symbol.type_reference {
Some(t) => t.clone(),
None => {
return Err(format!(
"Unable to determine type for {}",
symbol.unresolved
));
}
}
}
Operation::Literal { data, typename } => {
typename.visit(self, symbol_table)?;

Expand Down
5 changes: 5 additions & 0 deletions products/bluebell/core/src/passes/debug_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl IrPass for DebugPrinter {
IrIndentifierKind::Event => self.script.push_str("@"),
IrIndentifierKind::Namespace => self.script.push_str("@"),
IrIndentifierKind::BlockLabel => self.script.push_str(":"),
IrIndentifierKind::ContextResource => self.script.push_str("~"),
IrIndentifierKind::VirtualRegister => self.script.push_str("%"),
IrIndentifierKind::VirtualRegisterIntermediate => self.script.push_str("%"),
IrIndentifierKind::Memory => self.script.push_str("%"),
Expand Down Expand Up @@ -239,6 +240,10 @@ impl IrPass for DebugPrinter {
Operation::ResolveSymbol { symbol } => {
symbol.visit(self, symbol_table)?;
}
Operation::ResolveContextResource { symbol } => {
symbol.visit(self, symbol_table)?;
}

Operation::Literal { data, typename } => {
typename.visit(self, symbol_table)?;
self.script.push_str(" ");
Expand Down
14 changes: 14 additions & 0 deletions products/bluebell/core/src/support/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ impl BluebellModule for ScillaDefaultTypes {
context.declare_unsigned_integer("Uint128", 128);
context.declare_unsigned_integer("Uint256", 256);

for i in 0..=32 {
context.declare_unsigned_integer(&format!("ByStr{}", i), i * 8);
}

context.declare_dynamic_string("String");
}
}
Expand Down Expand Up @@ -335,6 +339,16 @@ impl BluebellModule for ScillaDefaultBuiltins {
});
*/

let _ = specification.declare_special_variable(
"_sender",
"ByteStr32",
|_ctx, block, _arg_types| {
// TODO: Check that the number of arguments is two and otherwise return an error
unimplemented!();
Ok([].to_vec())
},
);

// Assuming you have a 'specification' object available...
// Implementing `add`:
let _ = specification.declare_inline_generics("builtin__add", |_ctx, block, _arg_types| {
Expand Down
15 changes: 15 additions & 0 deletions products/bluebell/evm_assembly/src/compiler_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct EvmCompilerContext {

pub function_declarations: HashMap<String, EvmFunctionSignature>,
pub inline_generics: HashMap<String, InlineGenericsFn>,
pub special_variables: HashMap<String, InlineGenericsFn>,

/// Scilla types -> EVM types
precompiles: BTreeMap<H160, PrecompileFn>,
Expand Down Expand Up @@ -66,6 +67,7 @@ impl EvmCompilerContext {
type_declarations: HashMap::new(),
function_declarations: HashMap::new(),
inline_generics: HashMap::new(),
special_variables: HashMap::new(),
precompile_addresses: HashMap::new(),
precompiles: BTreeMap::new(),
contract_offset: 5,
Expand Down Expand Up @@ -108,6 +110,19 @@ impl EvmCompilerContext {
// .insert(name.to_string(), EvmType::Opaque);
}

pub fn declare_special_variable(
&mut self,
name: &str,
typename: &str,
builder: InlineGenericsFn,
) -> Result<(), String> {
if self.special_variables.contains_key(name) {
return Err(format!("Special variable {} already exists", name).to_string());
}
self.special_variables.insert(name.to_string(), builder);
Ok(())
}

pub fn declare_inline_generics(
&mut self,
name: &str,
Expand Down

0 comments on commit bdbe558

Please sign in to comment.