Skip to content

Commit

Permalink
callconstructors: add support for main with environ argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyxogen committed Jan 10, 2024
1 parent 0a00d55 commit 0caba68
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions llvm/lib/CheerpUtils/CallConstructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ PreservedAnalyses CallConstructorsPass::run(llvm::Module &M, llvm::ModuleAnalysi
Value* ExitCode = nullptr;
if (Main->arg_size())
{
if (Main->arg_size() != 2)
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();
Expand All @@ -82,7 +82,20 @@ PreservedAnalyses CallConstructorsPass::run(llvm::Module &M, llvm::ModuleAnalysi
Argc = ConstantInt::get(ArgcTy, 0);
Argv = ConstantPointerNull::get(cast<PointerType>(ArgvTy));
}
ExitCode = Builder.CreateCall(Main->getFunctionType(), Main, { Argc, Argv });

if (Main->arg_size() == 3)
{
Value* Environ = M.getNamedValue("environ");
assert(Environ && "environ not present");

Value* Env = Builder.CreateLoad(Main->getArg(2)->getType(), Environ);

ExitCode = Builder.CreateCall(Main->getFunctionType(), Main, { Argc, Argv, Env });
}
else
{
ExitCode = Builder.CreateCall(Main->getFunctionType(), Main, { Argc, Argv });
}
}
else
{
Expand Down

0 comments on commit 0caba68

Please sign in to comment.