CIRCT 20.0.0git
Loading...
Searching...
No Matches
SVModule.cpp
Go to the documentation of this file.
1//===- SVModule.cpp - SV API nanobind 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 "CIRCTModules.h"
10
11#include "circt-c/Dialect/SV.h"
12#include "mlir-c/Bindings/Python/Interop.h"
13
14#include "mlir/Bindings/Python/NanobindAdaptors.h"
15#include "mlir/CAPI/IR.h"
16#include "mlir/CAPI/Support.h"
17
18#include "llvm/ADT/SmallVector.h"
19
20#include "NanobindUtils.h"
21#include <nanobind/nanobind.h>
22#include <nanobind/stl/string.h>
23namespace nb = nanobind;
24
25using namespace mlir::python::nanobind_adaptors;
26
28 m.doc() = "SV Python Native Extension";
29
30 mlir_attribute_subclass(m, "SVAttributeAttr", svAttrIsASVAttributeAttr)
31 .def_classmethod(
32 "get",
33 [](nb::object cls, std::string name, nb::object expressionObj,
34 nb::object emitAsCommentObj, MlirContext ctxt) {
35 // Set emitAsComment from optional boolean flag.
36 bool emitAsComment = false;
37 if (!emitAsCommentObj.is_none())
38 emitAsComment = nb::cast<bool>(emitAsCommentObj);
39
40 // Need temporary storage for casted string.
41 std::string expr;
42 MlirStringRef expression = {nullptr, 0};
43 if (!expressionObj.is_none()) {
44 expr = nb::cast<std::string>(expressionObj);
45 expression = mlirStringRefCreateFromCString(expr.c_str());
46 }
47 return cls(svSVAttributeAttrGet(
48 ctxt, mlirStringRefCreateFromCString(name.c_str()), expression,
49 emitAsComment));
50 },
51 "Create a SystemVerilog attribute", nb::arg(), nb::arg("name"),
52 nb::arg("expression") = nb::none(),
53 nb::arg("emit_as_comment") = nb::none(), nb::arg("ctxt") = nb::none())
54 .def_property_readonly("name",
55 [](MlirAttribute self) {
56 MlirStringRef name =
58 return std::string(name.data, name.length);
59 })
60 .def_property_readonly("expression",
61 [](MlirAttribute self) -> nb::object {
62 MlirStringRef name =
64 if (name.data == nullptr)
65 return nb::none();
66 return nb::str(name.data, name.length);
67 })
68 .def_property_readonly("emit_as_comment", [](MlirAttribute self) {
70 });
71}
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(nanobind::module_ &m)