Skip to content

Commit

Permalink
feat(irdl): added get_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
woxjro committed Oct 2, 2024
1 parent 88faca5 commit f5b2da5
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 29 deletions.
8 changes: 8 additions & 0 deletions mlir/dialect/irdl/michelson.irdl.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ module {
}

// Cryptographic operations
irdl.operation @get_bytes {
%number = irdl.any
%res = irdl.any

irdl.operands(%number)
irdl.results(%res)
}

irdl.operation @sha256 {
%byt = irdl.any
%res = irdl.any
Expand Down
6 changes: 4 additions & 2 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
.stderr;
let json = String::from_utf8(res)?;

println!("{json}");

let deserialized: Block = serde_json::from_str(&json)?;
let smart_contract = get_smart_contract_operation(deserialized)?;

Expand All @@ -40,11 +42,11 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let contents = format!(
"{command_typecheck}{command_mock}{michelson_code}",
command_typecheck = format!(
"#tezos-client --mode mockup --base-dir \
"#octez-client --mode mockup --base-dir \
/tmp/mockup typecheck script {output}\n"
),
command_mock = format!(
"#tezos-client --mode mockup --base-dir /tmp/mockup \
"#octez-client --mode mockup --base-dir /tmp/mockup \
run script {output} on storage '' and input '' --trace-stack\n"
)
);
Expand Down
55 changes: 36 additions & 19 deletions src/michelify/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use michelson_ast::val::Val as MichelsonVal;
#[derive(Debug, Clone)]
pub enum StackType {
Address,
Bytes,
Unit,
Int,
Nat,
Mutez,
Operation,
Option {
Expand Down Expand Up @@ -60,20 +63,35 @@ impl From<Type> for StackType {
}

impl StackType {
pub fn default_value_instruction(&self) -> MichelsonInstruction {
pub fn default_value_instruction(&self) -> Vec<MichelsonInstruction> {
match self {
StackType::Unit => MichelsonInstruction::Unit,
StackType::Address => MichelsonInstruction::Source,
StackType::Mutez => MichelsonInstruction::Push {
StackType::Unit => vec![MichelsonInstruction::Unit],
StackType::Address => vec![MichelsonInstruction::Source],
StackType::Bytes => vec![
MichelsonInstruction::Push {
ty: MichelsonType::Int,
val: MichelsonVal::Int(0),
},
MichelsonInstruction::Bytes,
],
StackType::Int => vec![MichelsonInstruction::Push {
ty: MichelsonType::Int,
val: MichelsonVal::Int(0),
}],
StackType::Nat => vec![MichelsonInstruction::Push {
ty: MichelsonType::Nat,
val: MichelsonVal::Nat(0),
}],
StackType::Mutez => vec![MichelsonInstruction::Push {
ty: MichelsonType::Mutez,
val: MichelsonVal::Mutez(0),
},
StackType::Option { ty } => MichelsonInstruction::None {
}],
StackType::Option { ty } => vec![MichelsonInstruction::None {
ty: MichelsonType::from(ty.as_ref().to_owned()),
},
StackType::List { ty } => MichelsonInstruction::Nil {
}],
StackType::List { ty } => vec![MichelsonInstruction::Nil {
ty: MichelsonType::from(ty.as_ref().to_owned()),
},
}],
ty => todo!("{:?}", ty),
}
}
Expand All @@ -98,11 +116,9 @@ fn stupidly_from(ty: michelson_dialect::Type) -> StackType {
michelson_dialect::Type::Contract { ty } => StackType::Contract {
ty: Box::new(stupidly_from(ty.as_ref().to_owned())),
},
michelson_dialect::Type::Bytes
| michelson_dialect::Type::Int
| michelson_dialect::Type::Nat => {
todo!()
}
michelson_dialect::Type::Bytes => StackType::Bytes,
michelson_dialect::Type::Int => StackType::Int,
michelson_dialect::Type::Nat => StackType::Nat,
}
}

Expand All @@ -111,7 +127,10 @@ impl From<StackType> for MichelsonType {
fn from(stack_type: StackType) -> MichelsonType {
match stack_type {
StackType::Address => MichelsonType::Address,
StackType::Bytes => MichelsonType::Bytes,
StackType::Unit => MichelsonType::Unit,
StackType::Int => MichelsonType::Int,
StackType::Nat => MichelsonType::Nat,
StackType::Mutez => MichelsonType::Mutez,
StackType::Operation => MichelsonType::Operation,
StackType::Option { ty } => MichelsonType::Option {
Expand Down Expand Up @@ -152,11 +171,9 @@ impl From<michelson_dialect::Type> for MichelsonType {
ty: Box::new(MichelsonType::from(*ty)),
},
michelson_dialect::Type::Address => MichelsonType::Address,
michelson_dialect::Type::Bytes
| michelson_dialect::Type::Int
| michelson_dialect::Type::Nat => {
todo!()
}
michelson_dialect::Type::Bytes => MichelsonType::Bytes,
michelson_dialect::Type::Int => MichelsonType::Int,
michelson_dialect::Type::Nat => MichelsonType::Nat,
}
}
}
Expand Down
46 changes: 38 additions & 8 deletions src/michelify/phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,19 @@ pub fn stack_initialization(
continue; // parameter と storage の初期値の処理をスキップ
}
let stack_type: StackType = value.get_type().into();
michelson_instructions.push(instruction_row!(
stack_type.default_value_instruction(),
format!(
"{} : {}",
value.get_id(),
MichelsonType::from(value.get_type()).to_string()
)
));
stack_type
.default_value_instruction()
.iter()
.for_each(|instr| {
michelson_instructions.push(instruction_row!(
instr.to_owned(),
format!(
"{} : {}",
value.get_id(),
MichelsonType::from(value.get_type()).to_string()
)
));
});
}

michelson_instructions.push(instruction_row!(MichelsonInstruction::Comment(
Expand Down Expand Up @@ -387,6 +392,31 @@ pub fn compile_operations(
.collect::<Vec<_>>(),
);
}
michelson::ast::Operation::GetBytesOp { number, result } => {
let operand_address =
(*get_address_closure)(GetAddressClosureArg::Value(number.get_value()));
let result_address =
(*get_address_closure)(GetAddressClosureArg::Value(result.get_value()));

instructions.append(
&mut vec![
MichelsonInstruction::Comment(format!(
"{} = michelson.get_bytes({}) {{",
result.get_value().get_id(),
number.get_value().get_id()
)),
MichelsonInstruction::DupN(operand_address),
MichelsonInstruction::Bytes,
MichelsonInstruction::DigN(result_address),
MichelsonInstruction::Drop,
MichelsonInstruction::DugN(result_address - 1),
MichelsonInstruction::Comment("}".to_string()),
]
.iter()
.map(|instr| instr.to_wrapped_instruction())
.collect::<Vec<_>>(),
);
}
michelson::ast::Operation::Sha256Op { result, bytes } => {
let operand_address =
(*get_address_closure)(GetAddressClosureArg::Value(bytes.get_value()));
Expand Down
11 changes: 11 additions & 0 deletions src/mlir/dialect/michelson/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ pub enum Operation {
fst: ast::Operand,
snd: ast::Operand,
},
GetBytesOp {
result: ast::Result,
number: ast::Operand,
},
Sha256Op {
result: ast::Result,
bytes: ast::Operand,
Expand All @@ -107,6 +111,7 @@ enum OperationKind {
GetContractOp,
MakeListOp,
MakePairOp,
GetBytesOp,
Sha256Op,
Sha3Op,
Sha512Op,
Expand All @@ -124,6 +129,7 @@ impl ToString for OperationKind {
OperationKind::GetContractOp => "get_contract".to_owned(),
OperationKind::MakeListOp => "make_list".to_owned(),
OperationKind::MakePairOp => "make_pair".to_owned(),
OperationKind::GetBytesOp => "get_bytes".to_owned(),
OperationKind::Sha256Op => "sha256".to_owned(),
OperationKind::Sha3Op => "sha3".to_owned(),
OperationKind::Sha512Op => "sha512".to_owned(),
Expand Down Expand Up @@ -180,6 +186,11 @@ impl From<ast::Operation> for Operation {
address: operation.operands[0].to_owned(),
result: operation.results[0].to_owned(),
}
} else if operation.get_mnemonic() == OperationKind::GetBytesOp.to_string() {
Operation::GetBytesOp {
number: operation.operands[0].to_owned(),
result: operation.results[0].to_owned(),
}
} else if operation.get_mnemonic() == OperationKind::Sha256Op.to_string() {
Operation::Sha256Op {
bytes: operation.operands[0].to_owned(),
Expand Down

0 comments on commit f5b2da5

Please sign in to comment.