Skip to content

Commit 0c0027e

Browse files
kleisauketurran
authored andcommitted
Prefer use of Wasm signatures when building the thunk name
1 parent cf1cde8 commit 0c0027e

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

clang/lib/CodeGen/Targets/WebAssembly.cpp

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "TargetInfo.h"
1111

1212
#include "clang/AST/ParentMapContext.h"
13-
#include <sstream>
1413

1514
using namespace clang;
1615
using namespace clang::CodeGen;
@@ -192,10 +191,11 @@ class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
192191
}
193192

194193
private:
195-
// Build the thunk name: "%s_{type1}_{type2}_..."
194+
// Build the thunk name: "%s_{OrigName}_{WasmSig}"
196195
std::string getThunkName(std::string OrigName,
197196
const FunctionProtoType *DstProto,
198197
const ASTContext &Ctx) const;
198+
char getTypeSig(const QualType &Ty, const ASTContext &Ctx) const;
199199
std::string sanitizeTypeString(const std::string &typeStr) const;
200200
std::string getTypeName(const QualType &qt, const ASTContext &Ctx) const;
201201
const DeclRefExpr *findDeclRefExpr(const Expr *E) const;
@@ -285,43 +285,49 @@ CodeGen::createWebAssemblyTargetCodeGenInfo(CodeGenModule &CGM,
285285
return std::make_unique<WebAssemblyTargetCodeGenInfo>(CGM.getTypes(), K);
286286
}
287287

288-
// Helper to sanitize type name string for use in function name
289-
std::string WebAssemblyTargetCodeGenInfo::sanitizeTypeString(
290-
const std::string &typeStr) const {
291-
std::string s;
292-
for (char c : typeStr) {
293-
if (isalnum(c))
294-
s += c;
295-
else if (c == ' ')
296-
s += '_';
297-
else
298-
s += '_';
288+
// Helper to get the type signature character for a given QualType
289+
// Returns a character that represents the given QualType in a wasm signature.
290+
// See getInvokeSig() in WebAssemblyAsmPrinter for related logic.
291+
char WebAssemblyTargetCodeGenInfo::getTypeSig(const QualType &Ty,
292+
const ASTContext &Ctx) const {
293+
if (Ty->isAnyPointerType()) {
294+
return Ctx.getTypeSize(Ctx.VoidPtrTy) == 32 ? 'i' : 'j';
295+
}
296+
if (Ty->isIntegerType()) {
297+
return Ctx.getTypeSize(Ty) <= 32 ? 'i' : 'j';
298+
}
299+
if (Ty->isFloatingType()) {
300+
return Ctx.getTypeSize(Ty) <= 32 ? 'f' : 'd';
301+
}
302+
if (Ty->isVectorType()) {
303+
return 'V';
304+
}
305+
if (Ty->isWebAssemblyTableType()) {
306+
return 'F';
307+
}
308+
if (Ty->isWebAssemblyExternrefType()) {
309+
return 'X';
299310
}
300-
return s;
301-
}
302311

303-
// Helper to generate the type string from QualType
304-
std::string
305-
WebAssemblyTargetCodeGenInfo::getTypeName(const QualType &qt,
306-
const ASTContext &Ctx) const {
307-
PrintingPolicy Policy(Ctx.getLangOpts());
308-
Policy.SuppressTagKeyword = true;
309-
Policy.SuppressScope = true;
310-
Policy.AnonymousTagLocations = false;
311-
std::string typeStr = qt.getAsString(Policy);
312-
return sanitizeTypeString(typeStr);
312+
llvm_unreachable("Unhandled QualType");
313313
}
314314

315315
std::string
316316
WebAssemblyTargetCodeGenInfo::getThunkName(std::string OrigName,
317317
const FunctionProtoType *DstProto,
318318
const ASTContext &Ctx) const {
319-
std::ostringstream oss;
320-
oss << "__" << OrigName;
319+
320+
std::string ThunkName = "__" + OrigName + "_";
321+
QualType RetTy = DstProto->getReturnType();
322+
if (RetTy->isVoidType()) {
323+
ThunkName += 'v';
324+
} else {
325+
ThunkName += getTypeSig(RetTy, Ctx);
326+
}
321327
for (unsigned i = 0; i < DstProto->getNumParams(); ++i) {
322-
oss << "_" << getTypeName(DstProto->getParamType(i), Ctx);
328+
ThunkName += getTypeSig(DstProto->getParamType(i), Ctx);
323329
}
324-
return oss.str();
330+
return ThunkName;
325331
}
326332

327333
/// Recursively find the first DeclRefExpr in an Expr subtree.
@@ -394,4 +400,4 @@ const DeclRefExpr *WebAssemblyTargetCodeGenInfo::findDeclRefExprForVarUp(
394400
cur = parentStmt;
395401
}
396402
return nullptr;
397-
}
403+
}

0 commit comments

Comments
 (0)