helpers.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: Apache-2.0
  2. use indexmap::IndexMap;
  3. use num_bigint::BigInt;
  4. use solang::{
  5. lir::{
  6. expressions::{BinaryOperator, Expression, Operand, UnaryOperator},
  7. lir_type::{LIRType, Type},
  8. printer::Printer,
  9. vartable::Vartable,
  10. },
  11. sema::ast,
  12. };
  13. use solang_parser::pt::Loc;
  14. pub(crate) fn binop_expr(left: Operand, op: BinaryOperator, right: Operand) -> Expression {
  15. Expression::BinaryExpr {
  16. loc: Loc::Codegen,
  17. operator: op,
  18. left: Box::new(left),
  19. right: Box::new(right),
  20. }
  21. }
  22. pub(crate) fn unop_expr(op: UnaryOperator, right: Operand) -> Expression {
  23. Expression::UnaryExpr {
  24. loc: Loc::Codegen,
  25. operator: op,
  26. right: Box::new(right),
  27. }
  28. }
  29. pub(crate) fn num_literal(value: i32, signed: bool, width: u16) -> Operand {
  30. Operand::NumberLiteral {
  31. value: BigInt::from(value),
  32. ty: if signed {
  33. new_lir_type(Type::Int(width))
  34. } else {
  35. new_lir_type(Type::Uint(width))
  36. },
  37. loc: Loc::Codegen,
  38. }
  39. }
  40. #[macro_export]
  41. macro_rules! stringfy_expr {
  42. ($printer:expr, $expr:expr) => {{
  43. let mut buffer = Vec::new();
  44. $printer.print_expr(&mut buffer, $expr);
  45. String::from_utf8(buffer).expect("Failed to convert to string")
  46. }};
  47. }
  48. #[macro_export]
  49. macro_rules! stringfy_insn {
  50. ($printer:expr, $insn:expr) => {{
  51. let mut buf = Vec::new();
  52. $printer.print_instruction(&mut buf, $insn);
  53. String::from_utf8(buf).unwrap()
  54. }};
  55. }
  56. #[macro_export]
  57. macro_rules! stringfy_lir {
  58. ($printer:expr, $lir:expr) => {{
  59. let mut buf = Vec::new();
  60. $printer.print_lir(&mut buf, $lir);
  61. String::from_utf8(buf).unwrap()
  62. }};
  63. }
  64. #[macro_export]
  65. macro_rules! num_literal {
  66. ($value: expr, $width: expr) => {
  67. num_literal($value, false, $width)
  68. };
  69. ($value: expr) => {
  70. num_literal($value, false, 8)
  71. };
  72. // error
  73. () => {
  74. panic!("invalid number literal")
  75. };
  76. }
  77. #[macro_export]
  78. macro_rules! new_printer {
  79. () => {
  80. new_printer(&new_vartable())
  81. };
  82. }
  83. pub(crate) fn bool_literal(value: bool) -> Operand {
  84. Operand::BoolLiteral {
  85. value,
  86. loc: Loc::Codegen,
  87. }
  88. }
  89. pub(crate) fn identifier(id: usize) -> Operand {
  90. Operand::Id {
  91. id,
  92. loc: Loc::Codegen,
  93. }
  94. }
  95. pub fn new_printer(v: &Vartable) -> Printer {
  96. Printer::new(v)
  97. }
  98. pub fn new_vartable() -> Vartable {
  99. Vartable {
  100. vars: IndexMap::new(),
  101. args: IndexMap::new(),
  102. next_id: 0,
  103. }
  104. }
  105. pub fn set_tmp(v: &mut Vartable, id: usize, ty: Type) {
  106. v.set_tmp(
  107. id,
  108. LIRType {
  109. lir_type: ty,
  110. ast_type: /*mock value*/ ast::Type::Void,
  111. },
  112. );
  113. }
  114. pub fn new_lir_type(ty: Type) -> LIRType {
  115. LIRType {
  116. lir_type: ty,
  117. ast_type: /*mock value*/ ast::Type::Void,
  118. }
  119. }