blob: bb5f75de3ea79274017a012689ac504b63afdf4a [file] [log] [blame]
Chris Lattner16c7bb22002-05-09 02:28:59 +00001//===-- Writer.cpp - Library for writing C files --------------------------===//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00002//
3// This library implements the functionality defined in llvm/Assembly/CWriter.h
4// and CLocalVars.h
5//
6// TODO : Recursive types.
Chris Lattner16c7bb22002-05-09 02:28:59 +00007//
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00008//===-----------------------------------------------------------------------==//
Chris Lattner16c7bb22002-05-09 02:28:59 +00009
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000010#include "llvm/Assembly/CWriter.h"
11#include "CLocalVars.h"
12#include "llvm/SlotCalculator.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000013#include "llvm/Constants.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000014#include "llvm/DerivedTypes.h"
15#include "llvm/Module.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000016#include "llvm/GlobalVariable.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000017#include "llvm/Function.h"
18#include "llvm/Argument.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000019#include "llvm/BasicBlock.h"
20#include "llvm/iMemory.h"
21#include "llvm/iTerminators.h"
22#include "llvm/iPHINode.h"
23#include "llvm/iOther.h"
Chris Lattner2f5f51a2002-05-09 03:28:37 +000024#include "llvm/iOperators.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000025#include "llvm/SymbolTable.h"
26#include "llvm/Support/InstVisitor.h"
27#include "Support/StringExtras.h"
28#include "Support/STLExtras.h"
29
30#include <algorithm>
31#include <strstream>
32using std::string;
33using std::map;
34using std::vector;
35using std::ostream;
36
Chris Lattner16c7bb22002-05-09 02:28:59 +000037//===-----------------------------------------------------------------------==//
38//
39// Implementation of the CLocalVars methods
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000040
41// Appends a variable to the LocalVars map if it does not already exist
42// Also check that the type exists on the map.
43void CLocalVars::addLocalVar(const Type *t, const string & var) {
44 if (!LocalVars.count(t) ||
45 find(LocalVars[t].begin(), LocalVars[t].end(), var)
46 == LocalVars[t].end()) {
47 LocalVars[t].push_back(var);
48 }
49}
50
Chris Lattner16c7bb22002-05-09 02:28:59 +000051static string calcTypeNameVar(const Type *Ty,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000052 map<const Type *, string> &TypeNames,
53 string VariableName, string NameSoFar);
54
55static std::string getConstStrValue(const Constant* CPV);
56
57
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000058// We dont want identifier names with ., space, - in them.
59// So we replace them with _
60static string makeNameProper(string x) {
61 string tmp;
62 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++) {
63 if (*sI == '.')
64 tmp += '_';
65 else if (*sI == ' ')
66 tmp += '_';
67 else if (*sI == '-')
68 tmp += "__";
69 else
70 tmp += *sI;
71 }
72 return tmp;
73}
74
75static string getConstantName(const Constant *CPV) {
76 return CPV->getName();
77}
78
79
80static std::string getConstArrayStrValue(const Constant* CPV) {
81 std::string Result;
82
83 // As a special case, print the array as a string if it is an array of
84 // ubytes or an array of sbytes with positive values.
85 //
86 const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType();
87 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
88
89 if (ETy == Type::SByteTy) {
90 for (unsigned i = 0; i < CPV->getNumOperands(); ++i)
91 if (ETy == Type::SByteTy &&
92 cast<ConstantSInt>(CPV->getOperand(i))->getValue() < 0) {
93 isString = false;
94 break;
95 }
96 }
97
98 if (isString) {
99 Result = "\"";
100 for (unsigned i = 0; i < CPV->getNumOperands(); ++i) {
101 unsigned char C = (ETy == Type::SByteTy) ?
102 (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() :
103 (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue();
104
105 if (isprint(C)) {
106 Result += C;
107 } else {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000108 Result += "\\x";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000109 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
110 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
111 }
112 }
113 Result += "\"";
114
115 } else {
116 Result = "{";
117 if (CPV->getNumOperands()) {
118 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
119 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
120 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
121 }
122 Result += " }";
123 }
124
125 return Result;
126}
127
128static std::string getConstStructStrValue(const Constant* CPV) {
129 std::string Result = "{";
130 if (CPV->getNumOperands()) {
131 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
132 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
133 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
134 }
135
136 return Result + " }";
137}
138
139// our own getStrValue function for constant initializers
140static std::string getConstStrValue(const Constant* CPV) {
141 // Does not handle null pointers, that needs to be checked explicitly
142 string tempstr;
143 if (CPV == ConstantBool::False)
144 return "0";
145 else if (CPV == ConstantBool::True)
146 return "1";
147
148 else if (isa<ConstantArray>(CPV)) {
149 tempstr = getConstArrayStrValue(CPV);
150 }
151 else if (isa<ConstantStruct>(CPV)) {
152 tempstr = getConstStructStrValue(CPV);
153 }
154 else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CPV)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000155 tempstr = utostr(CUI->getValue());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000156 }
157 else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CPV)) {
158 tempstr = itostr(CSI->getValue());
159 }
160 else if (ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) {
161 tempstr = ftostr(CFP->getValue());
162 }
163
164 if (CPV->getType() == Type::ULongTy)
165 tempstr += "ull";
166 else if (CPV->getType() == Type::LongTy)
167 tempstr += "ll";
168 else if (CPV->getType() == Type::UIntTy ||
169 CPV->getType() == Type::UShortTy)
170 tempstr += "u";
171
172 return tempstr;
173
174}
175
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000176// Internal function
177// Essentially pass the Type* variable, an empty typestack and this prints
178// out the C type
Chris Lattner16c7bb22002-05-09 02:28:59 +0000179static string calcTypeName(const Type *Ty, map<const Type *, string> &TypeNames,
180 string &FunctionInfo) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000181
182 // Takin' care of the fact that boolean would be int in C
183 // and that ushort would be unsigned short etc.
184
185 // Base Case
186 if (Ty->isPrimitiveType())
187 switch (Ty->getPrimitiveID()) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000188 case Type::VoidTyID: return "void";
189 case Type::BoolTyID: return "bool";
190 case Type::UByteTyID: return "unsigned char";
191 case Type::SByteTyID: return "signed char";
192 case Type::UShortTyID: return "unsigned short";
193 case Type::ShortTyID: return "short";
194 case Type::UIntTyID: return "unsigned";
195 case Type::IntTyID: return "int";
196 case Type::ULongTyID: return "unsigned long long";
197 case Type::LongTyID: return "signed long long";
198 case Type::FloatTyID: return "float";
199 case Type::DoubleTyID: return "double";
200 default : assert(0 && "Unknown primitive type!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000201 }
202
203 // Check to see if the type is named.
204 map<const Type *, string>::iterator I = TypeNames.find(Ty);
205 if (I != TypeNames.end())
206 return I->second;
207
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000208 string Result;
209 string MInfo = "";
210 switch (Ty->getPrimitiveID()) {
211 case Type::FunctionTyID: {
212 const FunctionType *MTy = cast<const FunctionType>(Ty);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000213 Result = calcTypeName(MTy->getReturnType(), TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000214 if (MInfo != "")
215 Result += ") " + MInfo;
216 Result += "(";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000217 FunctionInfo += " (";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000218 for (FunctionType::ParamTypes::const_iterator
219 I = MTy->getParamTypes().begin(),
220 E = MTy->getParamTypes().end(); I != E; ++I) {
221 if (I != MTy->getParamTypes().begin())
Chris Lattner16c7bb22002-05-09 02:28:59 +0000222 FunctionInfo += ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000223 MInfo = "";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000224 FunctionInfo += calcTypeName(*I, TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000225 if (MInfo != "")
226 Result += ") " + MInfo;
227 }
228 if (MTy->isVarArg()) {
229 if (!MTy->getParamTypes().empty())
Chris Lattner16c7bb22002-05-09 02:28:59 +0000230 FunctionInfo += ", ";
231 FunctionInfo += "...";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000232 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000233 FunctionInfo += ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000234 break;
235 }
236 case Type::StructTyID: {
237 string tempstr = "";
238 const StructType *STy = cast<const StructType>(Ty);
239 Result = " struct {\n ";
240 int indx = 0;
241 for (StructType::ElementTypes::const_iterator
242 I = STy->getElementTypes().begin(),
243 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000244 Result += calcTypeNameVar(*I, TypeNames,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000245 "field" + itostr(indx++), tempstr);
246 Result += ";\n ";
247 }
248 Result += " } ";
249 break;
250 }
251 case Type::PointerTyID:
252 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner16c7bb22002-05-09 02:28:59 +0000253 TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000254 Result += "*";
255 break;
256 case Type::ArrayTyID: {
257 const ArrayType *ATy = cast<const ArrayType>(Ty);
258 int NumElements = ATy->getNumElements();
Chris Lattner16c7bb22002-05-09 02:28:59 +0000259 Result = calcTypeName(ATy->getElementType(), TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000260 Result += "*";
261 break;
262 }
263 default:
264 assert(0 && "Unhandled case in getTypeProps!");
265 Result = "<error>";
266 }
267
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000268 return Result;
269}
270
271// Internal function
272// Pass the Type* variable and and the variable name and this prints out the
273// variable declaration.
274// This is different from calcTypeName because if you need to declare an array
275// the size of the array would appear after the variable name itself
276// For eg. int a[10];
Chris Lattner16c7bb22002-05-09 02:28:59 +0000277static string calcTypeNameVar(const Type *Ty,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000278 map<const Type *, string> &TypeNames,
279 string VariableName, string NameSoFar) {
280 if (Ty->isPrimitiveType())
281 switch (Ty->getPrimitiveID()) {
282 case Type::BoolTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000283 return "bool " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000284 case Type::UByteTyID:
285 return "unsigned char " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000286 case Type::SByteTyID:
287 return "signed char " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000288 case Type::UShortTyID:
289 return "unsigned long long " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000290 case Type::ULongTyID:
291 return "unsigned long long " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000292 case Type::LongTyID:
293 return "signed long long " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000294 case Type::UIntTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000295 return "unsigned " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000296 default :
297 return Ty->getDescription() + " " + NameSoFar + VariableName;
298 }
299
300 // Check to see if the type is named.
301 map<const Type *, string>::iterator I = TypeNames.find(Ty);
302 if (I != TypeNames.end())
303 return I->second + " " + NameSoFar + VariableName;
304
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000305 string Result;
306 string tempstr = "";
307
308 switch (Ty->getPrimitiveID()) {
309 case Type::FunctionTyID: {
310 string MInfo = "";
311 const FunctionType *MTy = cast<const FunctionType>(Ty);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000312 Result += calcTypeName(MTy->getReturnType(), TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000313 if (MInfo != "")
314 Result += ") " + MInfo;
315 Result += " " + NameSoFar + VariableName;
316 Result += " (";
317 for (FunctionType::ParamTypes::const_iterator
318 I = MTy->getParamTypes().begin(),
319 E = MTy->getParamTypes().end(); I != E; ++I) {
320 if (I != MTy->getParamTypes().begin())
321 Result += ", ";
322 MInfo = "";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000323 Result += calcTypeName(*I, TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000324 if (MInfo != "")
325 Result += ") " + MInfo;
326 }
327 if (MTy->isVarArg()) {
328 if (!MTy->getParamTypes().empty())
329 Result += ", ";
330 Result += "...";
331 }
332 Result += ")";
333 break;
334 }
335 case Type::StructTyID: {
336 const StructType *STy = cast<const StructType>(Ty);
337 Result = " struct {\n ";
338 int indx = 0;
339 for (StructType::ElementTypes::const_iterator
340 I = STy->getElementTypes().begin(),
341 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000342 Result += calcTypeNameVar(*I, TypeNames,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000343 "field" + itostr(indx++), "");
344 Result += ";\n ";
345 }
346 Result += " }";
347 Result += " " + NameSoFar + VariableName;
348 break;
349 }
350
351 case Type::PointerTyID: {
352 Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner16c7bb22002-05-09 02:28:59 +0000353 TypeNames, tempstr,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000354 "(*" + NameSoFar + VariableName + ")");
355 break;
356 }
357
358 case Type::ArrayTyID: {
359 const ArrayType *ATy = cast<const ArrayType>(Ty);
360 int NumElements = ATy->getNumElements();
Chris Lattner16c7bb22002-05-09 02:28:59 +0000361 Result = calcTypeNameVar(ATy->getElementType(), TypeNames,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000362 tempstr, NameSoFar + VariableName + "[" +
363 itostr(NumElements) + "]");
364 break;
365 }
366 default:
367 assert(0 && "Unhandled case in getTypeProps!");
368 Result = "<error>";
369 }
370
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000371 return Result;
372}
373
374// printTypeVarInt - The internal guts of printing out a type that has a
375// potentially named portion and the variable associated with the type.
376static ostream &printTypeVarInt(ostream &Out, const Type *Ty,
377 map<const Type *, string> &TypeNames,
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000378 const string &VariableName) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000379 // Primitive types always print out their description, regardless of whether
380 // they have been named or not.
381
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000382 if (Ty->isPrimitiveType())
383 switch (Ty->getPrimitiveID()) {
384 case Type::BoolTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000385 return Out << "bool " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000386 case Type::UByteTyID:
387 return Out << "unsigned char " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000388 case Type::SByteTyID:
389 return Out << "signed char " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000390 case Type::UShortTyID:
391 return Out << "unsigned long long " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000392 case Type::ULongTyID:
393 return Out << "unsigned long long " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000394 case Type::LongTyID:
395 return Out << "signed long long " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000396 case Type::UIntTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000397 return Out << "unsigned " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000398 default :
399 return Out << Ty->getDescription() << " " << VariableName;
400 }
401
402 // Check to see if the type is named.
403 map<const Type *, string>::iterator I = TypeNames.find(Ty);
404 if (I != TypeNames.end()) return Out << I->second << " " << VariableName;
405
406 // Otherwise we have a type that has not been named but is a derived type.
407 // Carefully recurse the type hierarchy to print out any contained symbolic
408 // names.
409 //
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000410 string TypeNameVar, tempstr = "";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000411 TypeNameVar = calcTypeNameVar(Ty, TypeNames, VariableName, tempstr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000412 return Out << TypeNameVar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000413}
414
415// Internal guts of printing a type name
416static ostream &printTypeInt(ostream &Out, const Type *Ty,
417 map<const Type *, string> &TypeNames) {
418 // Primitive types always print out their description, regardless of whether
419 // they have been named or not.
420
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000421 if (Ty->isPrimitiveType())
422 switch (Ty->getPrimitiveID()) {
423 case Type::BoolTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000424 return Out << "bool";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000425 case Type::UByteTyID:
426 return Out << "unsigned char";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000427 case Type::SByteTyID:
428 return Out << "signed char";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000429 case Type::UShortTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000430 return Out << "unsigned short";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000431 case Type::ULongTyID:
432 return Out << "unsigned long long";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000433 case Type::LongTyID:
434 return Out << "signed long long";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000435 case Type::UIntTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000436 return Out << "unsigned";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000437 default :
438 return Out << Ty->getDescription();
439 }
440
441 // Check to see if the type is named.
442 map<const Type *, string>::iterator I = TypeNames.find(Ty);
443 if (I != TypeNames.end()) return Out << I->second;
444
445 // Otherwise we have a type that has not been named but is a derived type.
446 // Carefully recurse the type hierarchy to print out any contained symbolic
447 // names.
448 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000449 string MInfo;
450 string TypeName = calcTypeName(Ty, TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000451 // TypeNames.insert(std::make_pair(Ty, TypeName));
452 //Cache type name for later use
453 if (MInfo != "")
454 return Out << TypeName << ")" << MInfo;
455 else
456 return Out << TypeName;
457}
458
459namespace {
460
461 //Internal CWriter class mimics AssemblyWriter.
462 class CWriter {
463 ostream& Out;
464 SlotCalculator &Table;
465 const Module *TheModule;
466 map<const Type *, string> TypeNames;
467 public:
468 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
469 : Out(o), Table(Tab), TheModule(M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000470 }
471
Chris Lattner16c7bb22002-05-09 02:28:59 +0000472 inline void write(const Module *M) { printModule(M); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000473
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000474 ostream& printTypeVar(const Type *Ty, const string &VariableName) {
475 return printTypeVarInt(Out, Ty, TypeNames, VariableName);
476 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000477
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000478
479
480 ostream& printType(const Type *Ty, ostream &Out);
481 void writeOperand(const Value *Operand, ostream &Out,bool PrintName = true);
482
483 string getValueName(const Value *V);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000484 private :
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000485
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000486 void printModule(const Module *M);
487 void printSymbolTable(const SymbolTable &ST);
488 void printConstant(const Constant *CPV);
489 void printGlobal(const GlobalVariable *GV);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000490 void printFunctionSignature(const Function *F);
491 void printFunctionDecl(const Function *F); // Print just the forward decl
492 void printFunctionArgument(const Argument *FA);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000493
494 void printFunction(const Function *);
495
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000496 void outputBasicBlock(const BasicBlock *);
497 };
498 /* END class CWriter */
499
500
501 /* CLASS InstLocalVarsVisitor */
502 class InstLocalVarsVisitor : public InstVisitor<InstLocalVarsVisitor> {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000503 CWriter& CW;
Chris Lattner16c7bb22002-05-09 02:28:59 +0000504 void handleTerminator(TerminatorInst *tI, int indx);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000505 public:
506 CLocalVars CLV;
507
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000508 InstLocalVarsVisitor(CWriter &cw) : CW(cw) {}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000509
510 void visitInstruction(Instruction *I) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000511 if (I->getType() != Type::VoidTy) {
512 string tempostr = CW.getValueName(I);
513 CLV.addLocalVar(I->getType(), tempostr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000514 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000515 }
516
517 void visitBranchInst(BranchInst *I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000518 handleTerminator(I, 0);
519 if (I->isConditional())
520 handleTerminator(I, 1);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000521 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000522 };
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000523}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000524
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000525void InstLocalVarsVisitor::handleTerminator(TerminatorInst *tI,int indx) {
526 BasicBlock *bb = tI->getSuccessor(indx);
527
528 BasicBlock::const_iterator insIt = bb->begin();
529 while (insIt != bb->end()) {
530 if (const PHINode *pI = dyn_cast<PHINode>(*insIt)) {
531 // Its a phinode!
532 // Calculate the incoming index for this
533 assert(pI->getBasicBlockIndex(tI->getParent()) != -1);
534
535 CLV.addLocalVar(pI->getType(), CW.getValueName(pI));
536 } else
537 break;
538 insIt++;
539 }
540}
541
542namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000543 /* CLASS CInstPrintVisitor */
544
545 class CInstPrintVisitor: public InstVisitor<CInstPrintVisitor> {
546 CWriter& CW;
547 SlotCalculator& Table;
548 ostream &Out;
549 const Value *Operand;
550
551 void outputLValue(Instruction *);
552 void printPhiFromNextBlock(TerminatorInst *tI, int indx);
Chris Lattner44408262002-05-09 03:50:42 +0000553 void printIndexingExpr(MemAccessInst *MAI);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000554
555 public:
556 CInstPrintVisitor (CWriter &cw, SlotCalculator& table, ostream& o)
Chris Lattner44408262002-05-09 03:50:42 +0000557 : CW(cw), Table(table), Out(o) {}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000558
559 void visitCastInst(CastInst *I);
560 void visitCallInst(CallInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000561 void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000562 void visitReturnInst(ReturnInst *I);
563 void visitBranchInst(BranchInst *I);
564 void visitSwitchInst(SwitchInst *I);
565 void visitInvokeInst(InvokeInst *I) ;
566 void visitMallocInst(MallocInst *I);
567 void visitAllocaInst(AllocaInst *I);
568 void visitFreeInst(FreeInst *I);
569 void visitLoadInst(LoadInst *I);
570 void visitStoreInst(StoreInst *I);
571 void visitGetElementPtrInst(GetElementPtrInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000572 void visitPHINode(PHINode *I) {}
573
574 void visitNot(GenericUnaryInst *I);
575 void visitBinaryOperator(Instruction *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000576 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000577}
578
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000579void CInstPrintVisitor::outputLValue(Instruction *I) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000580 Out << " " << CW.getValueName(I) << " = ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000581}
582
583void CInstPrintVisitor::printPhiFromNextBlock(TerminatorInst *tI, int indx) {
584 BasicBlock *bb = tI->getSuccessor(indx);
585 BasicBlock::const_iterator insIt = bb->begin();
586 while (insIt != bb->end()) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000587 if (PHINode *pI = dyn_cast<PHINode>(*insIt)) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000588 //Its a phinode!
589 //Calculate the incoming index for this
590 int incindex = pI->getBasicBlockIndex(tI->getParent());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000591 if (incindex != -1) {
592 //now we have to do the printing
593 outputLValue(pI);
594 CW.writeOperand(pI->getIncomingValue(incindex), Out);
595 Out << ";\n";
596 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000597 }
598 else break;
599 insIt++;
600 }
601}
602
603// Implement all "other" instructions, except for PHINode
604void CInstPrintVisitor::visitCastInst(CastInst *I) {
605 outputLValue(I);
606 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
607 Out << "(";
608 CW.printType(I->getType(), Out);
609 Out << ")";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000610 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000611 Out << ";\n";
612}
613
614void CInstPrintVisitor::visitCallInst(CallInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000615 if (I->getType() != Type::VoidTy)
616 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000617 else
618 Out << " ";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000619
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000620 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
621 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
622 const FunctionType *MTy = PTy
623 ? dyn_cast<FunctionType>(PTy->getElementType()):0;
624 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
625
626 // If possible, print out the short form of the call instruction, but we can
627 // only do this if the first argument is a pointer to a nonvararg method,
628 // and if the value returned is not a pointer to a method.
629 //
630 if (RetTy && !MTy->isVarArg() &&
631 (!isa<PointerType>(RetTy)||
632 !isa<FunctionType>(cast<PointerType>(RetTy)))){
633 Out << " ";
634 Out << makeNameProper(Operand->getName());
635 } else {
636 Out << makeNameProper(Operand->getName());
637 }
638 Out << "(";
639 if (I->getNumOperands() > 1)
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000640 CW.writeOperand(I->getOperand(1), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000641 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
642 Out << ",";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000643 CW.writeOperand(I->getOperand(op), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000644 }
645
646 Out << " );\n";
647}
648
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000649// Specific Instruction type classes... note that all of the casts are
650// neccesary because we use the instruction classes as opaque types...
651//
652void CInstPrintVisitor::visitReturnInst(ReturnInst *I) {
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000653 Out << " return ";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000654 if (I->getNumOperands())
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000655 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000656 Out << ";\n";
657}
658
659void CInstPrintVisitor::visitBranchInst(BranchInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000660 TerminatorInst *tI = cast<TerminatorInst>(I);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000661 if (I->isConditional()) {
662 Out << " if (";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000663 CW.writeOperand(I->getCondition(), Out);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000664 Out << ") {\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000665 printPhiFromNextBlock(tI,0);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000666 Out << " goto ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000667 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000668 Out << ";\n";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000669 Out << " } else {\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000670 printPhiFromNextBlock(tI,1);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000671 Out << " goto ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000672 CW.writeOperand(I->getOperand(1), Out);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000673 Out << ";\n }\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000674 } else {
675 printPhiFromNextBlock(tI,0);
676 Out << " goto ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000677 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000678 Out << ";\n";
679 }
680 Out << "\n";
681}
682
683void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000684 assert(0 && "Switch not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000685}
686
687void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000688 assert(0 && "Invoke not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000689}
690
691void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
692 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000693 Out << "(";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000694 CW.printType(I->getType()->getElementType(), Out);
695 Out << "*)malloc(sizeof(";
696 CW.printTypeVar(I->getType()->getElementType(), "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000697 Out << ")";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000698
699 if (I->isArrayAllocation()) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000700 Out << " * " ;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000701 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000702 }
703 Out << ");";
704}
705
706void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
707 outputLValue(I);
708 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
709 string tempstr = "";
710 Out << "(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000711 CW.printTypeVar(I->getType(), tempstr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000712 Out << ") alloca(sizeof(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000713 CW.printTypeVar(cast<PointerType>(I->getType())->getElementType(),
714 tempstr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000715 Out << ")";
716 if (I->getNumOperands()) {
717 Out << " * " ;
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000718 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000719 }
720 Out << ");\n";
721}
722
723void CInstPrintVisitor::visitFreeInst(FreeInst *I) {
724 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
725 Out << "free(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000726 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000727 Out << ");\n";
728}
729
Chris Lattner44408262002-05-09 03:50:42 +0000730void CInstPrintVisitor::printIndexingExpr(MemAccessInst *MAI) {
731 CW.writeOperand(MAI->getPointerOperand(), Out);
732
733 for (MemAccessInst::op_iterator I = MAI->idx_begin(), E = MAI->idx_end();
734 I != E; ++I)
735 if ((*I)->getType() == Type::UIntTy) {
736 Out << "[";
737 CW.writeOperand(*I, Out);
738 Out << "]";
739 } else {
740 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000741 }
Chris Lattner44408262002-05-09 03:50:42 +0000742}
743
744void CInstPrintVisitor::visitLoadInst(LoadInst *I) {
745 outputLValue(I);
746 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000747 Out << ";\n";
748}
749
Chris Lattner44408262002-05-09 03:50:42 +0000750void CInstPrintVisitor::visitStoreInst(StoreInst *I) {
751 Out << " ";
752 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000753 Out << " = ";
Chris Lattner44408262002-05-09 03:50:42 +0000754 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000755 Out << ";\n";
756}
757
758void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) {
759 outputLValue(I);
Chris Lattner44408262002-05-09 03:50:42 +0000760 Out << "&";
761 printIndexingExpr(I);
762 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000763}
764
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000765void CInstPrintVisitor::visitNot(GenericUnaryInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000766 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000767 Out << "~";
768 CW.writeOperand(I->getOperand(0), Out);
769 Out << ";\n";
770}
771
772void CInstPrintVisitor::visitBinaryOperator(Instruction *I) {
773 // binary instructions, shift instructions, setCond instructions.
774 outputLValue(I);
775 if (isa<PointerType>(I->getType())) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000776 Out << "(";
777 CW.printType(I->getType(), Out);
778 Out << ")";
779 }
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000780
781 if (isa<PointerType>(I->getType())) Out << "(long long)";
782 CW.writeOperand(I->getOperand(0), Out);
783
784 switch (I->getOpcode()) {
785 case Instruction::Add: Out << "+"; break;
786 case Instruction::Sub: Out << "-"; break;
787 case Instruction::Mul: Out << "*"; break;
788 case Instruction::Div: Out << "/"; break;
789 case Instruction::Rem: Out << "%"; break;
790 case Instruction::And: Out << "&"; break;
791 case Instruction::Or: Out << "|"; break;
792 case Instruction::Xor: Out << "^"; break;
793 case Instruction::SetEQ: Out << "=="; break;
794 case Instruction::SetNE: Out << "!="; break;
795 case Instruction::SetLE: Out << "<="; break;
796 case Instruction::SetGE: Out << ">="; break;
797 case Instruction::SetLT: Out << "<"; break;
798 case Instruction::SetGT: Out << ">"; break;
799 case Instruction::Shl : Out << "<<"; break;
800 case Instruction::Shr : Out << ">>"; break;
801 default: cerr << "Invalid operator type!" << I; abort();
802 }
803
804 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000805 CW.writeOperand(I->getOperand(1), Out);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000806 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000807}
808
809/* END : CInstPrintVisitor implementation */
810
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000811string CWriter::getValueName(const Value *V) {
812 if (V->hasName()) // Print out the label if it exists...
Chris Lattner44408262002-05-09 03:50:42 +0000813 return "l_" + makeNameProper(V->getName()) + "_" +
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000814 utostr(V->getType()->getUniqueID());
815
816 int Slot = Table.getValSlot(V);
817 assert(Slot >= 0 && "Invalid value!");
Chris Lattner44408262002-05-09 03:50:42 +0000818 return "ltmp_" + itostr(Slot) + "_" +
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000819 utostr(V->getType()->getUniqueID());
820}
821
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000822void CWriter::printModule(const Module *M) {
823 // printing stdlib inclusion
824 // Out << "#include <stdlib.h>\n";
825
Chris Lattner16c7bb22002-05-09 02:28:59 +0000826 // get declaration for alloca
827 Out << "/* Provide Declarations */\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000828 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000829
830 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000831 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000832 << "typedef unsigned char bool;\n"
833
834 << "\n\n/* Global Symbols */\n";
835
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000836 // Loop over the symbol table, emitting all named constants...
837 if (M->hasSymbolTable())
838 printSymbolTable(*M->getSymbolTable());
839
Chris Lattner16c7bb22002-05-09 02:28:59 +0000840 Out << "\n\n/* Global Data */\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000841 for_each(M->gbegin(), M->gend(),
842 bind_obj(this, &CWriter::printGlobal));
843
Chris Lattner16c7bb22002-05-09 02:28:59 +0000844 // First output all the declarations of the functions as C requires Functions
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000845 // be declared before they are used.
Chris Lattner16c7bb22002-05-09 02:28:59 +0000846 //
847 Out << "\n\n/* Function Declarations */\n";
848 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunctionDecl));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000849
Chris Lattner16c7bb22002-05-09 02:28:59 +0000850 // Output all of the functions...
851 Out << "\n\n/* Function Bodies */\n";
852 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000853}
854
855// prints the global constants
856void CWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000857 string tempostr = getValueName(GV);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000858 if (GV->hasInternalLinkage()) Out << "static ";
859
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000860 printTypeVar(GV->getType()->getElementType(), tempostr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000861
862 if (GV->hasInitializer()) {
863 Out << " = " ;
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000864 writeOperand(GV->getInitializer(), Out, false);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000865 }
866
867 Out << ";\n";
868}
869
870// printSymbolTable - Run through symbol table looking for named constants
871// if a named constant is found, emit it's declaration...
872// Assuming that symbol table has only types and constants.
873void CWriter::printSymbolTable(const SymbolTable &ST) {
874 // GraphT G;
875 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
876 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
877 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
878
879 // TODO
880 // Need to run through all the used types in the program
881 // FindUsedTypes &FUT = new FindUsedTypes();
882 // const std::set<const Type *> &UsedTypes = FUT.getTypes();
883 // Filter out the structures printing forward definitions for each of them
884 // and creating the dependency graph.
885 // Print forward definitions to all of them
886 // print the typedefs topologically sorted
887
888 // But for now we have
889 for (; I != End; ++I) {
890 const Value *V = I->second;
891 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
892 printConstant(CPV);
893 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
894 string tempostr;
895 string tempstr = "";
896 Out << "typedef ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000897 tempostr = "llvm__" + I->first;
Chris Lattner16c7bb22002-05-09 02:28:59 +0000898 string TypeNameVar = calcTypeNameVar(Ty, TypeNames,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000899 tempostr, tempstr);
900 Out << TypeNameVar << ";\n";
901 if (!isa<PointerType>(Ty) ||
902 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
903 TypeNames.insert(std::make_pair(Ty, "llvm__"+I->first));
904 }
905 }
906 }
907}
908
909
910// printConstant - Print out a constant pool entry...
911//
912void CWriter::printConstant(const Constant *CPV) {
913 // TODO
914 // Dinakar : Don't know what to do with unnamed constants
915 // should do something about it later.
916
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000917 string tempostr = getValueName(CPV);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000918
919 // Print out the constant type...
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000920 printTypeVar(CPV->getType(), tempostr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000921
922 Out << " = ";
923 // Write the value out now...
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000924 writeOperand(CPV, Out, false);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000925
926 Out << "\n";
927}
928
Chris Lattner16c7bb22002-05-09 02:28:59 +0000929// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000930//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000931void CWriter::printFunctionDecl(const Function *F) {
932 printFunctionSignature(F);
933 Out << ";\n";
934}
935
936void CWriter::printFunctionSignature(const Function *F) {
937 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000938
939 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000940 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000941
Chris Lattner16c7bb22002-05-09 02:28:59 +0000942 // Print out the return type and name...
943 printType(F->getReturnType(), Out);
944 Out << " " << makeNameProper(F->getName()) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000945
Chris Lattner16c7bb22002-05-09 02:28:59 +0000946 if (!F->isExternal()) {
947 for_each(F->getArgumentList().begin(), F->getArgumentList().end(),
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000948 bind_obj(this, &CWriter::printFunctionArgument));
949 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000950 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000951 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000952 FT->getParamTypes().begin(),
953 E = FT->getParamTypes().end(); I != E; ++I) {
954 if (I != FT->getParamTypes().begin()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000955 printType(*I, Out);
956 }
957 }
958
959 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000960 if (FT->isVarArg()) {
961 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000962 Out << "..."; // Output varargs portion of signature!
963 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000964 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000965}
966
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000967
968// printFunctionArgument - This member is called for every argument that
969// is passed into the method. Simply print it out
970//
971void CWriter::printFunctionArgument(const Argument *Arg) {
972 // Insert commas as we go... the first arg doesn't get a comma
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000973 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
974
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000975 // Output type...
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000976 printTypeVar(Arg->getType(), getValueName(Arg));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000977}
978
Chris Lattner16c7bb22002-05-09 02:28:59 +0000979void CWriter::printFunction(const Function *F) {
980 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000981
Chris Lattnerf34ee812002-05-09 03:12:34 +0000982 Table.incorporateFunction(F);
983
Chris Lattner16c7bb22002-05-09 02:28:59 +0000984 // Process each of the basic blocks, gather information and call the
985 // output methods on the CLocalVars and Function* objects.
986
987 // gather local variable information for each basic block
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000988 InstLocalVarsVisitor ILV(*this);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000989 ILV.visit((Function *)F);
990
991 printFunctionSignature(F);
992 Out << " {\n";
993
994 // Loop over the symbol table, emitting all named constants...
995 if (F->hasSymbolTable())
996 printSymbolTable(*F->getSymbolTable());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000997
Chris Lattner16c7bb22002-05-09 02:28:59 +0000998 // print the local variables
999 // we assume that every local variable is alloca'ed in the C code.
1000 std::map<const Type*, VarListType> &locals = ILV.CLV.LocalVars;
1001
1002 map<const Type*, VarListType>::iterator iter;
1003 for (iter = locals.begin(); iter != locals.end(); ++iter) {
1004 VarListType::iterator listiter;
1005 for (listiter = iter->second.begin(); listiter != iter->second.end();
1006 ++listiter) {
1007 Out << " ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001008 printTypeVar(iter->first, *listiter);
Chris Lattner16c7bb22002-05-09 02:28:59 +00001009 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001010 }
1011 }
Chris Lattner16c7bb22002-05-09 02:28:59 +00001012
1013 // print the basic blocks
1014 for_each(F->begin(), F->end(), bind_obj(this, &CWriter::outputBasicBlock));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001015
Chris Lattner16c7bb22002-05-09 02:28:59 +00001016 Out << "}\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +00001017 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001018}
1019
1020void CWriter::outputBasicBlock(const BasicBlock* BB) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001021 Out << getValueName(BB) << ":\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001022
1023 // Output all of the instructions in the basic block...
1024 // print the basic blocks
1025 CInstPrintVisitor CIPV(*this, Table, Out);
1026 CIPV.visit((BasicBlock *) BB);
1027}
1028
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001029// printType - Go to extreme measures to attempt to print out a short, symbolic
1030// version of a type name.
1031ostream& CWriter::printType(const Type *Ty, ostream &Out) {
1032 return printTypeInt(Out, Ty, TypeNames);
1033}
1034
1035
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001036void CWriter::writeOperand(const Value *Operand,
Chris Lattner16c7bb22002-05-09 02:28:59 +00001037 ostream &Out, bool PrintName = true) {
Chris Lattner44408262002-05-09 03:50:42 +00001038 if (isa<GlobalValue>(Operand))
1039 Out << "(&"; // Global values are references as their addresses by llvm
1040
Chris Lattner16c7bb22002-05-09 02:28:59 +00001041 if (PrintName && Operand->hasName()) {
1042 // If Operand has a name.
Chris Lattner44408262002-05-09 03:50:42 +00001043 Out << "l_" << makeNameProper(Operand->getName()) << "_" <<
Chris Lattner16c7bb22002-05-09 02:28:59 +00001044 Operand->getType()->getUniqueID();
Chris Lattner44408262002-05-09 03:50:42 +00001045 } else if (const Constant *CPV = dyn_cast<const Constant>(Operand)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +00001046 if (isa<ConstantPointerNull>(CPV))
1047 Out << "NULL";
1048 else
1049 Out << getConstStrValue(CPV);
Chris Lattner44408262002-05-09 03:50:42 +00001050 } else {
Chris Lattner16c7bb22002-05-09 02:28:59 +00001051 int Slot = Table.getValSlot(Operand);
1052 if (Slot >= 0)
Chris Lattner44408262002-05-09 03:50:42 +00001053 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
Chris Lattner16c7bb22002-05-09 02:28:59 +00001054 else if (PrintName)
1055 Out << "<badref>";
1056 }
Chris Lattner44408262002-05-09 03:50:42 +00001057
1058 if (isa<GlobalValue>(Operand))
1059 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001060}
1061
1062
1063//===----------------------------------------------------------------------===//
1064// External Interface declaration
1065//===----------------------------------------------------------------------===//
1066
Chris Lattnerf34ee812002-05-09 03:12:34 +00001067void WriteToC(const Module *M, ostream &Out) {
1068 assert(M && "You can't write a null module!!");
1069 SlotCalculator SlotTable(M, false);
1070 CWriter W(Out, SlotTable, M);
1071 W.write(M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001072 Out.flush();
1073}