Skip to content

Commit e33baa9

Browse files
Merge private (#64)
* update submodule * Revert "update submodule" This reverts commit d296326. * initial implementation of builtin option * Merge correct llvm submodule (#62) update submodule Signed-off-by: ASDAlexander77 <duzhar@googlemail.com> * initial implementation of builtin option Signed-off-by: ASDAlexander77 <duzhar@googlemail.com> --------- Signed-off-by: ASDAlexander77 <duzhar@googlemail.com>
1 parent 41122a0 commit e33baa9

File tree

5 files changed

+131
-10
lines changed

5 files changed

+131
-10
lines changed

tsc/include/TypeScript/DataStructs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ struct CompileOptions
99
{
1010
bool isJit;
1111
bool disableGC;
12+
bool enableBuiltins;
1213
bool disableWarnings;
1314
bool generateDebugInfo;
1415
bool lldbDebugInfo;

tsc/include/TypeScript/MLIRLogic/MLIRCodeLogic.h

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,11 @@ class MLIRCustomMethods
196196
{
197197
mlir::OpBuilder &builder;
198198
mlir::Location &location;
199+
CompileOptions &compileOptions;
199200

200201
public:
201-
MLIRCustomMethods(mlir::OpBuilder &builder, mlir::Location &location) : builder(builder), location(location)
202+
MLIRCustomMethods(mlir::OpBuilder &builder, mlir::Location &location, CompileOptions &compileOptions)
203+
: builder(builder), location(location), compileOptions(compileOptions)
202204
{
203205
}
204206

@@ -208,13 +210,28 @@ class MLIRCustomMethods
208210
return o[objectName.str()];
209211
}
210212

211-
static bool isInternalFunctionName (StringRef functionName)
213+
static bool isInternalFunctionName (CompileOptions &compileOptions, StringRef functionName)
214+
{
215+
return compileOptions.enableBuiltins
216+
? isInternalFunctionNameBuiltin(functionName)
217+
: isInternalFunctionNameNoBuiltin(functionName);
218+
}
219+
220+
static bool isInternalFunctionNameBuiltin (StringRef functionName)
212221
{
213222
static std::map<std::string, bool> m {
214223
{"print", true}, {"assert", true}, {"parseInt", true}, {"parseFloat", true}, {"isNaN", true}, {"sizeof", true}, {GENERATOR_SWITCHSTATE, true},
215224
{"LoadLibraryPermanently", true}, { "SearchForAddressOfSymbol", true }, { "LoadReference", true }};
216225
return m[functionName.str()];
217-
}
226+
}
227+
228+
static bool isInternalFunctionNameNoBuiltin (StringRef functionName)
229+
{
230+
static std::map<std::string, bool> m {
231+
{"print", true}, {"assert", true}, {"sizeof", true}, {GENERATOR_SWITCHSTATE, true},
232+
{"LoadLibraryPermanently", true}, { "SearchForAddressOfSymbol", true }, { "LoadReference", true }};
233+
return m[functionName.str()];
234+
}
218235

219236
ValueOrLogicalResult callMethod(StringRef functionName, ArrayRef<mlir::Value> operands, const GenContext &genContext)
220237
{
@@ -228,16 +245,16 @@ class MLIRCustomMethods
228245
// assert - internal command;
229246
return mlirGenAssert(location, operands);
230247
}
231-
else if (functionName == "parseInt")
248+
else if (compileOptions.enableBuiltins && functionName == "parseInt")
232249
{
233250
// assert - internal command;
234251
return mlirGenParseInt(location, operands);
235252
}
236-
else if (functionName == "parseFloat")
253+
else if (compileOptions.enableBuiltins && functionName == "parseFloat")
237254
{
238255
return mlirGenParseFloat(location, operands);
239256
}
240-
else if (functionName == "isNaN")
257+
else if (compileOptions.enableBuiltins && functionName == "isNaN")
241258
{
242259
return mlirGenIsNaN(location, operands);
243260
}

tsc/lib/TypeScript/MLIRGen.cpp

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9597,7 +9597,7 @@ class MLIRGenImpl
95979597
}
95989598

95999599
// resolve function
9600-
MLIRCustomMethods cm(builder, location);
9600+
MLIRCustomMethods cm(builder, location, compileOptions);
96019601
return cm.callMethod(functionName, operands, genContext);
96029602
}
96039603

@@ -11443,7 +11443,7 @@ class MLIRGenImpl
1144311443

1144411444
ValueOrLogicalResult createDynamicArrayFromArrayLiteral(mlir::Location location, ArrayRef<ArrayElement> values, struct ArrayInfo arrayInfo, const GenContext &genContext)
1144511445
{
11446-
MLIRCustomMethods cm(builder, location);
11446+
MLIRCustomMethods cm(builder, location, compileOptions);
1144711447
SmallVector<mlir::Value> emptyArrayValues;
1144811448
auto arrType = getArrayType(arrayInfo.arrayElementType);
1144911449
auto newArrayOp = builder.create<mlir_ts::CreateArrayOp>(location, arrType, emptyArrayValues);
@@ -12624,7 +12624,7 @@ class MLIRGenImpl
1262412624
return value;
1262512625
}
1262612626

