@@ -1619,7 +1619,10 @@ void CodeGenModule::emitYulObject(const YulObject *YO) {
16191619 }
16201620 assert (nullptr != YO->getCode ());
16211621 if (YO->getName () == " .metadata" ) {
1622- // TODO: attach '.metadata' to wasm bytecode.
1622+ // ignore sub-object and code
1623+ for (const auto *D : YO->getDataList ()) {
1624+ emitYulDataInMetadata (D);
1625+ }
16231626 return ;
16241627 }
16251628 for (const auto *O : YO->getObjectList ()) {
@@ -1659,11 +1662,31 @@ void CodeGenModule::emitYulCode(const YulCode *YC, llvm::StringRef Name) {
16591662void CodeGenModule::emitYulData (const YulData *YD) {
16601663 llvm::StringRef Name = YD->getUniqueName ();
16611664 std::string Data = YD->getBody ()->getValue ();
1665+ if (YD->getName () == " .metadata" ) {
1666+ llvm::SmallVector<llvm::Metadata *, 8 > Ops;
1667+ llvm::StringRef Name = YD->getName ();
1668+ std::string Data = YD->getBody ()->getValue ();
1669+ Ops.push_back (llvm::MDString::get (VMContext, Name));
1670+ Ops.push_back (llvm::MDString::get (VMContext, Data));
1671+ auto MD = getModule ().getOrInsertNamedMetadata (" wasm.custom_sections" );
1672+ MD->addOperand (llvm::MDTuple::get (VMContext, Ops));
1673+ return ;
1674+ }
16621675 llvm::GlobalVariable *Variable =
16631676 createGlobalString (VMContext, getModule (), Data, Name);
16641677 YulDataMap.try_emplace (YD, Variable);
16651678}
16661679
1680+ void CodeGenModule::emitYulDataInMetadata (const YulData *YD) {
1681+ llvm::SmallVector<llvm::Metadata *, 8 > Ops;
1682+ std::string Name = " .metadata." + YD->getName ().str ();
1683+ std::string Data = YD->getBody ()->getValue ();
1684+ Ops.push_back (llvm::MDString::get (VMContext, Name));
1685+ Ops.push_back (llvm::MDString::get (VMContext, Data));
1686+ auto MD = getModule ().getOrInsertNamedMetadata (" wasm.custom_sections" );
1687+ MD->addOperand (llvm::MDTuple::get (VMContext, Ops));
1688+ }
1689+
16671690std::string CodeGenModule::getMangledName (const CallableVarDecl *CVD) {
16681691 // XXX: Implement mangling
16691692 std::string Name = CVD->getUniqueName ().str () + ' (' ;
0 commit comments