@@ -1928,171 +1928,6 @@ void SILGenModule::visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
1928
1928
// Nothing to do for #error/#warning; they've already been emitted.
1929
1929
}
1930
1930
1931
- void SILGenModule::emitEntryPoint (SourceFile *SF, SILFunction *TopLevel) {
1932
-
1933
- auto EntryRef = SILDeclRef::getMainFileEntryPoint (SF);
1934
- bool isAsyncTopLevel = false ;
1935
- if (SF->isAsyncContext ()) {
1936
- isAsyncTopLevel = true ;
1937
- auto asyncEntryRef = SILDeclRef::getAsyncMainFileEntryPoint (SF);
1938
- auto *asyncTopLevel = getFunction (asyncEntryRef, ForDefinition);
1939
- SILGenFunction (*this , *TopLevel, SF)
1940
- .emitAsyncMainThreadStart (asyncEntryRef);
1941
- TopLevel = asyncTopLevel;
1942
- EntryRef = asyncEntryRef;
1943
- }
1944
-
1945
- TopLevel->createProfiler (EntryRef);
1946
-
1947
- SILGenFunction TopLevelSGF (*this , *TopLevel, SF, true );
1948
- TopLevelSGF.MagicFunctionName = SwiftModule->getName ();
1949
- auto moduleCleanupLoc = CleanupLocation::getModuleCleanupLocation ();
1950
-
1951
- TopLevelSGF.prepareEpilog (llvm::None, true , moduleCleanupLoc);
1952
-
1953
- auto prologueLoc = RegularLocation::getModuleLocation ();
1954
- prologueLoc.markAsPrologue ();
1955
- if (SF->isAsyncContext ()) {
1956
- // emitAsyncMainThreadStart will create argc and argv.
1957
- // Just set the main actor as the expected executor; we should
1958
- // already be running on it.
1959
- SILValue executor = TopLevelSGF.emitMainExecutor (prologueLoc);
1960
- TopLevelSGF.ExpectedExecutor = TopLevelSGF.B .createOptionalSome (
1961
- prologueLoc, executor, SILType::getOptionalType (executor->getType ()));
1962
- } else {
1963
- // Create the argc and argv arguments.
1964
- auto entry = TopLevelSGF.B .getInsertionBB ();
1965
- auto context = TopLevelSGF.getTypeExpansionContext ();
1966
- auto paramTypeIter =
1967
- TopLevelSGF.F .getConventions ().getParameterSILTypes (context).begin ();
1968
-
1969
- entry->createFunctionArgument (*paramTypeIter);
1970
- entry->createFunctionArgument (*std::next (paramTypeIter));
1971
- }
1972
-
1973
- {
1974
- Scope S (TopLevelSGF.Cleanups , moduleCleanupLoc);
1975
- SILGenTopLevel (TopLevelSGF).visitSourceFile (SF);
1976
- }
1977
-
1978
- // Unregister the top-level function emitter.
1979
- TopLevelSGF.stopEmittingTopLevelCode ();
1980
-
1981
- // Write out the epilog.
1982
- auto moduleLoc = RegularLocation::getModuleLocation ();
1983
- moduleLoc.markAutoGenerated ();
1984
- auto returnInfo = TopLevelSGF.emitEpilogBB (moduleLoc);
1985
- auto returnLoc = returnInfo.second ;
1986
- returnLoc.markAutoGenerated ();
1987
-
1988
- SILFunction *exitFunc = nullptr ;
1989
-
1990
- SILType returnType;
1991
- if (isAsyncTopLevel) {
1992
- FuncDecl *exitFuncDecl = getExit ();
1993
- assert (exitFuncDecl && " Failed to find exit function declaration" );
1994
- exitFunc = getFunction (
1995
- SILDeclRef (exitFuncDecl, SILDeclRef::Kind::Func, /* isForeign*/ true ),
1996
- NotForDefinition);
1997
- SILFunctionType &funcType =
1998
- *exitFunc->getLoweredType ().getAs <SILFunctionType>();
1999
- returnType = SILType::getPrimitiveObjectType (
2000
- funcType.getParameters ().front ().getInterfaceType ());
2001
- } else {
2002
- returnType = TopLevelSGF.F .getConventions ().getSingleSILResultType (
2003
- TopLevelSGF.getTypeExpansionContext ());
2004
- }
2005
-
2006
- auto emitTopLevelReturnValue = [&](unsigned value) -> SILValue {
2007
- // Create an integer literal for the value.
2008
- auto litType = SILType::getBuiltinIntegerType (32 , getASTContext ());
2009
- SILValue retValue =
2010
- TopLevelSGF.B .createIntegerLiteral (moduleLoc, litType, value);
2011
-
2012
- // Wrap that in a struct if necessary.
2013
- if (litType != returnType) {
2014
- retValue = TopLevelSGF.B .createStruct (moduleLoc, returnType, retValue);
2015
- }
2016
- return retValue;
2017
- };
2018
-
2019
- // Fallthrough should signal a normal exit by returning 0.
2020
- SILValue returnValue;
2021
- if (TopLevelSGF.B .hasValidInsertionPoint ())
2022
- returnValue = emitTopLevelReturnValue (0 );
2023
-
2024
- // Handle the implicit rethrow block.
2025
- auto rethrowBB = TopLevelSGF.ThrowDest .getBlock ();
2026
- TopLevelSGF.ThrowDest = JumpDest::invalid ();
2027
-
2028
- // If the rethrow block wasn't actually used, just remove it.
2029
- if (rethrowBB->pred_empty ()) {
2030
- TopLevelSGF.eraseBasicBlock (rethrowBB);
2031
-
2032
- // Otherwise, we need to produce a unified return block.
2033
- } else {
2034
- auto returnBB = TopLevelSGF.createBasicBlock ();
2035
- if (TopLevelSGF.B .hasValidInsertionPoint ())
2036
- TopLevelSGF.B .createBranch (returnLoc, returnBB, returnValue);
2037
- returnValue = returnBB->createPhiArgument (returnType, OwnershipKind::Owned);
2038
- TopLevelSGF.B .emitBlock (returnBB);
2039
-
2040
- // Emit the rethrow block.
2041
- SILGenSavedInsertionPoint savedIP (TopLevelSGF, rethrowBB,
2042
- FunctionSection::Postmatter);
2043
-
2044
- // Log the error.
2045
- SILValue error = rethrowBB->getArgument (0 );
2046
- TopLevelSGF.B .createBuiltin (moduleLoc,
2047
- getASTContext ().getIdentifier (" errorInMain" ),
2048
- Types.getEmptyTupleType (), {}, {error});
2049
-
2050
- // Then end the lifetime of the error.
2051
- //
2052
- // We do this to appease the ownership verifier. We do not care about
2053
- // actually destroying the value since we are going to immediately exit,
2054
- // so this saves us a slight bit of code-size since end_lifetime is
2055
- // stripped out after ownership is removed.
2056
- TopLevelSGF.B .createEndLifetime (moduleLoc, error);
2057
-
2058
- // Signal an abnormal exit by returning 1.
2059
- TopLevelSGF.Cleanups .emitCleanupsForReturn (CleanupLocation (moduleLoc),
2060
- IsForUnwind);
2061
- TopLevelSGF.B .createBranch (returnLoc, returnBB, emitTopLevelReturnValue (1 ));
2062
- }
2063
-
2064
- // Return.
2065
- if (TopLevelSGF.B .hasValidInsertionPoint ()) {
2066
-
2067
- if (isAsyncTopLevel) {
2068
- SILValue exitCall = TopLevelSGF.B .createFunctionRef (moduleLoc, exitFunc);
2069
- TopLevelSGF.B .createApply (moduleLoc, exitCall, {}, {returnValue});
2070
- TopLevelSGF.B .createUnreachable (moduleLoc);
2071
- } else {
2072
- TopLevelSGF.B .createReturn (returnLoc, returnValue);
2073
- }
2074
- }
2075
-
2076
- // Okay, we're done emitting the top-level function; destroy the
2077
- // emitter and verify the result.
2078
- SILFunction &toplevel = TopLevelSGF.getFunction ();
2079
-
2080
- LLVM_DEBUG (llvm::dbgs () << " lowered toplevel sil:\n " ;
2081
- toplevel.print (llvm::dbgs ()));
2082
- toplevel.verifyIncompleteOSSA ();
2083
- emitLazyConformancesForFunction (&toplevel);
2084
- }
2085
-
2086
- void SILGenModule::emitEntryPoint (SourceFile *SF) {
2087
- assert (!M.lookUpFunction (getASTContext ().getEntryPointFunctionName ()) &&
2088
- " already emitted toplevel?!" );
2089
-
2090
- auto mainEntryRef = SILDeclRef::getMainFileEntryPoint (SF);
2091
- SILFunction *TopLevel = getFunction (mainEntryRef, ForDefinition);
2092
- TopLevel->setBare (IsBare);
2093
- emitEntryPoint (SF, TopLevel);
2094
- }
2095
-
2096
1931
namespace {
2097
1932
2098
1933
// An RAII object that constructs a \c SILGenModule instance.
0 commit comments