|
37 | 37 | #include <optional> |
38 | 38 | #include <sstream> |
39 | 39 |
|
| 40 | +#ifdef CXX_WITH_MLIR |
| 41 | +#include <cxx/mlir/codegen.h> |
| 42 | +#include <cxx/mlir/cxx_dialect.h> |
| 43 | +#include <llvm/Support/raw_os_ostream.h> |
| 44 | +#include <mlir/IR/MLIRContext.h> |
| 45 | +#endif |
| 46 | + |
40 | 47 | using namespace emscripten; |
41 | 48 |
|
42 | 49 | namespace { |
@@ -179,6 +186,31 @@ struct WrappedUnit { |
179 | 186 |
|
180 | 187 | co_return val{true}; |
181 | 188 | } |
| 189 | + |
| 190 | + auto emitIR() -> std::string { |
| 191 | +#ifdef CXX_WITH_MLIR |
| 192 | + mlir::MLIRContext context; |
| 193 | + context.loadDialect<mlir::cxx::CxxDialect>(); |
| 194 | + |
| 195 | + cxx::Codegen codegen(context, unit.get()); |
| 196 | + |
| 197 | + auto ir = codegen(unit->ast()); |
| 198 | + |
| 199 | + mlir::OpPrintingFlags flags; |
| 200 | + flags.enableDebugInfo(true, true); |
| 201 | + |
| 202 | + std::ostringstream out; |
| 203 | + llvm::raw_os_ostream os(out); |
| 204 | + ir.module->print(os, flags); |
| 205 | + os.flush(); |
| 206 | + |
| 207 | + auto code = out.str(); |
| 208 | + |
| 209 | + return code; |
| 210 | +#else |
| 211 | + return {}; |
| 212 | +#endif |
| 213 | + } |
182 | 214 | }; |
183 | 215 |
|
184 | 216 | auto getTokenText(std::intptr_t handle, std::intptr_t unitHandle) |
@@ -381,7 +413,8 @@ EMSCRIPTEN_BINDINGS(cxx) { |
381 | 413 | .function("parse", &WrappedUnit::parse) |
382 | 414 | .function("getHandle", &WrappedUnit::getHandle) |
383 | 415 | .function("getUnitHandle", &WrappedUnit::getUnitHandle) |
384 | | - .function("getDiagnostics", &WrappedUnit::getDiagnostics); |
| 416 | + .function("getDiagnostics", &WrappedUnit::getDiagnostics) |
| 417 | + .function("emitIR", &WrappedUnit::emitIR); |
385 | 418 |
|
386 | 419 | function("createUnit", &createUnit, allow_raw_pointers()); |
387 | 420 | function("getASTKind", &getASTKind); |
|
0 commit comments