|
| 1 | +//===--- Nbdl.cpp - Nbdl binding syntax for HeavyScheme ---------*- C++ -*-===// |
| 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 | +// This file defines syntax (nbdl impl) bindings for HeavyScheme. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include <heavy/Context.h> |
| 14 | +#include <heavy/Nbdl.h> |
| 15 | +#include <heavy/Value.h> |
| 16 | +#include <nbdl_gen/Dialect.h> |
| 17 | +#include <llvm/Support/Casting.h> |
| 18 | +#include <memory> |
| 19 | +#include <tuple> |
| 20 | + |
| 21 | +namespace nbdl_gen { |
| 22 | +std::tuple<std::string, mlir::Location, mlir::Operation*> |
| 23 | +translate_cpp(llvm::raw_ostream& OS, mlir::Operation* Op); |
| 24 | +} |
| 25 | + |
| 26 | +namespace heavy::nbdl_bind_var { |
| 27 | +heavy::ExternFunction write_cpp; |
| 28 | +} |
| 29 | + |
| 30 | +namespace heavy::nbdl_bind { |
| 31 | +// Translate a nbdl dialect operation to C++. |
| 32 | +void write_cpp(Context& C, ValueRefs Args) { |
| 33 | + if (Args.size() != 2) |
| 34 | + return C.RaiseError("invalid arity"); |
| 35 | + auto* Op = dyn_cast<mlir::Operation>(Args[0]); |
| 36 | + // Do not captured the emphemeral Tagged object. |
| 37 | + auto* Tagged = dyn_cast<heavy::Tagged>(Args[1]); |
| 38 | + heavy::Symbol* KindSym = C.CreateSymbol("::llvm::raw_ostream"); |
| 39 | + if (!Op) |
| 40 | + return C.RaiseError("expecting mlir.operation"); |
| 41 | + if (!Tagged || !Tagged->isa(KindSym)) |
| 42 | + return C.RaiseError("expecting ::llvm::raw_ostream"); |
| 43 | + |
| 44 | + auto& OS = Tagged->cast<llvm::raw_ostream>(); |
| 45 | + auto&& [ErrMsg, ErrLoc, Irritant] = nbdl_gen::translate_cpp(OS, Op); |
| 46 | + if (!ErrMsg.empty()) { |
| 47 | + auto Loc = heavy::SourceLocation(mlir::OpaqueLoc |
| 48 | + ::getUnderlyingLocationOrNull<heavy::SourceLocationEncoding*>( |
| 49 | + mlir::dyn_cast<mlir::OpaqueLoc>(ErrLoc))); |
| 50 | + heavy::Error* Err = C.CreateError(Loc, ErrMsg, |
| 51 | + Irritant ? heavy::Value(Irritant) : Undefined()); |
| 52 | + return C.Raise(Err); |
| 53 | + } |
| 54 | + C.Cont(); |
| 55 | +} |
| 56 | +} |
| 57 | + |
| 58 | +extern "C" { |
| 59 | +// initialize the module for run-time independent of the compiler |
| 60 | +void HEAVY_NBDL_INIT(heavy::Context& C) { |
| 61 | + C.DialectRegistry->insert<nbdl_gen::NbdlDialect>(); |
| 62 | + |
| 63 | + heavy::nbdl_bind_var::write_cpp = heavy::nbdl_bind::write_cpp; |
| 64 | +} |
| 65 | + |
| 66 | +void HEAVY_NBDL_LOAD_MODULE(heavy::Context& C) { |
| 67 | + HEAVY_NBDL_INIT(C); |
| 68 | + heavy::initModuleNames(C, HEAVY_NBDL_LIB_STR, { |
| 69 | + {"write-cpp", heavy::nbdl_bind_var::write_cpp}, |
| 70 | + }); |
| 71 | +} |
| 72 | +} |
0 commit comments