12627-
if (MLIRCustomMethods::isInternalFunctionName(name))
12627+
if (MLIRCustomMethods::isInternalFunctionName(compileOptions, name))
1262812628
{
1262912629
auto symbOp = builder.create<mlir_ts::SymbolRefOp>(
1263012630
location, builder.getNoneType(), mlir::FlatSymbolRefAttr::get(builder.getContext(), name));
@@ -17325,8 +17325,13 @@ genContext);
1732517325

1732617326
return mlir::Type();
1732717327
}
17328-
17328+
1732917329
bool isEmbededType(mlir::StringRef name)
17330+
{
17331+
return compileOptions.enableBuiltins ? isEmbededTypeWithBuiltins(name) : isEmbededTypeWithNoBuiltins(name);
17332+
}
17333+
17334+
bool isEmbededTypeWithBuiltins(mlir::StringRef name)
1733017335
{
1733117336
static llvm::StringMap<bool> embeddedTypes {
1733217337
{"TemplateStringsArray", true },
@@ -17385,7 +17390,40 @@ genContext);
1738517390
return type;
1738617391
}
1738717392

17393+
bool isEmbededTypeWithNoBuiltins(mlir::StringRef name)
17394+
{
17395+
static llvm::StringMap<bool> embeddedTypes {
17396+
{"TemplateStringsArray", true },
17397+
{"const", true },
17398+
#ifdef ENABLE_JS_BUILTIN_TYPES
17399+
{"Number", true },
17400+
{"Object", true },
17401+
{"String", true },
17402+
{"Boolean", true },
17403+
{"Function", true },
17404+
#endif
17405+
17406+
{"TypeOf", true },
17407+
{"Opague", true }, // to support void*
17408+
{"Reference", true }, // to support dll import
17409+
{"ThisType", true },
17410+
#ifdef ENABLE_JS_BUILTIN_TYPES
17411+
{"Awaited", true },
17412+
{"Promise", true },
17413+
#endif
17414+
{"Array", true }
17415+
};
17416+
17417+
auto type = embeddedTypes[name];
17418+
return type;
17419+
}
17420+
1738817421
mlir::Type getEmbeddedType(mlir::StringRef name)
17422+
{
17423+
return compileOptions.enableBuiltins ? getEmbeddedTypeBuiltins(name) : getEmbeddedTypeNoBuiltins(name);
17424+
}
17425+
17426+
mlir::Type getEmbeddedTypeBuiltins(mlir::StringRef name)
1738917427
{
1739017428
static llvm::StringMap<mlir::Type> embeddedTypes {
1739117429
{"TemplateStringsArray", getArrayType(getStringType()) },
@@ -17416,8 +17454,35 @@ genContext);
1741617454
return type;
1741717455
}
1741817456

