Path: blob/main/contrib/llvm-project/clang/lib/Interpreter/Wasm.cpp
35233 views
//===----------------- Wasm.cpp - Wasm Interpreter --------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements interpreter support for code execution in WebAssembly.9//10//===----------------------------------------------------------------------===//1112#include "Wasm.h"13#include "IncrementalExecutor.h"1415#include <llvm/IR/LegacyPassManager.h>16#include <llvm/IR/Module.h>17#include <llvm/MC/TargetRegistry.h>18#include <llvm/Target/TargetMachine.h>1920#include <clang/Interpreter/Interpreter.h>2122#include <string>2324namespace lld {25enum Flavor {26Invalid,27Gnu, // -flavor gnu28MinGW, // -flavor gnu MinGW29WinLink, // -flavor link30Darwin, // -flavor darwin31Wasm, // -flavor wasm32};3334using Driver = bool (*)(llvm::ArrayRef<const char *>, llvm::raw_ostream &,35llvm::raw_ostream &, bool, bool);3637struct DriverDef {38Flavor f;39Driver d;40};4142struct Result {43int retCode;44bool canRunAgain;45};4647Result lldMain(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,48llvm::raw_ostream &stderrOS, llvm::ArrayRef<DriverDef> drivers);4950namespace wasm {51bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,52llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput);53} // namespace wasm54} // namespace lld5556#include <dlfcn.h>5758namespace clang {5960WasmIncrementalExecutor::WasmIncrementalExecutor(61llvm::orc::ThreadSafeContext &TSC)62: IncrementalExecutor(TSC) {}6364llvm::Error WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {65std::string ErrorString;6667const llvm::Target *Target = llvm::TargetRegistry::lookupTarget(68PTU.TheModule->getTargetTriple(), ErrorString);69if (!Target) {70return llvm::make_error<llvm::StringError>("Failed to create Wasm Target: ",71llvm::inconvertibleErrorCode());72}7374llvm::TargetOptions TO = llvm::TargetOptions();75llvm::TargetMachine *TargetMachine = Target->createTargetMachine(76PTU.TheModule->getTargetTriple(), "", "", TO, llvm::Reloc::Model::PIC_);77PTU.TheModule->setDataLayout(TargetMachine->createDataLayout());78std::string ObjectFileName = PTU.TheModule->getName().str() + ".o";79std::string BinaryFileName = PTU.TheModule->getName().str() + ".wasm";8081std::error_code Error;82llvm::raw_fd_ostream ObjectFileOutput(llvm::StringRef(ObjectFileName), Error);8384llvm::legacy::PassManager PM;85if (TargetMachine->addPassesToEmitFile(PM, ObjectFileOutput, nullptr,86llvm::CodeGenFileType::ObjectFile)) {87return llvm::make_error<llvm::StringError>(88"Wasm backend cannot produce object.", llvm::inconvertibleErrorCode());89}9091if (!PM.run(*PTU.TheModule)) {9293return llvm::make_error<llvm::StringError>("Failed to emit Wasm object.",94llvm::inconvertibleErrorCode());95}9697ObjectFileOutput.close();9899std::vector<const char *> LinkerArgs = {"wasm-ld",100"-shared",101"--import-memory",102"--experimental-pic",103"--stack-first",104"--allow-undefined",105ObjectFileName.c_str(),106"-o",107BinaryFileName.c_str()};108109const lld::DriverDef WasmDriver = {lld::Flavor::Wasm, &lld::wasm::link};110std::vector<lld::DriverDef> WasmDriverArgs;111WasmDriverArgs.push_back(WasmDriver);112lld::Result Result =113lld::lldMain(LinkerArgs, llvm::outs(), llvm::errs(), WasmDriverArgs);114115if (Result.retCode)116return llvm::make_error<llvm::StringError>(117"Failed to link incremental module", llvm::inconvertibleErrorCode());118119void *LoadedLibModule =120dlopen(BinaryFileName.c_str(), RTLD_NOW | RTLD_GLOBAL);121if (LoadedLibModule == nullptr) {122llvm::errs() << dlerror() << '\n';123return llvm::make_error<llvm::StringError>(124"Failed to load incremental module", llvm::inconvertibleErrorCode());125}126127return llvm::Error::success();128}129130llvm::Error WasmIncrementalExecutor::removeModule(PartialTranslationUnit &PTU) {131return llvm::make_error<llvm::StringError>("Not implemented yet",132llvm::inconvertibleErrorCode());133}134135llvm::Error WasmIncrementalExecutor::runCtors() const {136// This seems to be automatically done when using dlopen()137return llvm::Error::success();138}139140llvm::Error WasmIncrementalExecutor::cleanUp() {141// Can't call cleanUp through IncrementalExecutor as it142// tries to deinitialize JIT which hasn't been initialized143return llvm::Error::success();144}145146WasmIncrementalExecutor::~WasmIncrementalExecutor() = default;147148} // namespace clang149150