blob: b268cb5273b3ccaeae8e7051d3427c975a544ad7 [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);
553
554 public:
555 CInstPrintVisitor (CWriter &cw, SlotCalculator& table, ostream& o)
556 : CW(cw), Table(table), Out(o) {
557
558 }
559
560 void visitCastInst(CastInst *I);
561 void visitCallInst(CallInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000562 void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000563 void visitReturnInst(ReturnInst *I);
564 void visitBranchInst(BranchInst *I);
565 void visitSwitchInst(SwitchInst *I);
566 void visitInvokeInst(InvokeInst *I) ;
567 void visitMallocInst(MallocInst *I);
568 void visitAllocaInst(AllocaInst *I);
569 void visitFreeInst(FreeInst *I);
570 void visitLoadInst(LoadInst *I);
571 void visitStoreInst(StoreInst *I);
572 void visitGetElementPtrInst(GetElementPtrInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000573 void visitPHINode(PHINode *I) {}
574
575 void visitNot(GenericUnaryInst *I);
576 void visitBinaryOperator(Instruction *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000577 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000578}
579
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000580void CInstPrintVisitor::outputLValue(Instruction *I) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000581 Out << " " << CW.getValueName(I) << " = ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000582}
583
584void CInstPrintVisitor::printPhiFromNextBlock(TerminatorInst *tI, int indx) {
585 BasicBlock *bb = tI->getSuccessor(indx);
586 BasicBlock::const_iterator insIt = bb->begin();
587 while (insIt != bb->end()) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000588 if (PHINode *pI = dyn_cast<PHINode>(*insIt)) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000589 //Its a phinode!
590 //Calculate the incoming index for this
591 int incindex = pI->getBasicBlockIndex(tI->getParent());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000592 if (incindex != -1) {
593 //now we have to do the printing
594 outputLValue(pI);
595 CW.writeOperand(pI->getIncomingValue(incindex), Out);
596 Out << ";\n";
597 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000598 }
599 else break;
600 insIt++;
601 }
602}
603
604// Implement all "other" instructions, except for PHINode
605void CInstPrintVisitor::visitCastInst(CastInst *I) {
606 outputLValue(I);
607 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
608 Out << "(";
609 CW.printType(I->getType(), Out);
610 Out << ")";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000611 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000612 Out << ";\n";
613}
614
615void CInstPrintVisitor::visitCallInst(CallInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000616 if (I->getType() != Type::VoidTy)
617 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000618 else
619 Out << " ";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000620
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000621 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
622 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
623 const FunctionType *MTy = PTy
624 ? dyn_cast<FunctionType>(PTy->getElementType()):0;
625 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
626
627 // If possible, print out the short form of the call instruction, but we can
628 // only do this if the first argument is a pointer to a nonvararg method,
629 // and if the value returned is not a pointer to a method.
630 //
631 if (RetTy && !MTy->isVarArg() &&
632 (!isa<PointerType>(RetTy)||
633 !isa<FunctionType>(cast<PointerType>(RetTy)))){
634 Out << " ";
635 Out << makeNameProper(Operand->getName());
636 } else {
637 Out << makeNameProper(Operand->getName());
638 }
639 Out << "(";
640 if (I->getNumOperands() > 1)
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000641 CW.writeOperand(I->getOperand(1), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000642 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
643 Out << ",";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000644 CW.writeOperand(I->getOperand(op), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000645 }
646
647 Out << " );\n";
648}
649
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000650// Specific Instruction type classes... note that all of the casts are
651// neccesary because we use the instruction classes as opaque types...
652//
653void CInstPrintVisitor::visitReturnInst(ReturnInst *I) {
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000654 Out << " return ";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000655 if (I->getNumOperands())
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000656 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000657 Out << ";\n";
658}
659
660void CInstPrintVisitor::visitBranchInst(BranchInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000661 TerminatorInst *tI = cast<TerminatorInst>(I);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000662 if (I->isConditional()) {
663 Out << " if (";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000664 CW.writeOperand(I->getCondition(), Out);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000665 Out << ") {\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000666 printPhiFromNextBlock(tI,0);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000667 Out << " goto ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000668 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000669 Out << ";\n";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000670 Out << " } else {\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000671 printPhiFromNextBlock(tI,1);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000672 Out << " goto ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000673 CW.writeOperand(I->getOperand(1), Out);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000674 Out << ";\n }\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000675 } else {
676 printPhiFromNextBlock(tI,0);
677 Out << " goto ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000678 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000679 Out << ";\n";
680 }
681 Out << "\n";
682}
683
684void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000685 assert(0 && "Switch not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000686}
687
688void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000689 assert(0 && "Invoke not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000690}
691
692void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
693 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000694 Out << "(";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000695 CW.printType(I->getType()->getElementType(), Out);
696 Out << "*)malloc(sizeof(";
697 CW.printTypeVar(I->getType()->getElementType(), "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000698 Out << ")";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000699
700 if (I->isArrayAllocation()) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000701 Out << " * " ;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000702 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000703 }
704 Out << ");";
705}
706
707void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
708 outputLValue(I);
709 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
710 string tempstr = "";
711 Out << "(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000712 CW.printTypeVar(I->getType(), tempstr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000713 Out << ") alloca(sizeof(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000714 CW.printTypeVar(cast<PointerType>(I->getType())->getElementType(),
715 tempstr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000716 Out << ")";
717 if (I->getNumOperands()) {
718 Out << " * " ;
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000719 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000720 }
721 Out << ");\n";
722}
723
724void CInstPrintVisitor::visitFreeInst(FreeInst *I) {
725 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
726 Out << "free(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000727 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000728 Out << ");\n";
729}
730
731void CInstPrintVisitor::visitLoadInst(LoadInst *I) {
732 outputLValue(I);
733 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
734 if (I->getNumOperands() <= 1) {
735 Out << "*";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000736 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000737 }
738 else {
739 //Check if it is an array type or struct type ptr!
740 int arrtype = 1;
741 const PointerType *PTy = dyn_cast<PointerType>(I->getType());
742 if (cast<const PointerType>(Operand->getType())->getElementType()->getPrimitiveID() == Type::StructTyID)
743 arrtype = 0;
744 if (arrtype && isa<GlobalValue>(Operand))
745 Out << "(&";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000746 CW.writeOperand(Operand,Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000747 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
748 if (i == 1) {
749 if (arrtype || !isa<GlobalValue>(Operand)) {
750 Out << "[";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000751 CW.writeOperand(I->getOperand(i), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000752 Out << "]";
753 }
754 if (isa<GlobalValue>(Operand) && arrtype)
755 Out << ")";
756 }
757 else {
758 if (arrtype == 1) Out << "[";
759 else
760 Out << ".field";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000761 CW.writeOperand(I->getOperand(i), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000762 if (arrtype == 1) Out << "]";
763 }
764 }
765 }
766 Out << ";\n";
767}
768
769void CInstPrintVisitor::visitStoreInst(StoreInst *I) {
770 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
771 if (I->getNumOperands() <= 2) {
772 Out << "*";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000773 CW.writeOperand(I->getOperand(1), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000774 }
775 else {
776 //Check if it is an array type or struct type ptr!
777 int arrtype = 1;
778 if (cast<const PointerType>(I->getOperand(1)->getType())->getElementType()->getPrimitiveID() == Type::StructTyID)
779 arrtype = 0;
780 if (isa<GlobalValue>(I->getOperand(1)) && arrtype)
781 Out << "(&";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000782 CW.writeOperand(I->getOperand(1), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000783 for (unsigned i = 2, E = I->getNumOperands(); i != E; ++i) {
784 if (i == 2) {
785 if (arrtype || !isa<GlobalValue>(I->getOperand(1))) {
786 Out << "[";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000787 CW.writeOperand(I->getOperand(i), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000788 Out << "]";
789 }
790 if (isa<GlobalValue>(I->getOperand(1)) && arrtype)
791 Out << ")";
792 }
793 else {
794 if (arrtype == 1) Out << "[";
795 else
796 Out << ".field";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000797 CW.writeOperand(I->getOperand(i), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000798 if (arrtype == 1) Out << "]";
799 }
800 }
801 }
802 Out << " = ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000803 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000804 Out << ";\n";
805}
806
807void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) {
808 outputLValue(I);
809 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
810 Out << " &(";
811 if (I->getNumOperands() <= 1)
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000812 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000813 else {
814 //Check if it is an array type or struct type ptr!
815 int arrtype = 1;
816 if ((cast<const PointerType>(Operand->getType()))->getElementType()->getPrimitiveID() == Type::StructTyID)
817 arrtype = 0;
818 if ((isa<GlobalValue>(Operand)) && arrtype)
819 Out << "(&";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000820 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000821 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
822 if (i == 1) {
823 if (arrtype || !isa<GlobalValue>(Operand)){
824 Out << "[";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000825 CW.writeOperand(I->getOperand(i), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000826 Out << "]";
827 }
828 if (isa<GlobalValue>(Operand) && arrtype)
829 Out << ")";
830 }
831 else {
832 if (arrtype == 1) Out << "[";
833 else
834 Out << ".field";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000835 CW.writeOperand(I->getOperand(i), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000836 if (arrtype == 1) Out << "]";
837 }
838 }
839 }
840 Out << ");\n";
841}
842
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000843void CInstPrintVisitor::visitNot(GenericUnaryInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000844 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000845 Out << "~";
846 CW.writeOperand(I->getOperand(0), Out);
847 Out << ";\n";
848}
849
850void CInstPrintVisitor::visitBinaryOperator(Instruction *I) {
851 // binary instructions, shift instructions, setCond instructions.
852 outputLValue(I);
853 if (isa<PointerType>(I->getType())) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000854 Out << "(";
855 CW.printType(I->getType(), Out);
856 Out << ")";
857 }
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000858
859 if (isa<PointerType>(I->getType())) Out << "(long long)";
860 CW.writeOperand(I->getOperand(0), Out);
861
862 switch (I->getOpcode()) {
863 case Instruction::Add: Out << "+"; break;
864 case Instruction::Sub: Out << "-"; break;
865 case Instruction::Mul: Out << "*"; break;
866 case Instruction::Div: Out << "/"; break;
867 case Instruction::Rem: Out << "%"; break;
868 case Instruction::And: Out << "&"; break;
869 case Instruction::Or: Out << "|"; break;
870 case Instruction::Xor: Out << "^"; break;
871 case Instruction::SetEQ: Out << "=="; break;
872 case Instruction::SetNE: Out << "!="; break;
873 case Instruction::SetLE: Out << "<="; break;
874 case Instruction::SetGE: Out << ">="; break;
875 case Instruction::SetLT: Out << "<"; break;
876 case Instruction::SetGT: Out << ">"; break;
877 case Instruction::Shl : Out << "<<"; break;
878 case Instruction::Shr : Out << ">>"; break;
879 default: cerr << "Invalid operator type!" << I; abort();
880 }
881
882 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000883 CW.writeOperand(I->getOperand(1), Out);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000884 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000885}
886
887/* END : CInstPrintVisitor implementation */
888
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000889string CWriter::getValueName(const Value *V) {
890 if (V->hasName()) // Print out the label if it exists...
891 return "llvm__" + makeNameProper(V->getName()) + "_" +
892 utostr(V->getType()->getUniqueID());
893
894 int Slot = Table.getValSlot(V);
895 assert(Slot >= 0 && "Invalid value!");
896 return "llvm__tmp_" + itostr(Slot) + "_" +
897 utostr(V->getType()->getUniqueID());
898}
899
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000900void CWriter::printModule(const Module *M) {
901 // printing stdlib inclusion
902 // Out << "#include <stdlib.h>\n";
903
Chris Lattner16c7bb22002-05-09 02:28:59 +0000904 // get declaration for alloca
905 Out << "/* Provide Declarations */\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000906 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000907
908 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000909 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000910 << "typedef unsigned char bool;\n"
911
912 << "\n\n/* Global Symbols */\n";
913
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000914 // Loop over the symbol table, emitting all named constants...
915 if (M->hasSymbolTable())
916 printSymbolTable(*M->getSymbolTable());
917
Chris Lattner16c7bb22002-05-09 02:28:59 +0000918 Out << "\n\n/* Global Data */\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000919 for_each(M->gbegin(), M->gend(),
920 bind_obj(this, &CWriter::printGlobal));
921
Chris Lattner16c7bb22002-05-09 02:28:59 +0000922 // First output all the declarations of the functions as C requires Functions
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000923 // be declared before they are used.
Chris Lattner16c7bb22002-05-09 02:28:59 +0000924 //
925 Out << "\n\n/* Function Declarations */\n";
926 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunctionDecl));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000927
Chris Lattner16c7bb22002-05-09 02:28:59 +0000928 // Output all of the functions...
929 Out << "\n\n/* Function Bodies */\n";
930 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000931}
932
933// prints the global constants
934void CWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000935 string tempostr = getValueName(GV);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000936 if (GV->hasInternalLinkage()) Out << "static ";
937
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000938 printTypeVar(GV->getType()->getElementType(), tempostr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000939
940 if (GV->hasInitializer()) {
941 Out << " = " ;
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000942 writeOperand(GV->getInitializer(), Out, false);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000943 }
944
945 Out << ";\n";
946}
947
948// printSymbolTable - Run through symbol table looking for named constants
949// if a named constant is found, emit it's declaration...
950// Assuming that symbol table has only types and constants.
951void CWriter::printSymbolTable(const SymbolTable &ST) {
952 // GraphT G;
953 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
954 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
955 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
956
957 // TODO
958 // Need to run through all the used types in the program
959 // FindUsedTypes &FUT = new FindUsedTypes();
960 // const std::set<const Type *> &UsedTypes = FUT.getTypes();
961 // Filter out the structures printing forward definitions for each of them
962 // and creating the dependency graph.
963 // Print forward definitions to all of them
964 // print the typedefs topologically sorted
965
966 // But for now we have
967 for (; I != End; ++I) {
968 const Value *V = I->second;
969 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
970 printConstant(CPV);
971 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
972 string tempostr;
973 string tempstr = "";
974 Out << "typedef ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000975 tempostr = "llvm__" + I->first;
Chris Lattner16c7bb22002-05-09 02:28:59 +0000976 string TypeNameVar = calcTypeNameVar(Ty, TypeNames,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000977 tempostr, tempstr);
978 Out << TypeNameVar << ";\n";
979 if (!isa<PointerType>(Ty) ||
980 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
981 TypeNames.insert(std::make_pair(Ty, "llvm__"+I->first));
982 }
983 }
984 }
985}
986
987
988// printConstant - Print out a constant pool entry...
989//
990void CWriter::printConstant(const Constant *CPV) {
991 // TODO
992 // Dinakar : Don't know what to do with unnamed constants
993 // should do something about it later.
994
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000995 string tempostr = getValueName(CPV);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000996
997 // Print out the constant type...
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000998 printTypeVar(CPV->getType(), tempostr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000999
1000 Out << " = ";
1001 // Write the value out now...
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001002 writeOperand(CPV, Out, false);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001003
1004 Out << "\n";
1005}
1006
Chris Lattner16c7bb22002-05-09 02:28:59 +00001007// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001008//
Chris Lattner16c7bb22002-05-09 02:28:59 +00001009void CWriter::printFunctionDecl(const Function *F) {
1010 printFunctionSignature(F);
1011 Out << ";\n";
1012}
1013
1014void CWriter::printFunctionSignature(const Function *F) {
1015 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001016
1017 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +00001018 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001019
Chris Lattner16c7bb22002-05-09 02:28:59 +00001020 // Print out the return type and name...
1021 printType(F->getReturnType(), Out);
1022 Out << " " << makeNameProper(F->getName()) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001023
Chris Lattner16c7bb22002-05-09 02:28:59 +00001024 if (!F->isExternal()) {
1025 for_each(F->getArgumentList().begin(), F->getArgumentList().end(),
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001026 bind_obj(this, &CWriter::printFunctionArgument));
1027 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001028 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001029 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +00001030 FT->getParamTypes().begin(),
1031 E = FT->getParamTypes().end(); I != E; ++I) {
1032 if (I != FT->getParamTypes().begin()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001033 printType(*I, Out);
1034 }
1035 }
1036
1037 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +00001038 if (FT->isVarArg()) {
1039 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001040 Out << "..."; // Output varargs portion of signature!
1041 }
Chris Lattner16c7bb22002-05-09 02:28:59 +00001042 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001043}
1044
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001045
1046// printFunctionArgument - This member is called for every argument that
1047// is passed into the method. Simply print it out
1048//
1049void CWriter::printFunctionArgument(const Argument *Arg) {
1050 // Insert commas as we go... the first arg doesn't get a comma
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001051 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
1052
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001053 // Output type...
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001054 printTypeVar(Arg->getType(), getValueName(Arg));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001055}
1056
Chris Lattner16c7bb22002-05-09 02:28:59 +00001057void CWriter::printFunction(const Function *F) {
1058 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001059
Chris Lattnerf34ee812002-05-09 03:12:34 +00001060 Table.incorporateFunction(F);
1061
Chris Lattner16c7bb22002-05-09 02:28:59 +00001062 // Process each of the basic blocks, gather information and call the
1063 // output methods on the CLocalVars and Function* objects.
1064
1065 // gather local variable information for each basic block
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001066 InstLocalVarsVisitor ILV(*this);
Chris Lattner16c7bb22002-05-09 02:28:59 +00001067 ILV.visit((Function *)F);
1068
1069 printFunctionSignature(F);
1070 Out << " {\n";
1071
1072 // Loop over the symbol table, emitting all named constants...
1073 if (F->hasSymbolTable())
1074 printSymbolTable(*F->getSymbolTable());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001075
Chris Lattner16c7bb22002-05-09 02:28:59 +00001076 // print the local variables
1077 // we assume that every local variable is alloca'ed in the C code.
1078 std::map<const Type*, VarListType> &locals = ILV.CLV.LocalVars;
1079
1080 map<const Type*, VarListType>::iterator iter;
1081 for (iter = locals.begin(); iter != locals.end(); ++iter) {
1082 VarListType::iterator listiter;
1083 for (listiter = iter->second.begin(); listiter != iter->second.end();
1084 ++listiter) {
1085 Out << " ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001086 printTypeVar(iter->first, *listiter);
Chris Lattner16c7bb22002-05-09 02:28:59 +00001087 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001088 }
1089 }
Chris Lattner16c7bb22002-05-09 02:28:59 +00001090
1091 // print the basic blocks
1092 for_each(F->begin(), F->end(), bind_obj(this, &CWriter::outputBasicBlock));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001093
Chris Lattner16c7bb22002-05-09 02:28:59 +00001094 Out << "}\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +00001095 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001096}
1097
1098void CWriter::outputBasicBlock(const BasicBlock* BB) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001099 Out << getValueName(BB) << ":\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001100
1101 // Output all of the instructions in the basic block...
1102 // print the basic blocks
1103 CInstPrintVisitor CIPV(*this, Table, Out);
1104 CIPV.visit((BasicBlock *) BB);
1105}
1106
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001107// printType - Go to extreme measures to attempt to print out a short, symbolic
1108// version of a type name.
1109ostream& CWriter::printType(const Type *Ty, ostream &Out) {
1110 return printTypeInt(Out, Ty, TypeNames);
1111}
1112
1113
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001114void CWriter::writeOperand(const Value *Operand,
Chris Lattner16c7bb22002-05-09 02:28:59 +00001115 ostream &Out, bool PrintName = true) {
Chris Lattner16c7bb22002-05-09 02:28:59 +00001116 if (PrintName && Operand->hasName()) {
1117 // If Operand has a name.
1118 Out << "llvm__" << makeNameProper(Operand->getName()) << "_" <<
1119 Operand->getType()->getUniqueID();
1120 return;
1121 }
1122 else if (const Constant *CPV = dyn_cast<const Constant>(Operand)) {
1123 if (isa<ConstantPointerNull>(CPV))
1124 Out << "NULL";
1125 else
1126 Out << getConstStrValue(CPV);
1127 }
1128 else {
1129 int Slot = Table.getValSlot(Operand);
1130 if (Slot >= 0)
1131 Out << "llvm__tmp_" << Slot << "_" << Operand->getType()->getUniqueID();
1132 else if (PrintName)
1133 Out << "<badref>";
1134 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001135}
1136
1137
1138//===----------------------------------------------------------------------===//
1139// External Interface declaration
1140//===----------------------------------------------------------------------===//
1141
Chris Lattnerf34ee812002-05-09 03:12:34 +00001142void WriteToC(const Module *M, ostream &Out) {
1143 assert(M && "You can't write a null module!!");
1144 SlotCalculator SlotTable(M, false);
1145 CWriter W(Out, SlotTable, M);
1146 W.write(M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001147 Out.flush();
1148}