CIRCT  19.0.0git
SVModule.cpp
Go to the documentation of this file.
1 //===- SVModule.cpp - SV API pybind module --------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "DialectModules.h"
10 
11 #include "circt-c/Dialect/SV.h"
12 #include "mlir-c/Bindings/Python/Interop.h"
13 
14 #include "mlir/Bindings/Python/PybindAdaptors.h"
15 #include "mlir/CAPI/IR.h"
16 #include "mlir/CAPI/Support.h"
17 
18 #include "llvm/ADT/SmallVector.h"
19 
20 #include "PybindUtils.h"
21 #include <pybind11/pybind11.h>
22 #include <pybind11/stl.h>
23 namespace py = pybind11;
24 
25 using namespace mlir::python::adaptors;
26 
28  m.doc() = "SV Python Native Extension";
29 
30  mlir_attribute_subclass(m, "SVAttributeAttr", svAttrIsASVAttributeAttr)
31  .def_classmethod(
32  "get",
33  [](py::object cls, std::string name, py::object expressionObj,
34  bool emitAsComment, MlirContext ctxt) {
35  // Need temporary storage for casted string.
36  std::string expr;
37  MlirStringRef expression = {nullptr, 0};
38  if (!expressionObj.is_none()) {
39  expr = expressionObj.cast<std::string>();
40  expression = mlirStringRefCreateFromCString(expr.c_str());
41  }
42  return cls(svSVAttributeAttrGet(
43  ctxt, mlirStringRefCreateFromCString(name.c_str()), expression,
44  emitAsComment));
45  },
46  "Create a SystemVerilog attribute", py::arg(), py::arg("name"),
47  py::arg("expression") = py::none(),
48  py::arg("emit_as_comment") = py::none(), py::arg("ctxt") = py::none())
49  .def_property_readonly("name",
50  [](MlirAttribute self) {
51  MlirStringRef name =
53  return std::string(name.data, name.length);
54  })
55  .def_property_readonly(
56  "expression",
57  [](MlirAttribute self) -> py::object {
58  MlirStringRef name = svSVAttributeAttrGetExpression(self);
59  if (name.data == nullptr)
60  return py::none();
61  return py::str(std::string(name.data, name.length));
62  })
63  .def_property_readonly("emit_as_comment", [](MlirAttribute self) {
65  });
66 }
MLIR_CAPI_EXPORTED MlirAttribute svSVAttributeAttrGet(MlirContext, MlirStringRef name, MlirStringRef expression, bool emitAsComment)
Definition: SV.cpp:26
MLIR_CAPI_EXPORTED MlirStringRef svSVAttributeAttrGetExpression(MlirAttribute)
Definition: SV.cpp:42
MLIR_CAPI_EXPORTED bool svSVAttributeAttrGetEmitAsComment(MlirAttribute)
Definition: SV.cpp:49
MLIR_CAPI_EXPORTED bool svAttrIsASVAttributeAttr(MlirAttribute)
Definition: SV.cpp:22
MLIR_CAPI_EXPORTED MlirStringRef svSVAttributeAttrGetName(MlirAttribute)
Definition: SV.cpp:38
void populateDialectSVSubmodule(pybind11::module &m)