Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/how/cmake_bgfx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ if(TSLANG_MEMORY_MODEL STREQUAL "gc")
list(APPEND TSLANG_LINK_LIBS gc)
endif()

# legacy_stdio_definitions: printf, sprintf_s and the rest of stdio as functions compiled code can
# call (the UCRT has them only as inline wrappers in its headers)
if(WIN32)
list(APPEND TSLANG_LINK_LIBS ntdll)
list(APPEND TSLANG_LINK_LIBS ntdll legacy_stdio_definitions)
else()
find_library(TSLANG_TINFO_LIB NAMES tinfo)
list(APPEND TSLANG_LINK_LIBS LLVMDemangle stdc++ m pthread dl rt)
Expand Down
6 changes: 4 additions & 2 deletions docs/how/cmake_dll/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ if (TSLANG_MEMORY_MODEL STREQUAL "gc")
list(APPEND TSLANG_LINK_LIBS "gcdll/gc")
endif()

# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows
# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows; legacy_stdio_definitions
# printf, sprintf_s and the rest of stdio as functions compiled code can call (the UCRT has them only
# as inline wrappers in its headers)
if(WIN32)
list(APPEND TSLANG_LINK_LIBS "ntdll")
list(APPEND TSLANG_LINK_LIBS "ntdll" "legacy_stdio_definitions")
else()
list(APPEND TSLANG_LINK_LIBS "LLVMDemangle" "stdc++" "m" "pthread" "tinfo" "dl" "rt")
endif()
Expand Down
6 changes: 4 additions & 2 deletions docs/how/cmake_tslang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ if (TSLANG_MEMORY_MODEL STREQUAL "gc")
endif()
endif()

# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows
# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows; legacy_stdio_definitions
# printf, sprintf_s and the rest of stdio as functions compiled code can call (the UCRT has them only
# as inline wrappers in its headers)
if(WIN32)
list(APPEND TSLANG_LINK_LIBS "ntdll")
list(APPEND TSLANG_LINK_LIBS "ntdll" "legacy_stdio_definitions")
else()
list(APPEND TSLANG_LINK_LIBS "LLVMDemangle" "stdc++" "m" "pthread" "tinfo" "dl" "rt")
endif()
Expand Down
4 changes: 3 additions & 1 deletion docs/how/cmake_winapp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ target_link_directories(
${GCLIBPATH}
)

set (LIBS "gcmt-lib")
# legacy_stdio_definitions: printf, sprintf_s and the rest of stdio as functions compiled code can
# call (the UCRT has them only as inline wrappers in its headers)
set (LIBS "gcmt-lib" "legacy_stdio_definitions")

target_link_libraries(
${PROJECT_NAME}
Expand Down
12 changes: 12 additions & 0 deletions tslang/include/TypeScript/DOM.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,19 @@ class VariableDeclarationDOM
invariant = value;
};

// A global that holds the address of the variable rather than the variable: another module's,
// imported from its library (-shared) - reads and writes go through it to the library's own
bool getIndirect() const
{
return indirect;
}
void setIndirect(bool value = true)
{
indirect = value;
}

