-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathCallConstructors.cpp
More file actions
158 lines (140 loc) · 5.11 KB
/
CallConstructors.cpp
File metadata and controls
158 lines (140 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//===-- CallConstructors.cpp - Cheerp backend pass ------------------------===//
//
// Cheerp: The C++ compiler for the Web
//
// This file is distributed under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE.TXT for details.
//
// Copyright 2022-2023 Leaning Technologies
//
//===----------------------------------------------------------------------===//
#include "llvm/Cheerp/CallConstructors.h"
#include "llvm/Cheerp/Utility.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/ADT/Triple.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#define DEBUG_TYPE "callconstructors"
using namespace llvm;
namespace cheerp
{
PreservedAnalyses CallConstructorsPass::run(llvm::Module &M, llvm::ModuleAnalysisManager &MPA)
{
bool Wasi = Triple(M.getTargetTriple()).getOS() == Triple::WASI;
bool useUtilityThread = !LowerAtomics && !Wasi;
FunctionType* Ty = FunctionType::get(Type::getVoidTy(M.getContext()), false);
Function* StartFunction = cast<Function>(M.getOrInsertFunction("_start", Ty).getCallee());
if (!StartFunction->empty())
return PreservedAnalyses::all();
BasicBlock* StartEntry = BasicBlock::Create(M.getContext(),"entry", StartFunction);
IRBuilder<> Builder(StartEntry);
Function* StartPreThread = nullptr;
if (useUtilityThread)
{
StartPreThread = cast<Function>(M.getOrInsertFunction("_startPreThread", Ty).getCallee());
BasicBlock* StartPreThreadEntry = BasicBlock::Create(M.getContext(),"entry", StartPreThread);
Builder.SetInsertPoint(StartPreThreadEntry);
}
if (LinearOutput == LinearOutputTy::Wasm)
{
// Here we declare the memory_init function, which will be called by _start.
Function* memoryInit = cast<Function>(M.getOrInsertFunction("__memory_init", Ty).getCallee());
memoryInit->setSection("asmjs");
Builder.CreateCall(Ty, memoryInit);
}
// Add a call to initialise the tls to _start.
Function* cheerpInitTls = cast<Function>(M.getOrInsertFunction("__cheerp_init_tls", Ty).getCallee());
Builder.CreateCall(Ty, cheerpInitTls);
Function* GetEnviron = M.getFunction("__syscall_main_environ");
if (GetEnviron)
Builder.CreateCall(GetEnviron->getFunctionType(), GetEnviron);
for (Constant* C: cheerp::getGlobalConstructors(M))
Builder.CreateCall(Ty, cast<Function>(C->getAggregateElement(1)->stripPointerCastsSafe()));
removeGlobalConstructorsGlobal(M);
if (useUtilityThread)
{
// If -pthread is passed, add a call to spawnUtility to setup the utility thread.
Function* spawnUtility = cast<Function>(M.getOrInsertFunction("spawnUtility", Ty).getCallee());
Builder.CreateCall(Ty, spawnUtility);
Builder.CreateRetVoid();
Builder.SetInsertPoint(StartEntry);
}
Function* Main = getMainFunction(M);
if (Wasi || (Main && Main->getSection() == "asmjs"))
{
StartFunction->setSection("asmjs");
if (useUtilityThread)
StartPreThread->setSection("asmjs");
}
if (Main)
{
Value* ExitCode = nullptr;
if (Main->arg_size())
{
if (Main->arg_size() != 2 && Main->arg_size() != 3)
llvm::report_fatal_error("main function has a strange signature");
Type* ArgcTy = Main->getArg(0)->getType();
Type* ArgvTy = Main->getArg(1)->getType();
Value* Argc = nullptr;
Value* Argv = nullptr;
Function* GetArgs = M.getFunction("__syscall_main_args");
if (GetArgs && GetArgs->getSection() == Main->getSection())
{
Value* ArgcA = Builder.CreateAlloca(ArgcTy);
Value* ArgvA = Builder.CreateAlloca(ArgvTy);
ArrayRef<Value*> ArgsA = { ArgcA, ArgvA };
Builder.CreateCall(GetArgs->getFunctionType(), GetArgs, { ArgcA, ArgvA});
Argc = Builder.CreateLoad(ArgcTy, ArgcA);
Argv = Builder.CreateLoad(ArgvTy, ArgvA);
}
else
{
Argc = ConstantInt::get(ArgcTy, 0);
Argv = ConstantPointerNull::get(cast<PointerType>(ArgvTy));
}
if (Main->arg_size() == 3)
{
Type* EnvTy = Main->getArg(2)->getType();
Value* Env = nullptr;
if (GetEnviron->getSection() == Main->getSection())
{
Value* Environ = M.getNamedValue("environ");
assert(Environ && "environ not present");
Env = Builder.CreateLoad(EnvTy, Environ);
}
else
{
Env = Builder.CreateAlloca(Builder.getInt8Ty()->getPointerTo(0));
Builder.CreateStore(ConstantPointerNull::get(Builder.getInt8Ty()->getPointerTo(0)), Env);
}
ExitCode = Builder.CreateCall(Main->getFunctionType(), Main, { Argc, Argv, Env });
}
else
{
ExitCode = Builder.CreateCall(Main->getFunctionType(), Main, { Argc, Argv });
}
}
else
{
ExitCode = Builder.CreateCall(Main->getFunctionType(), Main);
}
if (!LowerAtomics || Wasi)
{
// In WASI mode, or if -pthread has been passed, we call exit after main, which will run global destructors
Function* Exit = M.getFunction("exit");
assert(Exit != nullptr);
if (ExitCode->getType() != Builder.getInt32Ty())
ExitCode = ConstantInt::get(Builder.getInt32Ty(), 0);
Builder.CreateCall(Exit->getFunctionType(), Exit, ExitCode);
}
}
Builder.CreateRetVoid();
PreservedAnalyses PA = PreservedAnalyses::none();
PA.preserveSet(CFGAnalyses::ID());
return PA;
}
}