8 #include "mlir/CAPI/IR.h"
9 #include "mlir/Parser/Parser.h"
10 #include "mlir/Support/FileUtilities.h"
11 #include "llvm/Support/SourceMgr.h"
19 auto *op =
unwrap(MlirOperation{obj->internalRep.otherValuePtr});
20 llvm::raw_string_ostream stream(str);
22 obj->length = str.length();
23 obj->bytes = Tcl_Alloc(obj->length);
24 memcpy(obj->bytes, str.c_str(), obj->length);
25 obj->bytes[obj->length] =
'\0';
29 auto *op =
unwrap(MlirOperation{src->internalRep.otherValuePtr})->clone();
30 dup->internalRep.otherValuePtr =
wrap(op).ptr;
34 auto *op =
unwrap(MlirOperation{obj->internalRep.otherValuePtr});
39 Tcl_SetObjResult(interp, Tcl_NewStringObj(error, -1));
44 int objc, Tcl_Obj *
const objv[]) {
46 Tcl_WrongNumArgs(interp, objc, objv,
"usage: circt load [MLIR|FIR] [file]");
50 std::string errorMessage;
51 auto input = mlir::openInputFile(llvm::StringRef(Tcl_GetString(objv[2])),
57 llvm::SourceMgr sourceMgr;
58 sourceMgr.AddNewSourceBuffer(std::move(input), llvm::SMLoc());
59 mlir::SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, context);
62 if (!strcmp(Tcl_GetString(objv[1]),
"MLIR"))
63 module =
wrap(mlir::parseSourceFile<mlir::ModuleOp>(sourceMgr, context)
66 else if (!strcmp(Tcl_GetString(objv[1]),
"FIR"))
68 return returnErrorStr(interp,
"loading FIR files is unimplemented :(");
72 if (mlirOperationIsNull(module))
77 auto *obj = Tcl_NewObj();
78 obj->typePtr = Tcl_GetObjType(
"MlirOperation");
79 obj->internalRep.otherValuePtr = (
void *)m;
82 Tcl_SetObjResult(interp, obj);
88 Tcl_Obj *
const objv[]) {
90 Tcl_WrongNumArgs(interp, objc, objv,
"usage: circt load");
94 auto *context = (mlir::MLIRContext *)cdata;
96 if (!strcmp(
"load", Tcl_GetString(objv[1])))
102 static void deleteContext(ClientData data) {
delete (mlir::MLIRContext *)data; }
107 if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL)
111 Tcl_ObjType *operationType =
new Tcl_ObjType;
112 operationType->name =
"MlirOperation";
117 Tcl_RegisterObjType(operationType);
120 if (Tcl_PkgProvide(interp,
"Circt",
"1.0") == TCL_ERROR)
124 auto *context =
new mlir::MLIRContext;
125 context->loadDialect<circt::hw::HWDialect, circt::comb::CombDialect,
126 circt::sv::SVDialect>();
return wrap(CMemoryType::get(unwrap(ctx), baseType, numElements))
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
static int returnErrorStr(Tcl_Interp *interp, const char *error)
static void operationTypeUpdateStringProc(Tcl_Obj *obj)
static int circtTclFunction(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
int DLLEXPORT Circt_Init(Tcl_Interp *interp)
static int loadFirMlirFile(mlir::MLIRContext *context, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
static int operationTypeSetFromAnyProc(Tcl_Interp *interp, Tcl_Obj *obj)
static void operationTypeFreeIntRepProc(Tcl_Obj *obj)
static void deleteContext(ClientData data)
static void operationTypeDupIntRepProc(Tcl_Obj *src, Tcl_Obj *dup)