protected:
bool indirect = false;
bool readWrite;
bool atomic;
int ordering;
Expand Down
11 changes: 11 additions & 0 deletions tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,17 @@
// a C function pointer - one code pointer, not the code-and-context a TypeScript function value is.
// Only a function that captures nothing can be passed there (TSLANG_ISSUES 29).
#define CDECL_DECORATOR "cdecl"
// `@stdcall declare function GetCurrentProcessId(): u32`: f is __stdcall, as the Windows API is. On 32-bit
// Windows that is how it is called (the callee pops its arguments) and named (_GetCurrentProcessId@0,
// which is what kernel32.lib exports); on every other target it is the C convention, so the same
// declaration serves both. On a function with a body it is a callback the Windows API calls, as
// `@stdcall function entry(arg: Opaque): u32` for _beginthreadex. It marks the function
// STDCALL_ATTR_NAME, and CxxThrowCallingConvPass gives the function and its calls the convention.
#define STDCALL_DECORATOR "stdcall"
#define STDCALL_ATTR_NAME "tslang.stdcall"
// A library exports a `@stdcall` function, on 32-bit Windows, as a C function under its own name - which an
// importer's calls go through - and as itself under its name with this suffix, which an importer gives to C
#define STDCALL_EXPORT_SUFFIX "$stdcall"
// The memory model a module is compiled for, a string literal: `if (__MEMORY_MODEL__ === "gc")`
// generates the branch taken only (MLIRGenImpl::foldLiteralCompare)
#define MEMORY_MODEL_IDENTIFIER "__MEMORY_MODEL__"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace typescript
void printVariableDeclaration(StringRef, NamespaceInfo::TypePtr, mlir::Type, bool, StringRef = StringRef());
void print(StringRef, NamespaceInfo::TypePtr, mlir_ts::FunctionType, StringRef = StringRef(),
ArrayRef<int32_t> ownKeeps = {}, ArrayRef<int32_t> ownBorrows = {}, ArrayRef<int32_t> ownLends = {},
bool ownRecord = false, ArrayRef<int32_t> ownImmortalizes = {});
bool ownRecord = false, ArrayRef<int32_t> ownImmortalizes = {}, bool stdcall = false);
void print(ClassInfo::TypePtr);
void print(InterfaceInfo::TypePtr);
void printGenericClass(NamespaceInfo::TypePtr, StringRef);
Expand Down
3 changes: 2 additions & 1 deletion tslang/include/TypeScript/Pass/CxxThrowCallingConvPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace ts
// _CxxThrowException is __stdcall on 32-bit x86 MSVC; its symbol is then __CxxThrowException@8.
// Sets that convention on the declaration and on every call and invoke of it. A call whose
// convention differs from its callee's is undefined behaviour, which InstCombine turns into
// `unreachable`, so this runs before the optimization pipeline.
// `unreachable`, so this runs before the optimization pipeline. A `@stdcall declare function`
// (the Windows API) is given the same convention, the same way.
class CxxThrowCallingConvPass : public llvm::PassInfoMixin<CxxThrowCallingConvPass>
{
public:
Expand Down
6 changes: 5 additions & 1 deletion tslang/include/TypeScript/VSCodeTemplate/Files.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,11 @@ if (TSLANG_MEMORY_MODEL STREQUAL "gc")
list(APPEND TSLANG_LINK_LIBS "gc")
endif()

if(NOT WIN32)
# printf, sprintf_s and the rest of stdio as functions compiled code can call: the UCRT has them only
# as inline wrappers in its headers, which code tslang compiles never sees.
if(WIN32)
list(APPEND TSLANG_LINK_LIBS "legacy_stdio_definitions")
else()
list(APPEND TSLANG_LINK_LIBS "stdc++" "m" "pthread" "dl" "rt")
endif()

Expand Down
9 changes: 8 additions & 1 deletion tslang/lib/TypeScript/DeclarationPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ namespace typescript

void MLIRDeclarationPrinter::print(StringRef name, NamespaceInfo::TypePtr elementNamespace, mlir_ts::FunctionType funcType, StringRef dllName,
ArrayRef<int32_t> ownKeeps, ArrayRef<int32_t> ownBorrows, ArrayRef<int32_t> ownLends,
bool ownRecord, ArrayRef<int32_t> ownImmortalizes)
bool ownRecord, ArrayRef<int32_t> ownImmortalizes, bool stdcall)
{
printNamespaceBegin(elementNamespace);

Expand All @@ -467,6 +467,13 @@ namespace typescript
newline();
}

// __stdcall on 32-bit Windows: the importer calls it so, and by the name it has there (_f@N)
if (stdcall)
{
os << "@" STDCALL_DECORATOR;
newline();
}

printOwnMarkers(ownKeeps, ownBorrows, ownLends, ownRecord, /*hasThis=*/false, 0, ownImmortalizes);

printFunction(
Expand Down
5 changes: 5 additions & 0 deletions tslang/lib/TypeScript/MLIRGenFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,11 @@ namespace mlirgen
globalOp->setAttr(name, attr);
}
}

