mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-26 07:24:25 +00:00
Added routine to create a char array for a string.
Also, print char arrays as strings. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@799 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@ -13,6 +13,8 @@
|
|||||||
#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);
|
||||||
@ -161,7 +163,38 @@ 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
|
||||||
|
// ubytes or an array of sbytes with positive values.
|
||||||
|
//
|
||||||
|
const Type* elType = cast<ArrayType>(this->getType())->getElementType();
|
||||||
|
bool isString = (elType == Type::SByteTy || elType == Type::UByteTy)
|
||||||
|
&& Operands.size() > 0;
|
||||||
|
if (isString) {
|
||||||
|
for (unsigned i = 0; i < Operands.size(); i++) {
|
||||||
|
if (elType == Type::SByteTy && cast<ConstPoolSInt>(Operands[i]) < 0)
|
||||||
|
{
|
||||||
|
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) {
|
||||||
|
makeString << ends;
|
||||||
|
Result = "c\"";
|
||||||
|
Result += makeString.str();
|
||||||
|
Result += "\"";
|
||||||
|
free(makeString.str());
|
||||||
|
} else {
|
||||||
|
Result = "[";
|
||||||
if (Operands.size()) {
|
if (Operands.size()) {
|
||||||
Result += " " + Operands[0]->getType()->getDescription() +
|
Result += " " + Operands[0]->getType()->getDescription() +
|
||||||
" " + cast<ConstPoolVal>(Operands[0])->getStrValue();
|
" " + cast<ConstPoolVal>(Operands[0])->getStrValue();
|
||||||
@ -169,8 +202,10 @@ string ConstPoolArray::getStrValue() const {
|
|||||||
Result += ", " + Operands[i]->getType()->getDescription() +
|
Result += ", " + Operands[i]->getType()->getDescription() +
|
||||||
" " + cast<ConstPoolVal>(Operands[i])->getStrValue();
|
" " + cast<ConstPoolVal>(Operands[i])->getStrValue();
|
||||||
}
|
}
|
||||||
|
Result += " ]";
|
||||||
|
}
|
||||||
|
|
||||||
return Result + " ]";
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string ConstPoolStruct::getStrValue() const {
|
string ConstPoolStruct::getStrValue() const {
|
||||||
@ -384,6 +419,7 @@ 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) {
|
||||||
@ -393,6 +429,35 @@ ConstPoolArray *ConstPoolArray::get(const ArrayType *Ty,
|
|||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ConstPoolArray *ConstPoolArray::get(const string& stringConstant) {
|
||||||
|
ConstPoolArray *Result = StringConstants[stringConstant.c_str()];
|
||||||
|
if (Result == NULL) {
|
||||||
|
ArrayType *aty = ArrayType::get(Type::UByteTy/*,stringConstant.length()*/);
|
||||||
|
vector<ConstPoolVal*> charVals;
|
||||||
|
for (const char *c = stringConstant.c_str(); *c; ++c) {
|
||||||
|
if (isprint(*c))
|
||||||
|
charVals.push_back(ConstPoolUInt::get(Type::UByteTy, *c));
|
||||||
|
else {
|
||||||
|
char charString[3];
|
||||||
|
sprintf(charString, "\\%02X", *c);
|
||||||
|
charVals.push_back(ConstPoolUInt::get(Type::UByteTy, charString[0]));
|
||||||
|
charVals.push_back(ConstPoolUInt::get(Type::UByteTy, charString[1]));
|
||||||
|
charVals.push_back(ConstPoolUInt::get(Type::UByteTy, charString[2]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// destroyConstant - Remove the constant from the constant table...
|
// destroyConstant - Remove the constant from the constant table...
|
||||||
//
|
//
|
||||||
void ConstPoolArray::destroyConstant() {
|
void ConstPoolArray::destroyConstant() {
|
||||||
|
Reference in New Issue
Block a user