-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CheerpWritePass moved to /CheerpWriter/
- Loading branch information
1 parent
4d38664
commit fdf6a7d
Showing
4 changed files
with
154 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
//===-- CheerpWritePass.cpp - Pass writer for CheerpWriter ----------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the Apache License v2.0 with LLVM Exceptions. | ||
// See LICENSE.TXT for details. | ||
// | ||
// Copyright 2011-2023 Leaning Technologies | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Cheerp/CheerpWritePass.h" | ||
#include "llvm/Support/FileSystem.h" | ||
#include "llvm/Support/Path.h" | ||
#include "llvm/Cheerp/WasmWriter.h" | ||
#include "llvm/Cheerp/DTSWriter.h" | ||
using namespace llvm; | ||
|
||
PreservedAnalyses cheerp::CheerpWritePassImpl::run(Module& M, ModuleAnalysisManager& MAM) | ||
{ | ||
cheerp::Registerize ®isterize = MAM.getResult<cheerp::RegisterizeAnalysis>(M); | ||
cheerp::GlobalDepsAnalyzer &GDA = MAM.getResult<cheerp::GlobalDepsAnalysis>(M); | ||
cheerp::PointerAnalyzer &PA = MAM.getResult<cheerp::PointerAnalysis>(M); | ||
cheerp::InvokeWrapping &IW = MAM.getResult<cheerp::InvokeWrappingAnalysis>(M); | ||
cheerp::AllocaStoresExtractor &allocaStoresExtractor = MAM.getResult<cheerp::AllocaStoresExtractorAnalysis>(M); | ||
cheerp::LinearMemoryHelper &linearHelper = MAM.getResult<LinearMemoryAnalysis>(M); | ||
std::unique_ptr<cheerp::SourceMapGenerator> sourceMapGenerator; | ||
GDA.forceTypedArrays = ForceTypedArrays; | ||
if (!SourceMap.empty()) | ||
{ | ||
std::error_code ErrorCode; | ||
sourceMapGenerator.reset(new cheerp::SourceMapGenerator(SourceMap, SourceMapPrefix, SourceMapStandAlone, ErrorCode)); | ||
if (ErrorCode) | ||
{ | ||
// An error occurred opening the source map file, bail out | ||
llvm::report_fatal_error(StringRef(ErrorCode.message()), false); | ||
return PreservedAnalyses::none(); | ||
} | ||
} | ||
PA.fullResolve(); | ||
PA.computeConstantOffsets(M); | ||
// Destroy the stores here, we need them to properly compute the pointer kinds, but we want to optimize them away before registerize | ||
allocaStoresExtractor.unlinkStores(); | ||
|
||
registerize.assignRegisters(M, PA); | ||
#ifdef REGISTERIZE_STATS | ||
cheerp::reportRegisterizeStatistics(); | ||
#endif | ||
|
||
Triple TargetTriple(M.getTargetTriple()); | ||
bool WasmOnly = TargetTriple.getOS() == Triple::WASI; | ||
std::error_code ErrorCode; | ||
llvm::ToolOutputFile secondaryFile(SecondaryOutputFile, ErrorCode, sys::fs::OF_None); | ||
std::unique_ptr<llvm::formatted_raw_ostream> secondaryOut; | ||
if (!SecondaryOutputFile.empty()) | ||
{ | ||
secondaryOut.reset(new formatted_raw_ostream(secondaryFile.os())); | ||
} | ||
else if (WasmOnly && LinearOutput != AsmJs) | ||
{ | ||
secondaryOut.reset(new formatted_raw_ostream(Out)); | ||
} | ||
|
||
std::error_code dtsErrorCode; | ||
llvm::ToolOutputFile dtsFile(DTSOutputFile, dtsErrorCode, sys::fs::OF_None); | ||
std::unique_ptr<llvm::formatted_raw_ostream> dtsOut; | ||
if (!DTSOutputFile.empty()) | ||
{ | ||
dtsOut.reset(new formatted_raw_ostream(dtsFile.os())); | ||
} | ||
|
||
// Build the ordered list of reserved names | ||
std::vector<std::string> reservedNames(ReservedNames.begin(), ReservedNames.end()); | ||
std::sort(reservedNames.begin(), reservedNames.end()); | ||
|
||
cheerp::NameGenerator namegen(M, GDA, registerize, PA, linearHelper, reservedNames, PrettyCode, WasmExportedMemory); | ||
|
||
std::string wasmFile; | ||
std::string asmjsMemFile; | ||
llvm::formatted_raw_ostream* memOut = nullptr; | ||
switch (LinearOutput) | ||
{ | ||
case Wasm: | ||
if (!SecondaryOutputPath.empty()) | ||
wasmFile = SecondaryOutputPath.getValue(); | ||
else if (!SecondaryOutputFile.empty()) | ||
wasmFile = std::string(llvm::sys::path::filename(SecondaryOutputFile.getValue())); | ||
break; | ||
case AsmJs: | ||
if (!SecondaryOutputPath.empty()) | ||
asmjsMemFile = SecondaryOutputPath.getValue(); | ||
else if (!SecondaryOutputFile.empty()) | ||
asmjsMemFile = std::string(llvm::sys::path::filename(SecondaryOutputFile.getValue())); | ||
memOut = secondaryOut.get(); | ||
break; | ||
} | ||
|
||
MODULE_TYPE makeModule = getModuleType(MakeModule); | ||
|
||
if (MakeDTS && dtsOut) | ||
{ | ||
cheerp::CheerpDTSWriter dtsWriter(M, *dtsOut, sourceMapGenerator.get(), PrettyCode, makeModule); | ||
dtsWriter.makeDTS(); | ||
} | ||
|
||
if (!WasmOnly) | ||
{ | ||
cheerp::CheerpWriter writer(M, MAM, Out, PA, registerize, GDA, linearHelper, namegen, allocaStoresExtractor, IW.getLandingPadTable(), memOut, asmjsMemFile, | ||
sourceMapGenerator.get(), PrettyCode, makeModule, !NoNativeJavaScriptMath, | ||
!NoJavaScriptMathImul, !NoJavaScriptMathFround, !NoCredits, MeasureTimeToMain, CheerpHeapSize, | ||
BoundsCheck, SymbolicGlobalsAsmJS, wasmFile, ForceTypedArrays); | ||
writer.makeJS(); | ||
} | ||
|
||
if (LinearOutput != AsmJs && secondaryOut) | ||
{ | ||
cheerp::CheerpWasmWriter wasmWriter(M, MAM, *secondaryOut, PA, registerize, GDA, linearHelper, IW.getLandingPadTable(), namegen, | ||
M.getContext(), CheerpHeapSize, !WasmOnly, | ||
PrettyCode, WasmSharedMemory, | ||
WasmExportedTable); | ||
wasmWriter.makeWasm(); | ||
} | ||
|
||
allocaStoresExtractor.destroyStores(); | ||
|
||
if (!SecondaryOutputFile.empty() && ErrorCode) | ||
{ | ||
// An error occurred opening the asm.js memory file, bail out | ||
llvm::report_fatal_error(StringRef(ErrorCode.message()), false); | ||
return PreservedAnalyses::none(); | ||
} | ||
if (!DTSOutputFile.empty() && dtsErrorCode) | ||
{ | ||
llvm::report_fatal_error(StringRef(dtsErrorCode.message()), false); | ||
return PreservedAnalyses::none(); | ||
} | ||
if (!WasmOnly) | ||
secondaryFile.keep(); | ||
if (MakeDTS) | ||
dtsFile.keep(); | ||
|
||
|
||
return PreservedAnalyses::none(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters