1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 19:52:54 +01:00

* Fix the constpoolarray -> c"" printing routines to escape things properly

* Fix slightly broken sharing problems
* Do not escape the literal string passed in

llvm-svn: 814
This commit is contained in:
Chris Lattner 2001-10-14 23:54:12 +00:00
parent 657954e39b
commit fc567e1535

View File

@ -13,8 +13,6 @@
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/Analysis/SlotCalculator.h" #include "llvm/Analysis/SlotCalculator.h"
#include <algorithm> #include <algorithm>
#include <hash_map>
#include <strstream.h>
#include <assert.h> #include <assert.h>
ConstPoolBool *ConstPoolBool::True = new ConstPoolBool(true); ConstPoolBool *ConstPoolBool::True = new ConstPoolBool(true);
@ -164,35 +162,36 @@ string ConstPoolFP::getStrValue() const {
string ConstPoolArray::getStrValue() const { string ConstPoolArray::getStrValue() const {
string Result; string Result;
strstream makeString;
// As a special case, print the array as a string if it is an array of // As a special case, print the array as a string if it is an array of
// ubytes or an array of sbytes with positive values. // ubytes or an array of sbytes with positive values.
// //
const Type* elType = cast<ArrayType>(this->getType())->getElementType(); const Type *ETy = cast<ArrayType>(getType())->getElementType();
bool isString = (elType == Type::SByteTy || elType == Type::UByteTy) bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
&& Operands.size() > 0; for (unsigned i = 0; i < Operands.size(); i++)
if (isString) { if (ETy == Type::SByteTy &&
for (unsigned i = 0; i < Operands.size(); i++) { cast<ConstPoolSInt>(Operands[i])->getValue() < 0) {
if (elType == Type::SByteTy && cast<ConstPoolSInt>(Operands[i]) < 0) isString = false;
{ break;
isString = false;
break;
}
// else it is a legal char and is safe to cast to an unsigned int
uint64_t c = ((elType == Type::SByteTy)
? (uint64_t) cast<ConstPoolSInt>(Operands[i])->getValue()
: cast<ConstPoolUInt>(Operands[i])->getValue());
makeString << (char) c;
} }
}
if (isString) { if (isString) {
makeString << ends; Result = "c\"";
Result = "c\""; for (unsigned i = 0; i < Operands.size(); i++) {
Result += makeString.str(); unsigned char C = (ETy == Type::SByteTy) ?
(unsigned char)cast<ConstPoolSInt>(Operands[i])->getValue() :
(unsigned char)cast<ConstPoolUInt>(Operands[i])->getValue();
if (isprint(C)) {
Result += C;
} else {
Result += '\\';
Result += (C/16)+'0';
Result += (C&15)+'0';
}
}
Result += "\""; Result += "\"";
free(makeString.str());
} else { } else {
Result = "["; Result = "[";
if (Operands.size()) { if (Operands.size()) {
@ -419,7 +418,6 @@ ConstPoolFP *ConstPoolFP::get(const Type *Ty, double V) {
//---- ConstPoolArray::get() implementation... //---- ConstPoolArray::get() implementation...
// //
static ValueMap<vector<ConstPoolVal*>, ConstPoolArray> ArrayConstants; static ValueMap<vector<ConstPoolVal*>, ConstPoolArray> ArrayConstants;
static hash_map<const char*, ConstPoolArray*> StringConstants;
ConstPoolArray *ConstPoolArray::get(const ArrayType *Ty, ConstPoolArray *ConstPoolArray::get(const ArrayType *Ty,
const vector<ConstPoolVal*> &V) { const vector<ConstPoolVal*> &V) {
@ -429,32 +427,21 @@ ConstPoolArray *ConstPoolArray::get(const ArrayType *Ty,
return Result; return Result;
} }
ConstPoolArray *ConstPoolArray::get(const string& stringConstant) { // ConstPoolArray::get(const string&) - Return an array that is initialized to
ConstPoolArray *Result = StringConstants[stringConstant.c_str()]; // contain the specified string. A null terminator is added to the specified
if (Result == NULL) { // string so that it may be used in a natural way...
ArrayType *aty = ArrayType::get(Type::UByteTy/*,stringConstant.length()*/); //
vector<ConstPoolVal*> charVals; ConstPoolArray *ConstPoolArray::get(const string &Str) {
for (const char *c = stringConstant.c_str(); *c; ++c) { vector<ConstPoolVal*> ElementVals;
if (isprint(*c))
charVals.push_back(ConstPoolUInt::get(Type::UByteTy, *c)); for (unsigned i = 0; i < Str.length(); ++i)
else { ElementVals.push_back(ConstPoolUInt::get(Type::UByteTy, Str[i]));
char charString[3];
sprintf(charString, "\\%02X", *c); // Add a null terminator to the string...
charVals.push_back(ConstPoolUInt::get(Type::UByteTy, charString[0])); ElementVals.push_back(ConstPoolUInt::get(Type::UByteTy, 0));
charVals.push_back(ConstPoolUInt::get(Type::UByteTy, charString[1]));
charVals.push_back(ConstPoolUInt::get(Type::UByteTy, charString[2])); ArrayType *ATy = ArrayType::get(Type::UByteTy/*,stringConstant.length()*/);
} return ConstPoolArray::get(ATy, ElementVals);
}
// Append "\00" which is the null character in LLVM
charVals.push_back(ConstPoolUInt::get(Type::UByteTy, '\\'));
charVals.push_back(ConstPoolUInt::get(Type::UByteTy, '0'));
charVals.push_back(ConstPoolUInt::get(Type::UByteTy, '0'));
Result = ConstPoolArray::get(aty, charVals);
StringConstants[stringConstant.c_str()] = Result;
}
return Result;
} }