10
10
#include " TargetInfo.h"
11
11
12
12
#include " clang/AST/ParentMapContext.h"
13
- #include < sstream>
14
13
15
14
using namespace clang ;
16
15
using namespace clang ::CodeGen;
@@ -192,10 +191,11 @@ class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
192
191
}
193
192
194
193
private:
195
- // Build the thunk name: "%s_{type1 }_{type2}_... "
194
+ // Build the thunk name: "%s_{OrigName }_{WasmSig} "
196
195
std::string getThunkName (std::string OrigName,
197
196
const FunctionProtoType *DstProto,
198
197
const ASTContext &Ctx) const ;
198
+ char getTypeSig (const QualType &Ty, const ASTContext &Ctx) const ;
199
199
std::string sanitizeTypeString (const std::string &typeStr) const ;
200
200
std::string getTypeName (const QualType &qt, const ASTContext &Ctx) const ;
201
201
const DeclRefExpr *findDeclRefExpr (const Expr *E) const ;
@@ -285,43 +285,49 @@ CodeGen::createWebAssemblyTargetCodeGenInfo(CodeGenModule &CGM,
285
285
return std::make_unique<WebAssemblyTargetCodeGenInfo>(CGM.getTypes (), K);
286
286
}
287
287
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' ;
299
310
}
300
- return s;
301
- }
302
311
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" );
313
313
}
314
314
315
315
std::string
316
316
WebAssemblyTargetCodeGenInfo::getThunkName (std::string OrigName,
317
317
const FunctionProtoType *DstProto,
318
318
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
+ }
321
327
for (unsigned i = 0 ; i < DstProto->getNumParams (); ++i) {
322
- oss << " _ " << getTypeName (DstProto->getParamType (i), Ctx);
328
+ ThunkName += getTypeSig (DstProto->getParamType (i), Ctx);
323
329
}
324
- return oss. str () ;
330
+ return ThunkName ;
325
331
}
326
332
327
333
// / Recursively find the first DeclRefExpr in an Expr subtree.
@@ -394,4 +400,4 @@ const DeclRefExpr *WebAssemblyTargetCodeGenInfo::findDeclRefExprForVarUp(
394
400
cur = parentStmt;
395
401
}
396
402
return nullptr ;
397
- }
403
+ }
0 commit comments