if (declared->hasAttr(STDCALL_ATTR_NAME) && compileOptions.targetInfo.stdcallDecoratesCxxThrow)
{
stdcallDynamicImports[globalName] = dllFuncName.str();
}
}
}

Expand Down
62 changes: 58 additions & 4 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2273,6 +2273,17 @@ class MLIRGenImpl

varDecl->setUsing(varClass.isUsing);

// another module's variable, imported from its library: the global holds its address
// (mlirGen(VariableDeclaration)'s initFunc), the name is the variable it points to
if (varClass.isDynamicImport)
{
if (auto refType = dyn_cast<mlir_ts::RefType>(type))
{
varDecl->setType(refType.getElementType());
varDecl->setIndirect();
}
}

if (varClass.atomic)
{
varDecl->setAtomic(varClass.ordering, varClass.syncscope);
Expand Down Expand Up @@ -3811,11 +3822,17 @@ class MLIRGenImpl
addAttr(name, mlir::UnitAttr::get(builder.getContext()));
}

if (name == "varargs")
if (name == "varargs")
{
addAttr("func.varargs", mlir::BoolAttr::get(builder.getContext(), true));
}

// a function of the Windows API, or one it calls back: __stdcall on 32-bit Windows (STDCALL_DECORATOR)
if (name == STDCALL_DECORATOR)
{
addAttr(STDCALL_ATTR_NAME, mlir::UnitAttr::get(builder.getContext()));
}

// a body's facts come from the body: the decorator is a declaration's alone
if (name == NO_DROPS_DECORATOR)
{
Expand Down Expand Up @@ -10925,6 +10942,25 @@ class MLIRGenImpl
EXIT_IF_FAILED_OR_NO_VALUE(result)
auto value = V(result);

// A `@stdcall` function of another library given to C (a parameter of a C function type, @cdecl), on
// 32-bit Windows: C calls it as __stdcall, so it is the function itself, not the C function the
// library exports under its name for calls from TypeScript (stdcallDynamicImports)
if (!stdcallDynamicImports.empty() && isa_and_nonnull<mlir_ts::FunctionType>(operandsProcessingInfo.getReceiverType()))
{
auto loadOp = value.getDefiningOp<mlir_ts::LoadOp>();
auto addressOp = loadOp ? loadOp.getReference().getDefiningOp<mlir_ts::AddressOfOp>() : mlir_ts::AddressOfOp();
auto found = addressOp ? stdcallDynamicImports.find(addressOp.getGlobalName()) : stdcallDynamicImports.end();
if (found != stdcallDynamicImports.end())
{
auto location = loc(expression);
auto symbolName = V(mlirGenStringValue(location, found->second + STDCALL_EXPORT_SUFFIX, true));
auto address = builder.create<mlir_ts::SearchForAddressOfSymbolOp>(location, getOpaqueType(), symbolName);
auto itself = cast(location, value.getType(), address, argGenContext);
EXIT_IF_FAILED_OR_NO_VALUE(itself)
value = V(itself);
}
}

if (expression == SyntaxKind::SpreadElement)
{
auto location = loc(expression);
Expand Down Expand Up @@ -13255,8 +13291,20 @@ class MLIRGenImpl
return mlir::Value();
}

auto address = builder.create<mlir_ts::AddressOfOp>(location, mlir_ts::RefType::get(value->getType()),
value->getName(), ::mlir::IntegerAttr());
auto refType = mlir_ts::RefType::get(value->getType());
mlir::Value address;
if (value->getIndirect())
{
// the global holds the address of the variable: another module's, in its library
auto holder = builder.create<mlir_ts::AddressOfOp>(location, mlir_ts::RefType::get(refType),
value->getName(), ::mlir::IntegerAttr());
address = builder.create<mlir_ts::LoadOp>(location, refType, holder);
}
else
{
address = builder.create<mlir_ts::AddressOfOp>(location, refType, value->getName(), ::mlir::IntegerAttr());
}

if (asAddess)
{
return address;
Expand Down Expand Up @@ -16351,7 +16399,8 @@ class MLIRGenImpl
};
dp.print(funcProto->getNameWithoutNamespace(), elementNamespace, funcProto->getFuncType(), dllName,
marker(OWN_DECLARED_KEEPS_ATTR_NAME), marker(OWN_DECLARED_BORROWS_ATTR_NAME), marker(OWN_DECLARED_LENDS_ATTR_NAME),
funcOp && funcOp->hasAttr(OWN_DECLARED_RECORD_ATTR_NAME), marker(OWN_DECLARED_IMMORTALIZES_ATTR_NAME));
funcOp && funcOp->hasAttr(OWN_DECLARED_RECORD_ATTR_NAME), marker(OWN_DECLARED_IMMORTALIZES_ATTR_NAME),
funcOp && funcOp->hasAttr(STDCALL_ATTR_NAME));

