llvm.org GIT mirror llvm / 3702d26
For PR645: Implement new syntax for local and global symbols. Types and local symbols use the % prefix. Global variables and functions use the @ prefix. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33523 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Spencer 13 years ago
1 changed file(s) with 53 addition(s) and 93 deletion(s). Raw diff Collapse all Expand all
178178 return 0;
179179 }
180180
181 // getLLVMName - Turn the specified string into an 'LLVM name', which is either
182 // prefixed with % (if the string only contains simple characters) or is
183 // surrounded with ""'s (if it has special chars in it).
184 static std::string getLLVMName(const std::string &Name,
185 bool prefixName = true) {
186 assert(!Name.empty() && "Cannot get empty name!");
187
188 // First character cannot start with a number...
189 if (Name[0] >= '0' && Name[0] <= '9')
190 return "\"" + Name + "\"";
191
181 /// NameNeedsQuotes - Return true if the specified llvm name should be wrapped
182 /// with ""'s.
183 static bool NameNeedsQuotes(const std::string &Name) {
184 if (Name[0] >= '0' && Name[0] <= '9') return true;
192185 // Scan to see if we have any characters that are not on the "white list"
193186 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
194187 char C = Name[i];
195188 assert(C != '"' && "Illegal character in LLVM value name!");
196189 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
197190 C != '-' && C != '.' && C != '_')
198 return "\"" + Name + "\"";
191 return true;
192 }
193 return false;
194 }
195
196 enum PrefixType {
197 GlobalPrefix,
198 LabelPrefix,
199 LocalPrefix
200 };
201
202 /// getLLVMName - Turn the specified string into an 'LLVM name', which is either
203 /// prefixed with % (if the string only contains simple characters) or is
204 /// surrounded with ""'s (if it has special chars in it).
205 static std::string getLLVMName(const std::string &Name, PrefixType Prefix) {
206 assert(!Name.empty() && "Cannot get empty name!");
207
208 // First character cannot start with a number...
209 if (NameNeedsQuotes(Name)) {
210 if (Prefix == GlobalPrefix)
211 return "@\"" + Name + "\"";
212 return "\"" + Name + "\"";
199213 }
200214
201215 // If we get here, then the identifier is legal to use as a "VarID".
202 if (prefixName)
203 return "%"+Name;
204 else
205 return Name;
216 switch (Prefix) {
217 default: assert(0 && "Bad prefix!");
218 case GlobalPrefix: return '@' + Name;
219 case LabelPrefix: return Name;
220 case LocalPrefix: return '%' + Name;
221 }
206222 }
207223
208224
223239 !cast(Ty)->getElementType()->isPrimitiveType() ||
224240 !cast(Ty)->getElementType()->isInteger() ||
225241 isa(cast(Ty)->getElementType()))
226 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
242 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first, LocalPrefix)));
227243 }
228244 }
229245
586602 SlotMachine *Machine) {
587603 Out << ' ';
588604 if (V->hasName())
589 Out << getLLVMName(V->getName());
605 Out << getLLVMName(V->getName(),
606 isa(V) ? GlobalPrefix : LocalPrefix);
590607 else {
591608 const Constant *CV = dyn_cast(V);
592609 if (CV && !isa(CV)) {
601618 PrintEscapedString(IA->getConstraintString(), Out);
602619 Out << '"';
603620 } else {
621 char Prefix = '%';
604622 int Slot;
605623 if (Machine) {
606 if (const GlobalValue *GV = dyn_cast(V))
624 if (const GlobalValue *GV = dyn_cast(V)) {
607625 Slot = Machine->getGlobalSlot(GV);
608 else
626 Prefix = '@';
627 } else {
609628 Slot = Machine->getLocalSlot(V);
629 }
610630 } else {
611631 Machine = createSlotMachine(V);
612632 if (Machine) {
613 if (const GlobalValue *GV = dyn_cast(V))
633 if (const GlobalValue *GV = dyn_cast(V)) {
614634 Slot = Machine->getGlobalSlot(GV);
615 else
635 Prefix = '@';
636 } else {
616637 Slot = Machine->getLocalSlot(V);
638 }
617639 } else {
618640 Slot = -1;
619641 }
620642 delete Machine;
621643 }
622644 if (Slot != -1)
623 Out << '%' << Slot;
645 Out << Prefix << Slot;
624646 else
625647 Out << "";
626648 }
671693 inline void write(const Function *F) { printFunction(F); }
672694 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
673695 inline void write(const Instruction *I) { printInstruction(*I); }
674 inline void write(const Constant *CPV) { printConstant(CPV); }
675696 inline void write(const Type *Ty) { printType(Ty); }
676697
677698 void writeOperand(const Value *Op, bool PrintType);
681702 private:
682703 void printModule(const Module *M);
683704 void printTypeSymbolTable(const TypeSymbolTable &ST);
684 void printValueSymbolTable(const SymbolTable &ST);
685 void printConstant(const Constant *CPV);
686705 void printGlobal(const GlobalVariable *GV);
687706 void printFunction(const Function *F);
688707 void printArgument(const Argument *FA, FunctionType::ParameterAttributes A);
786805
787806 if (!M->getDataLayout().empty())
788807 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
789
790 switch (M->getEndianness()) {
791 case Module::LittleEndian: Out << "target endian = little\n"; break;
792 case Module::BigEndian: Out << "target endian = big\n"; break;
793 case Module::AnyEndianness: break;
794 }
795 switch (M->getPointerSize()) {
796 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
797 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
798 case Module::AnyPointerSize: break;
799 }
800808 if (!M->getTargetTriple().empty())
801809 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
802810
836844
837845 // Loop over the symbol table, emitting all named constants.
838846 printTypeSymbolTable(M->getTypeSymbolTable());
839 printValueSymbolTable(M->getValueSymbolTable());
840847
841848 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
842849 I != E; ++I)
850857 }
851858
852859 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
853 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
860 if (GV->hasName()) Out << getLLVMName(GV->getName(), GlobalPrefix) << " = ";
854861
855862 if (!GV->hasInitializer())
856863 switch (GV->getLinkage()) {
900907 // Print the types.
901908 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
902909 TI != TE; ++TI) {
903 Out << "\t" << getLLVMName(TI->first) << " = type ";
910 Out << "\t" << getLLVMName(TI->first, LocalPrefix) << " = type ";
904911
905912 // Make sure we print out at least one level of the type structure, so
906913 // that we do not get %FILE = type %FILE
907914 //
908915 printTypeAtLeastOneLevel(TI->second) << "\n";
909916 }
910 }
911
912 // printSymbolTable - Run through symbol table looking for constants
913 // and types. Emit their declarations.
914 void AssemblyWriter::printValueSymbolTable(const SymbolTable &ST) {
915
916 // Print the constants, in type plane order.
917 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
918 PI != ST.plane_end(); ++PI) {
919 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
920 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
921
922 for (; VI != VE; ++VI) {
923 const Value* V = VI->second;
924 const Constant *CPV = dyn_cast(V) ;
925 if (CPV && !isa(V)) {
926 printConstant(CPV);
927 }
928 }
929 }
930 }
931
932
933 /// printConstant - Print out a constant pool entry...
934 ///
935 void AssemblyWriter::printConstant(const Constant *CPV) {
936 // Don't print out unnamed constants, they will be inlined
937 if (!CPV->hasName()) return;
938
939 // Print out name...
940 Out << "\t" << getLLVMName(CPV->getName()) << " =";
941
942 // Write the value out now.
943 writeOperand(CPV, true);
944
945 printInfoComment(*CPV);
946 Out << "\n";
947917 }
948918
949919 /// printFunction - Print all aspects of a function.
951921 void AssemblyWriter::printFunction(const Function *F) {
952922 // Print out the return type and name...
953923 Out << "\n";
954
955 // Ensure that no local symbols conflict with global symbols.
956 const_cast(F)->renameLocalSymbols();
957924
958925 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
959926
999966 const FunctionType *FT = F->getFunctionType();
1000967 printType(F->getReturnType()) << ' ';
1001968 if (!F->getName().empty())
1002 Out << getLLVMName(F->getName());
969 Out << getLLVMName(F->getName(), GlobalPrefix);
1003970 else
1004 Out << "\"\"";
971 Out << "@\"\"";
1005972 Out << '(';
1006973 Machine.incorporateFunction(F);
1007974
10571024
10581025 // Output name, if available...
10591026 if (Arg->hasName())
1060 Out << ' ' << getLLVMName(Arg->getName());
1027 Out << ' ' << getLLVMName(Arg->getName(), LocalPrefix);
10611028 }
10621029
10631030 /// printBasicBlock - This member is called for each basic block in a method.
10641031 ///
10651032 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
10661033 if (BB->hasName()) { // Print out the label if it exists...
1067 Out << "\n" << getLLVMName(BB->getName(), false) << ':';
1034 Out << "\n" << getLLVMName(BB->getName(), LabelPrefix) << ':';
10681035 } else if (!BB->use_empty()) { // Don't print block # of no uses...
10691036 Out << "\n;
10701037 int Slot = Machine.getLocalSlot(BB);
11381105
11391106 // Print out name if it exists...
11401107 if (I.hasName())
1141 Out << getLLVMName(I.getName()) << " = ";
1108 Out << getLLVMName(I.getName(), LocalPrefix) << " = ";
11421109
11431110 // If this is a volatile load or store, print out the volatile marker.
11441111 if ((isa(I) && cast(I).isVolatile()) ||
15321499
15331500 // Lookup the Value in the function and module maps.
15341501 ValueMap::const_iterator FVI = FI->second.map.find(V);
1535 TypedPlanes::const_iterator MI = mMap.find(VTy);
15361502
15371503 // If the value doesn't exist in the function map, it is a
15381504 if (FVI == FI->second.map.end()) return -1;
15391505
1540 // Return the slot number as the module's contribution to
1541 // the type plane plus the index in the function's contribution
1542 // to the type plane.
1543 if (MI != mMap.end())
1544 return MI->second.next_slot + FVI->second;
1545 else
1546 return FVI->second;
1506 return FVI->second;
15471507 }
15481508
15491509