Skip to content

Commit 05899b7

Browse files
author
Manman Ren
committed
SILParser: parse sil_global and sil_global_addr.
rdar://15493552 Swift SVN r10823
1 parent 15be357 commit 05899b7

File tree

9 files changed

+102
-11
lines changed

9 files changed

+102
-11
lines changed

include/swift/AST/Diagnostics.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,10 @@ ERROR(sil_vtable_func_not_found, decl_parsing,none,
440440
ERROR(sil_vtable_class_not_found, decl_parsing,none,
441441
"sil class not found %0", (Identifier))
442442

443+
// SIL Global
444+
ERROR(sil_global_variable_not_found, decl_parsing,none,
445+
"sil global not found %0", (Identifier))
446+
443447
//------------------------------------------------------------------------------
444448
// Type parsing diagnostics
445449
//------------------------------------------------------------------------------

include/swift/Parse/Parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ class Parser {
573573
bool parseDeclSIL();
574574
bool parseDeclSILStage();
575575
bool parseSILVTable();
576+
bool parseSILGlobal();
576577

577578
//===--------------------------------------------------------------------===//
578579
// Type Parsing

include/swift/Parse/Tokens.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ KEYWORD(undef) // This is only enabled when parsing .sil files.
4949
KEYWORD(sil) // This is only enabled when parsing .sil files.
5050
KEYWORD(sil_stage) // This is only enabled when parsing .sil files.
5151
KEYWORD(sil_vtable) // This is only enabled when parsing .sil files.
52+
KEYWORD(sil_global) // This is only enabled when parsing .sil files.
5253

5354
// Statement keywords.
5455
KEYWORD(if)

lib/Parse/Lexer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,8 @@ void Lexer::lexIdentifier() {
526526

527527
// These keywords are only active in SIL mode.
528528
if ((Kind == tok::kw_sil || Kind == tok::kw_sil_stage ||
529-
Kind == tok::kw_sil_vtable || Kind == tok::kw_undef) && !InSILMode)
529+
Kind == tok::kw_sil_vtable || Kind == tok::kw_sil_global ||
530+
Kind == tok::kw_undef) && !InSILMode)
530531
Kind = tok::identifier;
531532

532533
return formToken(Kind, TokStart);

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ bool Parser::parseTopLevel() {
6767
} else if (Tok.is(tok::kw_sil_vtable)) {
6868
assert(isInSILMode() && "'sil' should only be a keyword in SIL mode");
6969
parseSILVTable();
70+
} else if (Tok.is(tok::kw_sil_global)) {
71+
assert(isInSILMode() && "'sil' should only be a keyword in SIL mode");
72+
parseSILGlobal();
7073
} else {
7174
parseBraceItems(Items,
7275
allowTopLevelCode() ? BraceItemListKind::TopLevelCode

lib/Parse/ParseSIL.cpp

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ bool SILParser::parseSILOpcode(ValueKind &Opcode, SourceLoc &OpcodeLoc,
915915
.Case("ref_to_object_pointer", ValueKind::RefToObjectPointerInst)
916916
.Case("ref_to_raw_pointer", ValueKind::RefToRawPointerInst)
917917
.Case("ref_to_unowned", ValueKind::RefToUnownedInst)
918+
.Case("sil_global_addr", ValueKind::SILGlobalAddrInst)
918919
.Case("strong_release", ValueKind::StrongReleaseInst)
919920
.Case("strong_retain", ValueKind::StrongRetainInst)
920921
.Case("strong_retain_autoreleased", ValueKind::StrongRetainAutoreleasedInst)
@@ -1981,9 +1982,37 @@ bool SILParser::parseSILInstruction(SILBasicBlock *BB) {
19811982
ResultVal = B.createGlobalAddr(InstLoc, cast<VarDecl>(VD), Ty);
19821983
break;
19831984
}
1984-
case ValueKind::SILGlobalAddrInst:
1985-
llvm_unreachable("not implemented");
1985+
case ValueKind::SILGlobalAddrInst: {
1986+
Identifier GlobalName;
1987+
SourceLoc IdLoc;
1988+
SILType Ty;
1989+
if (P.parseToken(tok::at_sign, diag::expected_sil_value_name) ||
1990+
parseSILIdentifier(GlobalName, IdLoc, diag::expected_sil_value_name) ||
1991+
P.parseToken(tok::colon, diag::expected_tok_in_sil_instr, ":") ||
1992+
parseSILType(Ty))
1993+
return true;
1994+
1995+
// Go through list of global variables in the SILModule.
1996+
SILGlobalVariable *Global = nullptr;
1997+
for (SILGlobalVariable &g : SILMod.getSILGlobals())
1998+
if (g.getName().equals(GlobalName.str())) {
1999+
if (g.getLoweredType().getAddressType() != Ty) {
2000+
P.diagnose(IdLoc, diag::sil_value_use_type_mismatch, GlobalName.str(),
2001+
g.getLoweredType().getSwiftRValueType());
2002+
return true;
2003+
}
2004+
Global = &g;
2005+
break;
2006+
}
2007+
2008+
if (!Global) {
2009+
P.diagnose(IdLoc, diag::sil_global_variable_not_found, GlobalName);
2010+
return true;
2011+
}
19862012

2013+
ResultVal = B.createSILGlobalAddr(InstLoc, Global);
2014+
break;
2015+
}
19872016
case ValueKind::SwitchEnumInst:
19882017
case ValueKind::DestructiveSwitchEnumAddrInst: {
19892018
if (parseTypedValueRef(Val))
@@ -2437,6 +2466,36 @@ bool Parser::parseDeclSILStage() {
24372466
return false;
24382467
}
24392468

2469+
/// decl-sil-global: [[only in SIL mode]]
2470+
/// 'sil_global' sil-linkage @name : sil-type [external]
2471+
bool Parser::parseSILGlobal() {
2472+
consumeToken(tok::kw_sil_global);
2473+
SILLinkage GlobalLinkage;
2474+
Identifier GlobalName;
2475+
SILType GlobalType;
2476+
SourceLoc NameLoc;
2477+
2478+
// Inform the lexer that we're lexing the body of the SIL declaration.
2479+
Lexer::SILBodyRAII Tmp(*L);
2480+
if (parseSILLinkage(GlobalLinkage, *this) ||
2481+
parseToken(tok::at_sign, diag::expected_sil_value_name) ||
2482+
parseIdentifier(GlobalName, NameLoc, diag::expected_sil_value_name) ||
2483+
parseToken(tok::colon, diag::expected_sil_type))
2484+
return true;
2485+
2486+
SILParser State(*this);
2487+
if (State.parseSILType(GlobalType))
2488+
return true;
2489+
2490+
bool IsExternal = false;
2491+
if (parseSILOptional(IsExternal, State, "external"))
2492+
return true;
2493+
new (*SIL->M) SILGlobalVariable(*SIL->M, GlobalLinkage, GlobalName.str(),
2494+
GlobalType, !IsExternal,
2495+
SILFileLocation(NameLoc));
2496+
return false;
2497+
}
2498+
24402499
/// decl-sil-vtable: [[only in SIL mode]]
24412500
/// 'sil_vtable' ClassName decl-sil-vtable-body
24422501
/// decl-sil-vtable-body:

lib/Parse/ParseStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
218218
Tok.isNot(tok::r_brace)) &&
219219
Tok.isNot(tok::eof) &&
220220
Tok.isNot(tok::kw_sil) && Tok.isNot(tok::kw_sil_stage) &&
221-
Tok.isNot(tok::kw_sil_vtable) &&
221+
Tok.isNot(tok::kw_sil_vtable) && Tok.isNot(tok::kw_sil_global) &&
222222
!isTerminatorForBraceItemListKind(Tok, Kind, Entries)) {
223223
if (Kind == BraceItemListKind::TopLevelLibrary &&
224224
skipExtraTopLevelRBraces())

lib/SIL/SILPrinter.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,9 +1191,6 @@ void SILFunction::printName(raw_ostream &OS) const {
11911191
void SILGlobalVariable::print(llvm::raw_ostream &OS, bool Verbose) const {
11921192
OS << "// " << demangleSymbolAsString(getName()) << '\n';
11931193

1194-
// FIXME: Parsing support for sil_global.
1195-
OS << "/* ";
1196-
11971194
OS << "sil_global ";
11981195
printLinkage(OS, getLinkage());
11991196

@@ -1203,9 +1200,6 @@ void SILGlobalVariable::print(llvm::raw_ostream &OS, bool Verbose) const {
12031200
if (isExternalDeclaration())
12041201
OS << "external";
12051202

1206-
// FIXME: Parsing support for sil_global.
1207-
OS << " */";
1208-
12091203
OS << "\n\n";
12101204
}
12111205

test/SIL/Parser/basic.sil

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ sil_stage raw // CHECK: sil_stage raw
55
import Builtin
66
import swift
77

8+
// CHECK: sil_global internal @globalinit_token0 : $Builtin.Word
9+
sil_global internal @globalinit_token0 : $Builtin.Word
10+
811
// Linkage types.
912

1013
// CHECK-LABEL: sil thunk @clang_thunk : $() -> ()
@@ -1023,6 +1026,32 @@ entry(%0 : $Builtin.Int1):
10231026
return %1 : $()
10241027
}
10251028

1029+
var staticProp: Int64 = 0
1030+
1031+
// CHECK-LABEL: sil internal @globalinit_func0 : $@thin () -> () {
1032+
sil internal @globalinit_func0 : $@thin () -> () {
1033+
bb0:
1034+
%0 = global_addr #staticProp : $*Int64
1035+
%1 = mark_uninitialized [globalvar] %0 : $*Int64
1036+
%7 = tuple ()
1037+
return %7 : $()
1038+
}
1039+
1040+
// CHECK-LABEL: sil @_TV18lazy_global_access4Type10staticPropSia : $@thin () -> Builtin.RawPointer {
1041+
sil @_TV18lazy_global_access4Type10staticPropSia : $@thin () -> Builtin.RawPointer {
1042+
bb0:
1043+
%0 = builtin_function_ref "once" : $@thin (Builtin.RawPointer, @owned @callee_owned () -> ()) -> ()
1044+
// CHECK: sil_global_addr @globalinit_token0 : $*Builtin.Word
1045+
%1 = sil_global_addr @globalinit_token0 : $*Builtin.Word
1046+
%2 = address_to_pointer %1 : $*Builtin.Word to $Builtin.RawPointer
1047+
%3 = function_ref @globalinit_func0 : $@thin () -> ()
1048+
%4 = thin_to_thick_function %3 : $@thin () -> () to $@callee_owned () -> ()
1049+
%5 = apply %0(%2, %4) : $@thin (Builtin.RawPointer, @owned @callee_owned () -> ()) -> ()
1050+
%6 = global_addr #staticProp : $*Int64
1051+
%7 = address_to_pointer %6 : $*Int64 to $Builtin.RawPointer
1052+
return %7 : $Builtin.RawPointer
1053+
}
1054+
10261055
// CHECK-LABEL: sil_vtable Foo {
10271056
// CHECK: #Foo.subscript!getter.2: _TC3tmp3Foo9subscriptFT1xSi1ySi_Sig
10281057
// CHECK: #Foo.subscript!setter.2: _TC3tmp3Foo9subscriptFT1xSi1ySi_Sis
@@ -1037,4 +1066,3 @@ bb0:
10371066
store undef to undef : $*Builtin.Int1
10381067
unreachable
10391068
}
1040-

0 commit comments

Comments
 (0)