Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit c9a2123

Browse files
committed
Import of fastcomp commit 4105790
This is a minimal import of the emscripten "fastcomp" LLVM patchset. All it contains is the target definitions necessary to create a TargetMachine with the correct data layout. With this rustc can emit LLVM IR that emcc will run through the PNaCl lagalizer and the JS backend to generate asm.js.
1 parent 854037d commit c9a2123

19 files changed

+594
-0
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ set(LLVM_ALL_TARGETS
321321
BPF
322322
Hexagon
323323
Lanai
324+
JSBackend # @LOCALMOD
324325
Mips
325326
MSP430
326327
NVPTX

include/llvm/ADT/Triple.h

+9
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class Triple {
8282
nvptx64, // NVPTX: 64-bit
8383
le32, // le32: generic little-endian 32-bit CPU (PNaCl)
8484
le64, // le64: generic little-endian 64-bit CPU (PNaCl)
85+
asmjs, // asm.js JavaScript subset @LOCALMOD Emscripten
8586
amdil, // AMDIL
8687
amdil64, // AMDIL with 64-bit pointers
8788
hsail, // AMD HSAIL
@@ -165,6 +166,7 @@ class Triple {
165166
Haiku,
166167
Minix,
167168
RTEMS,
169+
Emscripten, // Emscripten JavaScript runtime @LOCALMOD Emscripten
168170
NaCl, // Native Client
169171
CNK, // BG/P Compute-Node Kernel
170172
Bitrig,
@@ -557,6 +559,13 @@ class Triple {
557559
return getOS() == Triple::NaCl;
558560
}
559561

562+
// @LOCALMOD-START Emscripten
563+
/// Tests whether the OS is Emscripten.
564+
bool isOSEmscripten() const {
565+
return getOS() == Triple::Emscripten;
566+
}
567+
// @LOCALMOD-END Emscripten
568+
560569
/// Tests whether the OS is Linux.
561570
bool isOSLinux() const {
562571
return getOS() == Triple::Linux;

lib/Support/Triple.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ StringRef Triple::getArchTypeName(ArchType Kind) {
5757
case nvptx64: return "nvptx64";
5858
case le32: return "le32";
5959
case le64: return "le64";
60+
case asmjs: return "asmjs"; // @LOCALMOD Emscripten
6061
case amdil: return "amdil";
6162
case amdil64: return "amdil64";
6263
case hsail: return "hsail";
@@ -127,6 +128,8 @@ StringRef Triple::getArchTypePrefix(ArchType Kind) {
127128
case le32: return "le32";
128129
case le64: return "le64";
129130

131+
case asmjs: return "asmjs"; // @LOCALMOD Emscripten
132+
130133
case amdil:
131134
case amdil64: return "amdil";
132135

@@ -192,6 +195,7 @@ StringRef Triple::getOSTypeName(OSType Kind) {
192195
case Haiku: return "haiku";
193196
case Minix: return "minix";
194197
case RTEMS: return "rtems";
198+
case Emscripten: return "emscripten"; // @LOCALMOD Emscripten
195199
case NaCl: return "nacl";
196200
case CNK: return "cnk";
197201
case Bitrig: return "bitrig";
@@ -291,6 +295,7 @@ Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
291295
.Case("nvptx64", nvptx64)
292296
.Case("le32", le32)
293297
.Case("le64", le64)
298+
.Case("asmjs", asmjs) // @LOCALMOD Emscripten
294299
.Case("amdil", amdil)
295300
.Case("amdil64", amdil64)
296301
.Case("hsail", hsail)
@@ -406,6 +411,7 @@ static Triple::ArchType parseArch(StringRef ArchName) {
406411
.Case("nvptx64", Triple::nvptx64)
407412
.Case("le32", Triple::le32)
408413
.Case("le64", Triple::le64)
414+
.Case("asmjs", Triple::asmjs) // @LOCALMOD Emscripten
409415
.Case("amdil", Triple::amdil)
410416
.Case("amdil64", Triple::amdil64)
411417
.Case("hsail", Triple::hsail)
@@ -475,6 +481,7 @@ static Triple::OSType parseOS(StringRef OSName) {
475481
.StartsWith("haiku", Triple::Haiku)
476482
.StartsWith("minix", Triple::Minix)
477483
.StartsWith("rtems", Triple::RTEMS)
484+
.StartsWith("emscripten", Triple::Emscripten) // @LOCALMOD Emscripten
478485
.StartsWith("nacl", Triple::NaCl)
479486
.StartsWith("cnk", Triple::CNK)
480487
.StartsWith("bitrig", Triple::Bitrig)
@@ -617,6 +624,7 @@ static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
617624
case Triple::amdil:
618625
case Triple::amdil64:
619626
case Triple::armeb:
627+
case Triple::asmjs: // @LOCALMOD Emscripten
620628
case Triple::avr:
621629
case Triple::bpfeb:
622630
case Triple::bpfel:
@@ -1170,6 +1178,7 @@ static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
11701178
case llvm::Triple::armeb:
11711179
case llvm::Triple::hexagon:
11721180
case llvm::Triple::le32:
1181+
case llvm::Triple::asmjs: // @LOCALMOD Emscripten
11731182
case llvm::Triple::mips:
11741183
case llvm::Triple::mipsel:
11751184
case llvm::Triple::nios2:
@@ -1254,6 +1263,7 @@ Triple Triple::get32BitArchVariant() const {
12541263
case Triple::hexagon:
12551264
case Triple::kalimba:
12561265
case Triple::le32:
1266+
case Triple::asmjs: // @LOCALMOD Emscripten
12571267
case Triple::mips:
12581268
case Triple::mipsel:
12591269
case Triple::nios2:
@@ -1309,6 +1319,7 @@ Triple Triple::get64BitArchVariant() const {
13091319
case Triple::tce:
13101320
case Triple::tcele:
13111321
case Triple::xcore:
1322+
case Triple::asmjs: // @LOCALMOD Emscripten
13121323
case Triple::sparcel:
13131324
case Triple::shave:
13141325
T.setArch(UnknownArch);
@@ -1368,6 +1379,7 @@ Triple Triple::getBigEndianArchVariant() const {
13681379
case Triple::amdgcn:
13691380
case Triple::amdil64:
13701381
case Triple::amdil:
1382+
case Triple::asmjs:
13711383
case Triple::avr:
13721384
case Triple::hexagon:
13731385
case Triple::hsail64:
@@ -1452,6 +1464,7 @@ bool Triple::isLittleEndian() const {
14521464
case Triple::amdil64:
14531465
case Triple::amdil:
14541466
case Triple::arm:
1467+
case Triple::asmjs:
14551468
case Triple::avr:
14561469
case Triple::bpfel:
14571470
case Triple::hexagon:

lib/Target/JSBackend/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
add_llvm_target(JSBackendCodeGen
2+
JSBackend.cpp
3+
JSTargetMachine.cpp
4+
JSTargetTransformInfo.cpp
5+
)
6+
7+
add_dependencies(LLVMJSBackendCodeGen intrinsics_gen)
8+
9+
add_subdirectory(TargetInfo)
10+
add_subdirectory(MCTargetDesc)

lib/Target/JSBackend/JS.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- JS.h - Top-level interface for JS representation ------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file contains the entry points for global functions defined in the JS
11+
// target library, as used by the LLVM JIT.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef TARGET_JS_H
16+
#define TARGET_JS_H
17+
18+
namespace llvm {
19+
20+
class JSTargetMachine;
21+
22+
} // End llvm namespace
23+
24+
#endif

lib/Target/JSBackend/JSBackend.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- JSBackend.cpp - Library for converting LLVM code to JS -----===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file implements compiling of LLVM IR, which is assumed to have been
11+
// simplified using the PNaCl passes, i64 legalization, and other necessary
12+
// transformations, into JavaScript in asm.js format, suitable for passing
13+
// to emscripten for final processing.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#include "JSTargetMachine.h"
18+
#include "MCTargetDesc/JSBackendMCTargetDesc.h"
19+
#include "llvm/Target/TargetMachine.h"
20+
#include "llvm/Target/TargetSubtargetInfo.h"
21+
#include "llvm/Target/TargetLowering.h"
22+
23+
using namespace llvm;
24+
25+
extern "C" void LLVMInitializeJSBackendTarget() {
26+
// Register the target.
27+
RegisterTargetMachine<JSTargetMachine> X(TheJSBackendTarget);
28+
}
29+
30+
//===----------------------------------------------------------------------===//
31+
// External Interface declaration
32+
//===----------------------------------------------------------------------===//
33+
34+
bool JSTargetMachine::addPassesToEmitFile(
35+
PassManagerBase &PM, raw_pwrite_stream &Out, CodeGenFileType FileType,
36+
bool DisableVerify, AnalysisID StartBefore,
37+
AnalysisID StartAfter, AnalysisID StopBefore, AnalysisID StopAfter) {
38+
return false;
39+
}
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===-- JSTargetMachine.cpp - Define TargetMachine for the JS -------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file defines the JS specific subclass of TargetMachine.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "JSTargetMachine.h"
15+
#include "JSTargetTransformInfo.h"
16+
#include "llvm/Analysis/TargetTransformInfo.h"
17+
#include "llvm/Support/TargetRegistry.h"
18+
using namespace llvm;
19+
20+
extern const llvm::SubtargetFeatureKV JSSubTypeKV[] = {
21+
{ "asmjs", "Select the asmjs processor", { }, { } }
22+
};
23+
24+
static const llvm::SubtargetInfoKV JSProcSchedModels[] = {
25+
{ "asmjs", &MCSchedModel::GetDefaultSchedModel() }
26+
};
27+
28+
JSSubtarget::JSSubtarget(const TargetMachine& TM, const Triple &TT) :
29+
TargetSubtargetInfo(TT, "asmjs", "asmjs", None, makeArrayRef(JSSubTypeKV, 1), JSProcSchedModels, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr),
30+
TL(TM)
31+
{}
32+
33+
34+
JSTargetMachine::JSTargetMachine(const Target &T, const Triple &TT,
35+
StringRef CPU, StringRef FS, const TargetOptions &Options,
36+
Optional<Reloc::Model>& RM, CodeModel::Model CM,
37+
CodeGenOpt::Level OL)
38+
: LLVMTargetMachine(T, "e-p:32:32-i64:64-v128:32:128-n32-S128", TT,
39+
CPU, FS, Options, Reloc::Static, CM, OL),
40+
ST(*this, TT) {
41+
}
42+
43+
TargetIRAnalysis JSTargetMachine::getTargetIRAnalysis() {
44+
return TargetIRAnalysis([this](const Function &F) {
45+
return TargetTransformInfo(JSTTIImpl(this, F));
46+
});
47+
}
48+
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===-- JSTargetMachine.h - TargetMachine for the JS Backend ----*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===---------------------------------------------------------------------===//
9+
//
10+
// This file declares the TargetMachine that is used by the JS/asm.js/
11+
// emscripten backend.
12+
//
13+
//===---------------------------------------------------------------------===//
14+
15+
#ifndef JSTARGETMACHINE_H
16+
#define JSTARGETMACHINE_H
17+
18+
#include "JS.h"
19+
#include "llvm/Target/TargetMachine.h"
20+
#include "llvm/Target/TargetSubtargetInfo.h"
21+
#include "llvm/Target/TargetLowering.h"
22+
23+
namespace llvm {
24+
25+
class formatted_raw_ostream;
26+
27+
class JSTargetLowering : public TargetLowering {
28+
public:
29+
explicit JSTargetLowering(const TargetMachine& TM) : TargetLowering(TM) {}
30+
};
31+
32+
class JSSubtarget : public TargetSubtargetInfo {
33+
JSTargetLowering TL;
34+
35+
public:
36+
JSSubtarget(const TargetMachine& TM, const Triple &TT);
37+
38+
const TargetLowering *getTargetLowering() const override {
39+
return &TL;
40+
}
41+
};
42+
43+
class JSTargetMachine : public LLVMTargetMachine {
44+
const JSSubtarget ST;
45+
46+
public:
47+
JSTargetMachine(const Target &T, const Triple &TT,
48+
StringRef CPU, StringRef FS, const TargetOptions &Options,
49+
Optional<Reloc::Model>& RM, CodeModel::Model CM,
50+
CodeGenOpt::Level OL);
51+
52+
bool addPassesToEmitFile(
53+
PassManagerBase &PM, raw_pwrite_stream &Out, CodeGenFileType FileType,
54+
bool DisableVerify = true, AnalysisID StartBefore = nullptr,
55+
AnalysisID StartAfter = nullptr,
56+
AnalysisID StopBefore = nullptr,
57+
AnalysisID StopAfter = nullptr) override;
58+
59+
TargetIRAnalysis getTargetIRAnalysis() override;
60+
61+
const TargetSubtargetInfo *getJSSubtargetImpl() const {
62+
return &ST;
63+
}
64+
65+
const JSSubtarget *getSubtargetImpl(const Function &F) const override {
66+
return &ST;
67+
}
68+
};
69+
70+
} // End llvm namespace
71+
72+
#endif

0 commit comments

Comments
 (0)