Skip to content

Commit b4f4e37

Browse files
committed
[WebAssebmly][MC] Support .import_name/.import_field asm directives
Convert the MC test to use asm rather than bitcode. This is a precursor to https://reviews.llvm.org/D70520. Differential Revision: https://reviews.llvm.org/D70877
1 parent 1d9291c commit b4f4e37

File tree

7 files changed

+63
-36
lines changed

7 files changed

+63
-36
lines changed

clang/include/clang/Basic/AttrDocs.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4104,7 +4104,7 @@ or `msvc documentation <https://docs.microsoft.com/pl-pl/cpp/cpp/selectany>`_.
41044104
def WebAssemblyImportModuleDocs : Documentation {
41054105
let Category = DocCatFunction;
41064106
let Content = [{
4107-
Clang supports the ``__attribute__((import_module(<module_name>)))``
4107+
Clang supports the ``__attribute__((import_module(<module_name>)))``
41084108
attribute for the WebAssembly target. This attribute may be attached to a
41094109
function declaration, where it modifies how the symbol is to be imported
41104110
within the WebAssembly linking environment.
@@ -4114,14 +4114,14 @@ name, which typically identifies a module from which to import, and a field
41144114
name, which typically identifies a field from that module to import. By
41154115
default, module names for C/C++ symbols are assigned automatically by the
41164116
linker. This attribute can be used to override the default behavior, and
4117-
reuqest a specific module name be used instead.
4117+
request a specific module name be used instead.
41184118
}];
41194119
}
41204120

41214121
def WebAssemblyImportNameDocs : Documentation {
41224122
let Category = DocCatFunction;
41234123
let Content = [{
4124-
Clang supports the ``__attribute__((import_name(<name>)))``
4124+
Clang supports the ``__attribute__((import_name(<name>)))``
41254125
attribute for the WebAssembly target. This attribute may be attached to a
41264126
function declaration, where it modifies how the symbol is to be imported
41274127
within the WebAssembly linking environment.
@@ -4131,7 +4131,7 @@ name, which typically identifies a module from which to import, and a field
41314131
name, which typically identifies a field from that module to import. By
41324132
default, field names for C/C++ symbols are the same as their C/C++ symbol
41334133
names. This attribute can be used to override the default behavior, and
4134-
reuqest a specific field name be used instead.
4134+
request a specific field name be used instead.
41354135
}];
41364136
}
41374137

File renamed without changes.

llvm/include/llvm/MC/MCSymbolWasm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class MCSymbolWasm : public MCSymbol {
7878
}
7979
void setImportModule(StringRef Name) { ImportModule = Name; }
8080

81+
bool hasImportName() const { return ImportName.hasValue(); }
8182
const StringRef getImportName() const {
8283
if (ImportName.hasValue()) {
8384
return ImportName.getValue();

llvm/lib/MC/WasmObjectWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,7 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
14521452
Flags |= wasm::WASM_SYMBOL_EXPORTED;
14531453
}
14541454
}
1455-
if (WS.getName() != WS.getImportName())
1455+
if (WS.hasImportName())
14561456
Flags |= wasm::WASM_SYMBOL_EXPLICIT_NAME;
14571457

14581458
wasm::WasmSymbolInfo Info;

llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,30 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
712712
return expect(AsmToken::EndOfStatement, "EOL");
713713
}
714714

715+
if (DirectiveID.getString() == ".import_module") {
716+
auto SymName = expectIdent();
717+
if (SymName.empty())
718+
return true;
719+
if (expect(AsmToken::Comma, ","))
720+
return true;
721+
auto ImportModule = expectIdent();
722+
auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName));
723+
WasmSym->setImportModule(ImportModule);
724+
TOut.emitImportModule(WasmSym, ImportModule);
725+
}
726+
727+
if (DirectiveID.getString() == ".import_name") {
728+
auto SymName = expectIdent();
729+
if (SymName.empty())
730+
return true;
731+
if (expect(AsmToken::Comma, ","))
732+
return true;
733+
auto ImportName = expectIdent();
734+
auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName));
735+
WasmSym->setImportName(ImportName);
736+
TOut.emitImportName(WasmSym, ImportName);
737+
}
738+
715739
if (DirectiveID.getString() == ".eventtype") {
716740
auto SymName = expectIdent();
717741
if (SymName.empty())

llvm/test/MC/WebAssembly/import-module.ll

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# RUN: llvm-mc -triple=wasm32 < %s | FileCheck %s -check-prefix=CHECK-ASM
2+
# RUN: llvm-mc -triple=wasm32 -filetype=obj -o - < %s | obj2yaml | FileCheck %s
3+
4+
test:
5+
.functype test () -> ()
6+
call foo
7+
call plain
8+
end_function
9+
10+
.functype foo () -> ()
11+
.functype plain () -> ()
12+
.import_module foo, bar
13+
.import_name foo, qux
14+
15+
# CHECK-ASM: .import_module foo, bar
16+
# CHECK-ASM: .import_name foo, qux
17+
18+
# CHECK: - Type: IMPORT
19+
# CHECK-NEXT: Imports:
20+
# CHECK: - Module: bar
21+
# CHECK-NEXT: Field: qux
22+
# CHECK-NEXT: Kind: FUNCTION
23+
24+
# CHECK: - Module: env
25+
# CHECK-NEXT: Field: plain
26+
# CHECK-NEXT: Kind: FUNCTION
27+
28+
# CHECK: - Type: CUSTOM
29+
# CHECK: Name: foo
30+
# CHECK-NEXT: Flags: [ UNDEFINED, EXPLICIT_NAME ]
31+
32+
# CHECK: Name: plain
33+
# CHECK-NEXT: Flags: [ UNDEFINED ]

0 commit comments

Comments
 (0)