llvm.org GIT mirror llvm / 959e37e
[WebAssemby] Support main functions with alternate signatures. WebAssembly requires caller and callee signatures to match, so the usual C runtime trick of calling main and having it just work regardless of whether main is defined as '()' or '(int argc, char *argv[])' doesn't work. Extend the FixFunctionBitcasts pass to rewrite main to use the latter form. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@320041 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 2 years ago
2 changed file(s) with 63 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
141141 }
142142
143143 bool FixFunctionBitcasts::runOnModule(Module &M) {
144 Function *Main = nullptr;
145 CallInst *CallMain = nullptr;
144146 SmallVector, 0> Uses;
145147 SmallPtrSet ConstantBCs;
146148
147149 // Collect all the places that need wrappers.
148 for (Function &F : M) FindUses(&F, F, Uses, ConstantBCs);
150 for (Function &F : M) {
151 FindUses(&F, F, Uses, ConstantBCs);
152
153 // If we have a "main" function, and its type isn't
154 // "int main(int argc, char *argv[])", create an artificial call with it
155 // bitcasted to that type so that we generate a wrapper for it, so that
156 // the C runtime can call it.
157 if (F.getName() == "main") {
158 Main = &F;
159 LLVMContext &C = M.getContext();
160 Type *MainArgTys[] = {
161 PointerType::get(Type::getInt8PtrTy(C), 0),
162 Type::getInt32Ty(C)
163 };
164 FunctionType *MainTy = FunctionType::get(Type::getInt32Ty(C), MainArgTys,
165 /*isVarArg=*/false);
166 if (F.getFunctionType() != MainTy) {
167 Value *Args[] = {
168 UndefValue::get(MainArgTys[0]),
169 UndefValue::get(MainArgTys[1])
170 };
171 Value *Casted = ConstantExpr::getBitCast(Main,
172 PointerType::get(MainTy, 0));
173 CallMain = CallInst::Create(Casted, Args, "call_main");
174 Use *UseMain = &CallMain->getOperandUse(2);
175 Uses.push_back(std::make_pair(UseMain, &F));
176 }
177 }
178 }
149179
150180 DenseMap, Function *> Wrappers;
151181
180210 U->set(Wrapper);
181211 }
182212
213 // If we created a wrapper for main, rename the wrapper so that it's the
214 // one that gets called from startup.
215 if (CallMain) {
216 Main->setName("__original_main");
217 Function *MainWrapper =
218 cast(CallMain->getCalledValue()->stripPointerCasts());
219 MainWrapper->setName("main");
220 MainWrapper->setLinkage(Main->getLinkage());
221 MainWrapper->setVisibility(Main->getVisibility());
222 Main->setLinkage(Function::PrivateLinkage);
223 Main->setVisibility(Function::DefaultVisibility);
224 delete CallMain;
225 }
226
183227 return true;
184228 }
0 ; RUN: llc < %s -asm-verbose=false | FileCheck %s
1
2 ; Test main functions with alternate signatures.
3
4 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
5 target triple = "wasm32-unknown-unknown-wasm"
6
7 define void @main() {
8 ret void
9 }
10
11 ; CHECK-LABEL: .L__original_main:
12 ; CHECK-NEXT: end_function
13
14 ; CHECK-LABEL: main:
15 ; CHECK-NEXT: .param i32, i32
16 ; CHECK-NEXT: .result i32
17 ; CHECK: call .L__original_main@FUNCTION