Skip to content

Commit

Permalink
feat(compiler): added supports for the following ops
Browse files Browse the repository at this point in the history
- sha256
- sha3
- sha512
  • Loading branch information
woxjro committed Oct 2, 2024
1 parent aaf2383 commit c482e1d
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 19 deletions.
10 changes: 10 additions & 0 deletions src/michelify/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ 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!()
}
}
}

Expand Down Expand Up @@ -147,6 +152,11 @@ 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!()
}
}
}
}
Expand Down
95 changes: 87 additions & 8 deletions src/michelify/phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ pub fn stack_initialization(
type_heap_addresses: &HashMap<Type, usize>,
) -> Vec<MWrappedInstr> {
let mut michelson_instructions = vec![
instruction_row!(MichelsonInstruction::Comment("------ stack initialization ------ {".to_string())),
instruction_row!(MichelsonInstruction::Comment(
"------ stack initialization ------ {".to_string()
)),
//TODO: 引数が Option で包まなければいけない型の場合の処理をする
instruction_row!(
MichelsonInstruction::Unpair,
Expand Down Expand Up @@ -128,7 +130,9 @@ pub fn stack_initialization(
));
}

michelson_instructions.push(instruction_row!(MichelsonInstruction::Comment("---------------------------------- }".to_string())));
michelson_instructions.push(instruction_row!(MichelsonInstruction::Comment(
"---------------------------------- }".to_string()
)));

michelson_instructions
}
Expand All @@ -154,9 +158,9 @@ pub fn compile_operations(
(*get_address_closure)(GetAddressClosureArg::Value(result.get_value()));
instructions.append(
&mut [MichelsonInstruction::Comment(format!(
"{} = michelson.get_unit() {{ }}",
result.get_value().get_id()
))]
"{} = michelson.get_unit() {{ }}",
result.get_value().get_id()
))]
.iter()
.map(|instr| instr.to_wrapped_instruction())
.collect::<Vec<_>>(),
Expand All @@ -167,9 +171,9 @@ pub fn compile_operations(
(*get_address_closure)(GetAddressClosureArg::Value(result.get_value()));
instructions.append(
&mut [MichelsonInstruction::Comment(format!(
"{} = michelson.get_source() {{ }}",
result.get_value().get_id()
))]
"{} = michelson.get_source() {{ }}",
result.get_value().get_id()
))]
.iter()
.map(|instr| instr.to_wrapped_instruction())
.collect::<Vec<_>>(),
Expand Down Expand Up @@ -383,6 +387,81 @@ pub fn compile_operations(
.collect::<Vec<_>>(),
);
}
michelson::ast::Operation::Sha256Op { result, bytes } => {
let operand_address =
(*get_address_closure)(GetAddressClosureArg::Value(bytes.get_value()));
let result_address =
(*get_address_closure)(GetAddressClosureArg::Value(result.get_value()));

instructions.append(
&mut vec![
MichelsonInstruction::Comment(format!(
"{} = michelson.sha256({}) {{",
result.get_value().get_id(),
bytes.get_value().get_id()
)),
MichelsonInstruction::DupN(operand_address),
MichelsonInstruction::Sha256,
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::Sha3Op { result, bytes } => {
let operand_address =
(*get_address_closure)(GetAddressClosureArg::Value(bytes.get_value()));
let result_address =
(*get_address_closure)(GetAddressClosureArg::Value(result.get_value()));

instructions.append(
&mut vec![
MichelsonInstruction::Comment(format!(
"{} = michelson.sha3({}) {{",
result.get_value().get_id(),
bytes.get_value().get_id()
)),
MichelsonInstruction::DupN(operand_address),
MichelsonInstruction::Sha3,
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::Sha512Op { result, bytes } => {
let operand_address =
(*get_address_closure)(GetAddressClosureArg::Value(bytes.get_value()));
let result_address =
(*get_address_closure)(GetAddressClosureArg::Value(result.get_value()));

instructions.append(
&mut vec![
MichelsonInstruction::Comment(format!(
"{} = michelson.sha512({}) {{",
result.get_value().get_id(),
bytes.get_value().get_id()
)),
MichelsonInstruction::DupN(operand_address),
MichelsonInstruction::Sha512,
MichelsonInstruction::DigN(result_address),
MichelsonInstruction::Drop,
MichelsonInstruction::DugN(result_address - 1),
MichelsonInstruction::Comment("}".to_string()),
]
.iter()
.map(|instr| instr.to_wrapped_instruction())
.collect::<Vec<_>>(),
);
}
}
}
mlir::dialect::Operation::FuncOp { operation } => {
Expand Down
50 changes: 39 additions & 11 deletions src/mlir/dialect/michelson/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ lalrpop_mod!(pub mlir_parser);
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Type {
Address,
Bytes,
Unit,
Int,
Nat,
Mutez,
Operation,
Option { ty: Box<Type> },
Expand All @@ -20,17 +23,6 @@ impl Type {
pub fn get_dialect(&self) -> dialect::DialectKind {
dialect::DialectKind::Michelson
}

/// 型がスタックに積める値を持つか否かを判定する関数
/// 例えば
/// - Type::Mutez は 0 をというスタックに積める値をもつため true
/// - Type::Address は 初期値に相当する値は無いため false
pub fn has_initial_value(&self) -> bool {
match self {
Type::Pair { .. } => false,
_ => true,
}
}
}

impl From<String> for Type {
Expand All @@ -41,7 +33,10 @@ impl From<String> for Type {

pub enum Tok {
Unit,
Int,
Nat,
Address,
Bytes,
Contract,
Option,
Mutez,
Expand Down Expand Up @@ -88,6 +83,18 @@ pub enum Operation {
fst: ast::Operand,
snd: ast::Operand,
},
Sha256Op {
result: ast::Result,
bytes: ast::Operand,
},
Sha3Op {
result: ast::Result,
bytes: ast::Operand,
},
Sha512Op {
result: ast::Result,
bytes: ast::Operand,
},
}

enum OperationKind {
Expand All @@ -100,6 +107,9 @@ enum OperationKind {
GetContractOp,
MakeListOp,
MakePairOp,
Sha256Op,
Sha3Op,
Sha512Op,
}

impl ToString for OperationKind {
Expand All @@ -114,6 +124,9 @@ impl ToString for OperationKind {
OperationKind::GetContractOp => "get_contract".to_owned(),
OperationKind::MakeListOp => "make_list".to_owned(),
OperationKind::MakePairOp => "make_pair".to_owned(),
OperationKind::Sha256Op => "sha256".to_owned(),
OperationKind::Sha3Op => "sha3".to_owned(),
OperationKind::Sha512Op => "sha512".to_owned(),
}
}
}
Expand Down Expand Up @@ -167,6 +180,21 @@ impl From<ast::Operation> for Operation {
address: 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(),
result: operation.results[0].to_owned(),
}
} else if operation.get_mnemonic() == OperationKind::Sha3Op.to_string() {
Operation::Sha3Op {
bytes: operation.operands[0].to_owned(),
result: operation.results[0].to_owned(),
}
} else if operation.get_mnemonic() == OperationKind::Sha512Op.to_string() {
Operation::Sha512Op {
bytes: operation.operands[0].to_owned(),
result: operation.results[0].to_owned(),
}
} else {
todo!("'{}' is an unsupported operation", operation.get_mnemonic())
}
Expand Down
6 changes: 6 additions & 0 deletions src/mlir_parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ grammar;

pub MType: MType = {
<address:Address> => MType::Address,
<bytes:Bytes> => MType::Bytes,
<unit:Unit> => MType::Unit,
<int:Int> => MType::Int,
<nat:Nat> => MType::Nat,
<mutez:Mutez> => MType::Mutez,
<operation:Operation> => MType::Operation,
<list:List> "<" <ty:MType> ">" => MType::List { ty: Box::new(ty) },
Expand All @@ -24,8 +27,11 @@ pub FType: FType = {

Unit: Tok = <s:r"[!][a-zA-Z]+[.]unit"> => Tok::Unit;
Address: Tok = <s:r"[!][a-zA-Z]+[.]address"> => Tok::Address;
Bytes: Tok = <s:r"[!][a-zA-Z]+[.]bytes"> => Tok::Bytes;
Option: Tok = <s:r"[!][a-zA-Z]+[.]option"> => Tok::Option;
Mutez: Tok = <s:r"[!][a-zA-Z]+[.]mutez"> => Tok::Mutez;
Int: Tok = <s:r"[!][a-zA-Z]+[.]int"> => Tok::Int;
Nat: Tok = <s:r"[!][a-zA-Z]+[.]nat"> => Tok::Nat;
Contract: Tok = <s:r"[!][a-zA-Z]+[.]contract"> => Tok::Contract;
Operation: Tok = <s:r"[!][a-zA-Z]+[.]operation"> => Tok::Operation;
List: Tok = <s:r"[!][a-zA-Z]+[.]list"> => Tok::List;
Expand Down

0 comments on commit c482e1d

Please sign in to comment.