llvm.org GIT mirror llvm / 640fb6d
[WebAssembly] Fix types for address taken functions Differential Revision: https://reviews.llvm.org/D34966 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@307198 91177308-0d34-0410-b5e6-96231b3b80d8 Sam Clegg 2 years ago
7 changed file(s) with 70 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
2020 std::string ModuleName;
2121 SmallVector Returns;
2222 SmallVector Params;
23 bool ParamsSet = false;
24 bool ReturnsSet = false;
2325
2426 /// An expression describing how to calculate the size of a symbol. If a
2527 /// symbol has no size this field will be NULL.
4446
4547 const StringRef getModuleName() const { return ModuleName; }
4648
47 const SmallVector &getReturns() const { return Returns; }
49 const SmallVector &getReturns() const {
50 assert(ReturnsSet);
51 return Returns;
52 }
4853
4954 void setReturns(SmallVectorImpl &&Rets) {
55 ReturnsSet = true;
5056 Returns = std::move(Rets);
5157 }
5258
53 const SmallVector &getParams() const { return Params; }
59 const SmallVector &getParams() const {
60 assert(ParamsSet);
61 return Params;
62 }
5463
5564 void setParams(SmallVectorImpl &&Pars) {
65 ParamsSet = true;
5666 Params = std::move(Pars);
5767 }
5868 };
152152 }
153153
154154 void print(raw_ostream &Out) const {
155 Out << "Off=" << Offset << ", Sym=" << Symbol << ", Addend=" << Addend
155 Out << "Off=" << Offset << ", Sym=" << *Symbol << ", Addend=" << Addend
156156 << ", Type=" << Type << ", FixupSection=" << FixupSection;
157157 }
158158
114114 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
115115
116116 void WebAssemblyTargetAsmStreamer::emitIndirectFunctionType(
117 StringRef name, SmallVectorImpl &Params, SmallVectorImpl &Results) {
118 OS << "\t.functype\t" << name;
117 MCSymbol *Symbol, SmallVectorImpl &Params, SmallVectorImpl &Results) {
118 OS << "\t.functype\t" << Symbol->getName();
119119 if (Results.empty())
120120 OS << ", void";
121121 else {
170170 }
171171
172172 void WebAssemblyTargetELFStreamer::emitIndirectFunctionType(
173 StringRef name, SmallVectorImpl &Params, SmallVectorImpl &Results) {
173 MCSymbol *Symbol, SmallVectorImpl &Params, SmallVectorImpl &Results) {
174174 // Nothing to emit here. TODO: Re-design how linking works and re-evaluate
175175 // whether it's necessary for .o files to declare indirect function types.
176176 }
254254 }
255255
256256 void WebAssemblyTargetWasmStreamer::emitIndirectFunctionType(
257 StringRef name, SmallVectorImpl &Params, SmallVectorImpl &Results) {
258 // Nothing to emit here. TODO: Re-design how linking works and re-evaluate
259 // whether it's necessary for .o files to declare indirect function types.
257 MCSymbol *Symbol, SmallVectorImpl &Params,
258 SmallVectorImpl &Results) {
259 MCSymbolWasm *WasmSym = cast(Symbol);
260 if (WasmSym->isFunction()) {
261 // Symbol already has its arguments and result set.
262 return;
263 }
264
265 SmallVector ValParams;
266 for (MVT Ty : Params)
267 ValParams.push_back(WebAssembly::toValType(Ty));
268
269 SmallVector ValResults;
270 for (MVT Ty : Results)
271 ValResults.push_back(WebAssembly::toValType(Ty));
272
273 WasmSym->setParams(std::move(ValParams));
274 WasmSym->setReturns(std::move(ValResults));
275 WasmSym->setIsFunction(true);
260276 }
261277
262278 void WebAssemblyTargetWasmStreamer::emitGlobalImport(StringRef name) {
4343 /// .endfunc
4444 virtual void emitEndFunc() = 0;
4545 /// .functype
46 virtual void emitIndirectFunctionType(StringRef name,
46 virtual void emitIndirectFunctionType(MCSymbol *Symbol,
4747 SmallVectorImpl &Params,
4848 SmallVectorImpl &Results) = 0;
4949 /// .indidx
6868 void emitGlobal(ArrayRef Globals) override;
6969 void emitStackPointer(uint32_t Index) override;
7070 void emitEndFunc() override;
71 void emitIndirectFunctionType(StringRef name,
71 void emitIndirectFunctionType(MCSymbol *Symbol,
7272 SmallVectorImpl &Params,
7373 SmallVectorImpl &Results) override;
7474 void emitIndIdx(const MCExpr *Value) override;
8686 void emitGlobal(ArrayRef Globals) override;
8787 void emitStackPointer(uint32_t Index) override;
8888 void emitEndFunc() override;
89 void emitIndirectFunctionType(StringRef name,
89 void emitIndirectFunctionType(MCSymbol *Symbol,
9090 SmallVectorImpl &Params,
9191 SmallVectorImpl &Results) override;
9292 void emitIndIdx(const MCExpr *Value) override;
104104 void emitGlobal(ArrayRef Globals) override;
105105 void emitStackPointer(uint32_t Index) override;
106106 void emitEndFunc() override;
107 void emitIndirectFunctionType(StringRef name,
107 void emitIndirectFunctionType(MCSymbol *Symbol,
108108 SmallVectorImpl &Params,
109109 SmallVectorImpl &Results) override;
110110 void emitIndIdx(const MCExpr *Value) override;
8383 SmallVector Results;
8484 SmallVector Params;
8585 ComputeSignatureVTs(F, TM, Params, Results);
86 getTargetStreamer()->emitIndirectFunctionType(F.getName(), Params,
86 getTargetStreamer()->emitIndirectFunctionType(getSymbol(&F), Params,
8787 Results);
8888 }
8989 }
213213 const MCExpr *WebAssemblyAsmPrinter::lowerConstant(const Constant *CV) {
214214 if (const GlobalValue *GV = dyn_cast(CV))
215215 if (GV->getValueType()->isFunctionTy()) {
216 MCSymbol* Sym = getSymbol(GV);
217 if (!isa(Sym))
218 cast(Sym)->setIsFunction(true);
219216 return MCSymbolRefExpr::create(
220 Sym, MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
217 getSymbol(GV), MCSymbolRefExpr::VK_WebAssembly_FUNCTION, OutContext);
221218 }
222219 return AsmPrinter::lowerConstant(CV);
223220 }
111111 MCSymbolRefExpr::VariantKind VK =
112112 IsFunc ? MCSymbolRefExpr::VK_WebAssembly_FUNCTION
113113 : MCSymbolRefExpr::VK_None;
114 if (!isa(Sym))
115 cast(Sym)->setIsFunction(IsFunc);
116114
117115 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx);
118116
11 ; Verify that addresses of external functions generate correctly typed
22 ; imports and relocations or type R_TABLE_INDEX_I32.
33
4 declare void @f1() #1
5 @ptr_to_f1 = hidden global void ()* @f1, align 4
4 declare void @f1(i32) #1
5 @ptr_to_f1 = hidden global void (i32)* @f1, align 4
66
7
8 ; CHECK: - Type: IMPORT
9 ; CHECK: Imports:
10 ; CHECK: - Module: env
11 ; CHECK: Field: f1
12 ; CHECK: Kind: FUNCTION
13 ; CHECK: SigIndex: 0
14 ; CHECK: - Type: ELEM
15 ; CHECK: Segments:
16 ; CHECK: - Offset:
17 ; CHECK: Opcode: I32_CONST
18 ; CHECK: Value: 0
19 ; CHECK: Functions: [ 0 ]
20 ; CHECK: - Type: DATA
21 ; CHECK: Relocations:
22 ; CHECK: - Type: R_WEBASSEMBLY_TABLE_INDEX_I32
23 ; CHECK: Index: 0
24 ; CHECK: Offset: 0x00000006
7 ; CHECK: --- !WASM
8 ; CHECK-NEXT: FileHeader:
9 ; CHECK-NEXT: Version: 0x00000001
10 ; CHECK-NEXT: Sections:
11 ; CHECK-NEXT: - Type: TYPE
12 ; CHECK-NEXT: Signatures:
13 ; CHECK-NEXT: - Index: 0
14 ; CHECK-NEXT: ReturnType: NORESULT
15 ; CHECK-NEXT: ParamTypes:
16 ; CHECK-NEXT: - I32
17 ; CHECK: - Type: IMPORT
18 ; CHECK-NEXT: Imports:
19 ; CHECK-NEXT: - Module: env
20 ; CHECK-NEXT: Field: f1
21 ; CHECK-NEXT: Kind: FUNCTION
22 ; CHECK-NEXT: SigIndex: 0
23 ; CHECK: - Type: ELEM
24 ; CHECK-NEXT: Segments:
25 ; CHECK-NEXT: - Offset:
26 ; CHECK-NEXT: Opcode: I32_CONST
27 ; CHECK-NEXT: Value: 0
28 ; CHECK-NEXT: Functions: [ 0 ]
29 ; CHECK: - Type: DATA
30 ; CHECK-NEXT: Relocations:
31 ; CHECK-NEXT: - Type: R_WEBASSEMBLY_TABLE_INDEX_I32
32 ; CHECK-NEXT: Index: 0
33 ; CHECK-NEXT: Offset: 0x00000006