17457+
mlir::Type getEmbeddedTypeNoBuiltins(mlir::StringRef name)
17458+
{
17459+
static llvm::StringMap<mlir::Type> embeddedTypes {
17460+
{"TemplateStringsArray", getArrayType(getStringType()) },
17461+
{"const",getConstType() },
17462+
#ifdef ENABLE_JS_BUILTIN_TYPES
17463+
{"Number", getNumberType() },
17464+
{"Object", getObjectType(getAnyType()) },
17465+
{"String", getStringType()},
17466+
{"Boolean", getBooleanType()},
17467+
{"Function", getFunctionType({getArrayType(getAnyType())}, {getAnyType()}, true)},
17468+
#endif
17469+
{"Opaque", getOpaqueType()},
17470+
};
17471+
17472+
auto type = embeddedTypes[name];
17473+
return type;
17474+
}
17475+
1741917476
mlir::Type getEmbeddedTypeWithParam(mlir::StringRef name, NodeArray<TypeNode> &typeArguments,
1742017477
const GenContext &genContext)
17478+
{
17479+
return compileOptions.enableBuiltins
17480+
? getEmbeddedTypeWithParamBuiltins(name, typeArguments, genContext)
17481+
: getEmbeddedTypeWithParamNoBuiltins(name, typeArguments, genContext);
17482+
}
17483+
17484+
mlir::Type getEmbeddedTypeWithParamBuiltins(mlir::StringRef name, NodeArray<TypeNode> &typeArguments,
17485+
const GenContext &genContext)
1742117486
{
1742217487
auto translate = llvm::StringSwitch<std::function<mlir::Type(NodeArray<TypeNode> &, const GenContext &)>>(name)
1742317488
.Case("TypeOf", [&] (auto typeArguments, auto genContext) {
@@ -17532,8 +17597,41 @@ genContext);
1753217597
return translate(typeArguments, genContext);
1753317598
}
1753417599

17600+
mlir::Type getEmbeddedTypeWithParamNoBuiltins(mlir::StringRef name, NodeArray<TypeNode> &typeArguments,
17601+
const GenContext &genContext)
17602+
{
17603+
auto translate = llvm::StringSwitch<std::function<mlir::Type(NodeArray<TypeNode> &, const GenContext &)>>(name)
17604+
.Case("TypeOf", [&] (auto typeArguments, auto genContext) {
17605+
auto type = getFirstTypeFromTypeArguments(typeArguments, genContext);
17606+
type = mth.wideStorageType(type);
17607+
return type;
17608+
})
17609+
.Case("Reference", [&] (auto typeArguments, auto genContext) {
17610+
auto type = getFirstTypeFromTypeArguments(typeArguments, genContext);
17611+
return mlir_ts::RefType::get(type);
17612+
})
17613+
.Case("ThisType", std::bind(&MLIRGenImpl::getFirstTypeFromTypeArguments, this, std::placeholders::_1, std::placeholders::_2))
17614+
.Case("Array", [&] (auto typeArguments, auto genContext) {
17615+
auto elemnentType = getFirstTypeFromTypeArguments(typeArguments, genContext);
17616+
return getArrayType(elemnentType);
17617+
})
17618+
.Default([] (auto, auto) {
17619+
return mlir::Type();
17620+
});
17621+
17622+
return translate(typeArguments, genContext);
17623+
}
17624+
1753517625
mlir::Type getEmbeddedTypeWithManyParams(mlir::StringRef name, NodeArray<TypeNode> &typeArguments,
1753617626
const GenContext &genContext)
17627+
{
17628+
return compileOptions.enableBuiltins
17629+
? getEmbeddedTypeWithManyParamsBuiltins(name, typeArguments, genContext)
17630+
: mlir::Type();
17631+
}
17632+
17633+
mlir::Type getEmbeddedTypeWithManyParamsBuiltins(mlir::StringRef name, NodeArray<TypeNode> &typeArguments,
17634+
const GenContext &genContext)
1753717635
{
1753817636
auto translate = llvm::StringSwitch<std::function<mlir::Type(NodeArray<TypeNode> &, const GenContext &)>>(name)
1753917637
.Case("Exclude", [&] (auto typeArguments, auto genContext) {

tsc/tsc/opts.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extern cl::opt<bool> generateDebugInfo;
2020
extern cl::opt<bool> lldbDebugInfo;
2121
extern cl::opt<std::string> TargetTriple;
2222
extern cl::opt<enum Exports> exportAction;
23+
extern cl::opt<bool> enableBuiltins;
2324

2425
// obj
2526
extern cl::opt<std::string> TargetTriple;
@@ -35,6 +36,7 @@ CompileOptions prepareOptions()
3536
CompileOptions compileOptions;
3637
compileOptions.isJit = emitAction == Action::RunJIT;
3738
compileOptions.disableGC = disableGC;
39+
compileOptions.enableBuiltins = enableBuiltins;
3840
compileOptions.disableWarnings = disableWarnings;
3941
compileOptions.exportOpt = exportAction;
4042
compileOptions.generateDebugInfo = generateDebugInfo;

tsc/tsc/tsc.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ cl::opt<enum Action> emitAction("emit", cl::desc("Select the kind of output desi
7171
cl::values(clEnumValN(BuildDll, "dll", "build Shared library (.so/.dylib) file")),
7272
#endif
7373
cl::values(clEnumValN(RunJIT, "jit", "JIT code and run it by invoking main function")),
74+
cl::init(RunJIT),
7475
cl::cat(TypeScriptCompilerCategory));
7576

7677
cl::opt<bool> enableOpt{"opt", cl::desc("Enable optimizations"), cl::init(false), cl::cat(TypeScriptCompilerCategory), cl::cat(TypeScriptCompilerCategory)};
@@ -106,6 +107,8 @@ cl::opt<std::string> tsclibpath("tsc-lib-path", cl::desc("TypeScript Compiler Ru
106107
cl::opt<std::string> emsdksysrootpath("emsdk-sysroot-path", cl::desc("TypeScript Compiler Runtime library path. Should point to dir '<...>/emsdk/upstream/emscripten/cache/sysroot' or EMSDK_SYSROOT_PATH environmental variable. (used when '-mtriple=wasm32-pc-emscripten')"), cl::value_desc("emsdksysrootpath"), cl::cat(TypeScriptCompilerBuildCategory));
107108
cl::list<std::string> libs{"lib", cl::desc("Libraries to link statically. (used in --emit=exe)"), cl::ZeroOrMore, cl::MiscFlags::CommaSeparated, cl::cat(TypeScriptCompilerBuildCategory)};
108109

110+
cl::opt<bool> enableBuiltins("builtins", cl::desc("Builtin functionality (needed if Default lib is not provided)"), cl::init(true), cl::cat(TypeScriptCompilerCategory));
111+
109112
static void TscPrintVersion(llvm::raw_ostream &OS) {
110113
OS << "TypeScript Native Compiler (https://github.com/ASDAlexander77/TypeScriptCompiler):" << '\n';
111114
OS << " TSNC version " << TSC_PACKAGE_VERSION << '\n' << '\n';

0 commit comments

Comments
 (0)