declExports << ss.str().str();
}
Expand Down Expand Up @@ -17541,6 +17590,11 @@ class MLIRGenImpl

bool declarationMode;

// 32-bit Windows: the `@stdcall` functions of another library (-shared), by the global their address is
// kept in, and the name the library exports them under - a C function of that name, which calls them
// (STDCALL_EXPORT_SUFFIX). Given to C as a callback, one is looked up as itself instead (mlirGenOperand).
llvm::StringMap<std::string> stdcallDynamicImports;

std::stringstream declExports;
// generic class declarations must be re-imported as plain (non-".d.ts") source: unlike
// declExports (parsed with a "partial.d.ts" filename, which makes the parser mark
Expand Down
38 changes: 21 additions & 17 deletions tslang/lib/TypeScript/MLIRGenVariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,28 +878,32 @@ namespace mlirgen
auto referenceToStaticFieldOpaque = builder.create<mlir_ts::SearchForAddressOfSymbolOp>(
location, getOpaqueType(), dllVarName);

// The variable is the library's own: the global holds its address, and every
// read and write goes through it (createVariableDeclaration makes the name
// indirect, globalVariableAccess loads through it). A copy loaded here was a
// snapshot of its value when the program started, which never saw what the
// library assigned to it later.
auto storageType = fieldType;
if (varClass.isBoxed)
{
// the resolved symbol's storage is a single boxed
// pointer (ObjectType lowers to `ptr`) to the real
// data, not the data inline - load the pointer,
// instead of reading sizeof(fieldType) bytes directly
// at the symbol's own address (which would read past a
// lone 8-byte slot). The variable is that pointer, as
// the exporter's is: a copy of the data it points to
// was a snapshot, which the exporter's object never saw
// a write to, nor a method call, and a cast to an
// interface copied again. See DeclarationPrinter.cpp's
// the library's storage is a single boxed pointer (ObjectType lowers to
// `ptr`) to the real data, not the data inline. See DeclarationPrinter.cpp's
// @boxed emission.
auto boxedType = getObjectType(fieldType);
auto refToBoxed = cast(location, mlir_ts::RefType::get(boxedType), referenceToStaticFieldOpaque, genContext);
auto boxedPtr = V(builder.create<mlir_ts::LoadOp>(location, boxedType, refToBoxed));
return std::make_tuple(boxedPtr.getType(), boxedPtr, TypeProvided::Yes);
storageType = getObjectType(fieldType);
}
else if (auto funcType = dyn_cast<mlir_ts::FunctionType>(fieldType); funcType && !funcType.isCVarArg())
{
// an exported global holds a function as a hybrid one (adjustGlobalVariableType)
storageType = mlir_ts::HybridFunctionType::get(builder.getContext(), funcType);
}

auto refToStorage = cast(location, mlir_ts::RefType::get(storageType), referenceToStaticFieldOpaque, genContext);
if (refToStorage.failed_or_no_value())
{
return std::make_tuple(mlir::Type(), mlir::Value(), TypeProvided::No);
}

auto refToTyped = cast(location, mlir_ts::RefType::get(fieldType), referenceToStaticFieldOpaque, genContext);
auto valueOfField = builder.create<mlir_ts::LoadOp>(location, fieldType, refToTyped);
return std::make_tuple(valueOfField.getType(), V(valueOfField), TypeProvided::Yes);
return std::make_tuple(V(refToStorage).getType(), V(refToStorage), TypeProvided::Yes);
}
}

Expand Down
Loading
Loading