blob: 031a26efd4e3ea808b7b0663beecc01fc7104ad1 [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"
13#include "llvm/Module.h"
14#include "llvm/Argument.h"
15#include "llvm/Function.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Constants.h"
18#include "llvm/GlobalVariable.h"
19#include "llvm/BasicBlock.h"
20#include "llvm/iMemory.h"
21#include "llvm/iTerminators.h"
22#include "llvm/iPHINode.h"
23#include "llvm/iOther.h"
24#include "llvm/SymbolTable.h"
25#include "llvm/Support/InstVisitor.h"
26#include "Support/StringExtras.h"
27#include "Support/STLExtras.h"
28
29#include <algorithm>
30#include <strstream>
31using std::string;
32using std::map;
33using std::vector;
34using std::ostream;
35
Chris Lattner16c7bb22002-05-09 02:28:59 +000036//===-----------------------------------------------------------------------==//
37//
38// Implementation of the CLocalVars methods
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000039
40// Appends a variable to the LocalVars map if it does not already exist
41// Also check that the type exists on the map.
42void CLocalVars::addLocalVar(const Type *t, const string & var) {
43 if (!LocalVars.count(t) ||
44 find(LocalVars[t].begin(), LocalVars[t].end(), var)
45 == LocalVars[t].end()) {
46 LocalVars[t].push_back(var);
47 }
48}
49
Chris Lattner16c7bb22002-05-09 02:28:59 +000050static string calcTypeNameVar(const Type *Ty,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000051 map<const Type *, string> &TypeNames,
52 string VariableName, string NameSoFar);
53
54static std::string getConstStrValue(const Constant* CPV);
55
56
57//
58//Getting opcodes in terms of the operator
59//
60static const char *getOpcodeOperName(const Instruction *I) {
61 switch (I->getOpcode()) {
62 // Standard binary operators...
63 case Instruction::Add: return "+";
64 case Instruction::Sub: return "-";
65 case Instruction::Mul: return "*";
66 case Instruction::Div: return "/";
67 case Instruction::Rem: return "%";
68
69 // Logical operators...
70 case Instruction::And: return "&";
71 case Instruction::Or: return "|";
72 case Instruction::Xor: return "^";
73
74 // SetCond operators...
75 case Instruction::SetEQ: return "==";
76 case Instruction::SetNE: return "!=";
77 case Instruction::SetLE: return "<=";
78 case Instruction::SetGE: return ">=";
79 case Instruction::SetLT: return "<";
80 case Instruction::SetGT: return ">";
81
82 //ShiftInstruction...
83
84 case Instruction::Shl : return "<<";
85 case Instruction::Shr : return ">>";
86
87 default:
88 cerr << "Invalid operator type!" << I->getOpcode() << "\n";
89 abort();
90 }
91 return 0;
92}
93
94
95// We dont want identifier names with ., space, - in them.
96// So we replace them with _
97static string makeNameProper(string x) {
98 string tmp;
99 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++) {
100 if (*sI == '.')
101 tmp += '_';
102 else if (*sI == ' ')
103 tmp += '_';
104 else if (*sI == '-')
105 tmp += "__";
106 else
107 tmp += *sI;
108 }
109 return tmp;
110}
111
112static string getConstantName(const Constant *CPV) {
113 return CPV->getName();
114}
115
116
117static std::string getConstArrayStrValue(const Constant* CPV) {
118 std::string Result;
119
120 // As a special case, print the array as a string if it is an array of
121 // ubytes or an array of sbytes with positive values.
122 //
123 const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType();
124 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
125
126 if (ETy == Type::SByteTy) {
127 for (unsigned i = 0; i < CPV->getNumOperands(); ++i)
128 if (ETy == Type::SByteTy &&
129 cast<ConstantSInt>(CPV->getOperand(i))->getValue() < 0) {
130 isString = false;
131 break;
132 }
133 }
134
135 if (isString) {
136 Result = "\"";
137 for (unsigned i = 0; i < CPV->getNumOperands(); ++i) {
138 unsigned char C = (ETy == Type::SByteTy) ?
139 (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() :
140 (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue();
141
142 if (isprint(C)) {
143 Result += C;
144 } else {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000145 Result += "\\x";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000146 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
147 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
148 }
149 }
150 Result += "\"";
151
152 } else {
153 Result = "{";
154 if (CPV->getNumOperands()) {
155 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
156 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
157 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
158 }
159 Result += " }";
160 }
161
162 return Result;
163}
164
165static std::string getConstStructStrValue(const Constant* CPV) {
166 std::string Result = "{";
167 if (CPV->getNumOperands()) {
168 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
169 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
170 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
171 }
172
173 return Result + " }";
174}
175
176// our own getStrValue function for constant initializers
177static std::string getConstStrValue(const Constant* CPV) {
178 // Does not handle null pointers, that needs to be checked explicitly
179 string tempstr;
180 if (CPV == ConstantBool::False)
181 return "0";
182 else if (CPV == ConstantBool::True)
183 return "1";
184
185 else if (isa<ConstantArray>(CPV)) {
186 tempstr = getConstArrayStrValue(CPV);
187 }
188 else if (isa<ConstantStruct>(CPV)) {
189 tempstr = getConstStructStrValue(CPV);
190 }
191 else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CPV)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000192 tempstr = utostr(CUI->getValue());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000193 }
194 else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CPV)) {
195 tempstr = itostr(CSI->getValue());
196 }
197 else if (ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) {
198 tempstr = ftostr(CFP->getValue());
199 }
200
201 if (CPV->getType() == Type::ULongTy)
202 tempstr += "ull";
203 else if (CPV->getType() == Type::LongTy)
204 tempstr += "ll";
205 else if (CPV->getType() == Type::UIntTy ||
206 CPV->getType() == Type::UShortTy)
207 tempstr += "u";
208
209 return tempstr;
210
211}
212
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000213// Internal function
214// Essentially pass the Type* variable, an empty typestack and this prints
215// out the C type
Chris Lattner16c7bb22002-05-09 02:28:59 +0000216static string calcTypeName(const Type *Ty, map<const Type *, string> &TypeNames,
217 string &FunctionInfo) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000218
219 // Takin' care of the fact that boolean would be int in C
220 // and that ushort would be unsigned short etc.
221
222 // Base Case
223 if (Ty->isPrimitiveType())
224 switch (Ty->getPrimitiveID()) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000225 case Type::VoidTyID: return "void";
226 case Type::BoolTyID: return "bool";
227 case Type::UByteTyID: return "unsigned char";
228 case Type::SByteTyID: return "signed char";
229 case Type::UShortTyID: return "unsigned short";
230 case Type::ShortTyID: return "short";
231 case Type::UIntTyID: return "unsigned";
232 case Type::IntTyID: return "int";
233 case Type::ULongTyID: return "unsigned long long";
234 case Type::LongTyID: return "signed long long";
235 case Type::FloatTyID: return "float";
236 case Type::DoubleTyID: return "double";
237 default : assert(0 && "Unknown primitive type!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000238 }
239
240 // Check to see if the type is named.
241 map<const Type *, string>::iterator I = TypeNames.find(Ty);
242 if (I != TypeNames.end())
243 return I->second;
244
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000245 string Result;
246 string MInfo = "";
247 switch (Ty->getPrimitiveID()) {
248 case Type::FunctionTyID: {
249 const FunctionType *MTy = cast<const FunctionType>(Ty);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000250 Result = calcTypeName(MTy->getReturnType(), TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000251 if (MInfo != "")
252 Result += ") " + MInfo;
253 Result += "(";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000254 FunctionInfo += " (";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000255 for (FunctionType::ParamTypes::const_iterator
256 I = MTy->getParamTypes().begin(),
257 E = MTy->getParamTypes().end(); I != E; ++I) {
258 if (I != MTy->getParamTypes().begin())
Chris Lattner16c7bb22002-05-09 02:28:59 +0000259 FunctionInfo += ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000260 MInfo = "";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000261 FunctionInfo += calcTypeName(*I, TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000262 if (MInfo != "")
263 Result += ") " + MInfo;
264 }
265 if (MTy->isVarArg()) {
266 if (!MTy->getParamTypes().empty())
Chris Lattner16c7bb22002-05-09 02:28:59 +0000267 FunctionInfo += ", ";
268 FunctionInfo += "...";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000269 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000270 FunctionInfo += ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000271 break;
272 }
273 case Type::StructTyID: {
274 string tempstr = "";
275 const StructType *STy = cast<const StructType>(Ty);
276 Result = " struct {\n ";
277 int indx = 0;
278 for (StructType::ElementTypes::const_iterator
279 I = STy->getElementTypes().begin(),
280 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000281 Result += calcTypeNameVar(*I, TypeNames,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000282 "field" + itostr(indx++), tempstr);
283 Result += ";\n ";
284 }
285 Result += " } ";
286 break;
287 }
288 case Type::PointerTyID:
289 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner16c7bb22002-05-09 02:28:59 +0000290 TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000291 Result += "*";
292 break;
293 case Type::ArrayTyID: {
294 const ArrayType *ATy = cast<const ArrayType>(Ty);
295 int NumElements = ATy->getNumElements();
Chris Lattner16c7bb22002-05-09 02:28:59 +0000296 Result = calcTypeName(ATy->getElementType(), TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000297 Result += "*";
298 break;
299 }
300 default:
301 assert(0 && "Unhandled case in getTypeProps!");
302 Result = "<error>";
303 }
304
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000305 return Result;
306}
307
308// Internal function
309// Pass the Type* variable and and the variable name and this prints out the
310// variable declaration.
311// This is different from calcTypeName because if you need to declare an array
312// the size of the array would appear after the variable name itself
313// For eg. int a[10];
Chris Lattner16c7bb22002-05-09 02:28:59 +0000314static string calcTypeNameVar(const Type *Ty,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000315 map<const Type *, string> &TypeNames,
316 string VariableName, string NameSoFar) {
317 if (Ty->isPrimitiveType())
318 switch (Ty->getPrimitiveID()) {
319 case Type::BoolTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000320 return "bool " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000321 case Type::UByteTyID:
322 return "unsigned char " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000323 case Type::SByteTyID:
324 return "signed char " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000325 case Type::UShortTyID:
326 return "unsigned long long " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000327 case Type::ULongTyID:
328 return "unsigned long long " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000329 case Type::LongTyID:
330 return "signed long long " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000331 case Type::UIntTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000332 return "unsigned " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000333 default :
334 return Ty->getDescription() + " " + NameSoFar + VariableName;
335 }
336
337 // Check to see if the type is named.
338 map<const Type *, string>::iterator I = TypeNames.find(Ty);
339 if (I != TypeNames.end())
340 return I->second + " " + NameSoFar + VariableName;
341
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000342 string Result;
343 string tempstr = "";
344
345 switch (Ty->getPrimitiveID()) {
346 case Type::FunctionTyID: {
347 string MInfo = "";
348 const FunctionType *MTy = cast<const FunctionType>(Ty);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000349 Result += calcTypeName(MTy->getReturnType(), TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000350 if (MInfo != "")
351 Result += ") " + MInfo;
352 Result += " " + NameSoFar + VariableName;
353 Result += " (";
354 for (FunctionType::ParamTypes::const_iterator
355 I = MTy->getParamTypes().begin(),
356 E = MTy->getParamTypes().end(); I != E; ++I) {
357 if (I != MTy->getParamTypes().begin())
358 Result += ", ";
359 MInfo = "";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000360 Result += calcTypeName(*I, TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000361 if (MInfo != "")
362 Result += ") " + MInfo;
363 }
364 if (MTy->isVarArg()) {
365 if (!MTy->getParamTypes().empty())
366 Result += ", ";
367 Result += "...";
368 }
369 Result += ")";
370 break;
371 }
372 case Type::StructTyID: {
373 const StructType *STy = cast<const StructType>(Ty);
374 Result = " struct {\n ";
375 int indx = 0;
376 for (StructType::ElementTypes::const_iterator
377 I = STy->getElementTypes().begin(),
378 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000379 Result += calcTypeNameVar(*I, TypeNames,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000380 "field" + itostr(indx++), "");
381 Result += ";\n ";
382 }
383 Result += " }";
384 Result += " " + NameSoFar + VariableName;
385 break;
386 }
387
388 case Type::PointerTyID: {
389 Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner16c7bb22002-05-09 02:28:59 +0000390 TypeNames, tempstr,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000391 "(*" + NameSoFar + VariableName + ")");
392 break;
393 }
394
395 case Type::ArrayTyID: {
396 const ArrayType *ATy = cast<const ArrayType>(Ty);
397 int NumElements = ATy->getNumElements();
Chris Lattner16c7bb22002-05-09 02:28:59 +0000398 Result = calcTypeNameVar(ATy->getElementType(), TypeNames,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000399 tempstr, NameSoFar + VariableName + "[" +
400 itostr(NumElements) + "]");
401 break;
402 }
403 default:
404 assert(0 && "Unhandled case in getTypeProps!");
405 Result = "<error>";
406 }
407
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000408 return Result;
409}
410
411// printTypeVarInt - The internal guts of printing out a type that has a
412// potentially named portion and the variable associated with the type.
413static ostream &printTypeVarInt(ostream &Out, const Type *Ty,
414 map<const Type *, string> &TypeNames,
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000415 const string &VariableName) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000416 // Primitive types always print out their description, regardless of whether
417 // they have been named or not.
418
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000419 if (Ty->isPrimitiveType())
420 switch (Ty->getPrimitiveID()) {
421 case Type::BoolTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000422 return Out << "bool " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000423 case Type::UByteTyID:
424 return Out << "unsigned char " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000425 case Type::SByteTyID:
426 return Out << "signed char " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000427 case Type::UShortTyID:
428 return Out << "unsigned long long " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000429 case Type::ULongTyID:
430 return Out << "unsigned long long " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000431 case Type::LongTyID:
432 return Out << "signed long long " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000433 case Type::UIntTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000434 return Out << "unsigned " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000435 default :
436 return Out << Ty->getDescription() << " " << VariableName;
437 }
438
439 // Check to see if the type is named.
440 map<const Type *, string>::iterator I = TypeNames.find(Ty);
441 if (I != TypeNames.end()) return Out << I->second << " " << VariableName;
442
443 // Otherwise we have a type that has not been named but is a derived type.
444 // Carefully recurse the type hierarchy to print out any contained symbolic
445 // names.
446 //
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000447 string TypeNameVar, tempstr = "";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000448 TypeNameVar = calcTypeNameVar(Ty, TypeNames, VariableName, tempstr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000449 return Out << TypeNameVar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000450}
451
452// Internal guts of printing a type name
453static ostream &printTypeInt(ostream &Out, const Type *Ty,
454 map<const Type *, string> &TypeNames) {
455 // Primitive types always print out their description, regardless of whether
456 // they have been named or not.
457
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000458 if (Ty->isPrimitiveType())
459 switch (Ty->getPrimitiveID()) {
460 case Type::BoolTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000461 return Out << "bool";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000462 case Type::UByteTyID:
463 return Out << "unsigned char";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000464 case Type::SByteTyID:
465 return Out << "signed char";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000466 case Type::UShortTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000467 return Out << "unsigned short";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000468 case Type::ULongTyID:
469 return Out << "unsigned long long";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000470 case Type::LongTyID:
471 return Out << "signed long long";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000472 case Type::UIntTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000473 return Out << "unsigned";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000474 default :
475 return Out << Ty->getDescription();
476 }
477
478 // Check to see if the type is named.
479 map<const Type *, string>::iterator I = TypeNames.find(Ty);
480 if (I != TypeNames.end()) return Out << I->second;
481
482 // Otherwise we have a type that has not been named but is a derived type.
483 // Carefully recurse the type hierarchy to print out any contained symbolic
484 // names.
485 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000486 string MInfo;
487 string TypeName = calcTypeName(Ty, TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000488 // TypeNames.insert(std::make_pair(Ty, TypeName));
489 //Cache type name for later use
490 if (MInfo != "")
491 return Out << TypeName << ")" << MInfo;
492 else
493 return Out << TypeName;
494}
495
496namespace {
497
498 //Internal CWriter class mimics AssemblyWriter.
499 class CWriter {
500 ostream& Out;
501 SlotCalculator &Table;
502 const Module *TheModule;
503 map<const Type *, string> TypeNames;
504 public:
505 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
506 : Out(o), Table(Tab), TheModule(M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000507 }
508
Chris Lattner16c7bb22002-05-09 02:28:59 +0000509 inline void write(const Module *M) { printModule(M); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000510
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000511 ostream& printTypeVar(const Type *Ty, const string &VariableName) {
512 return printTypeVarInt(Out, Ty, TypeNames, VariableName);
513 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000514
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000515
516
517 ostream& printType(const Type *Ty, ostream &Out);
518 void writeOperand(const Value *Operand, ostream &Out,bool PrintName = true);
519
520 string getValueName(const Value *V);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000521 private :
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000522
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000523 void printModule(const Module *M);
524 void printSymbolTable(const SymbolTable &ST);
525 void printConstant(const Constant *CPV);
526 void printGlobal(const GlobalVariable *GV);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000527 void printFunctionSignature(const Function *F);
528 void printFunctionDecl(const Function *F); // Print just the forward decl
529 void printFunctionArgument(const Argument *FA);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000530
531 void printFunction(const Function *);
532
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000533 void outputBasicBlock(const BasicBlock *);
534 };
535 /* END class CWriter */
536
537
538 /* CLASS InstLocalVarsVisitor */
539 class InstLocalVarsVisitor : public InstVisitor<InstLocalVarsVisitor> {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000540 CWriter& CW;
Chris Lattner16c7bb22002-05-09 02:28:59 +0000541 void handleTerminator(TerminatorInst *tI, int indx);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000542 public:
543 CLocalVars CLV;
544
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000545 InstLocalVarsVisitor(CWriter &cw) : CW(cw) {}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000546
547 void visitInstruction(Instruction *I) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000548 if (I->getType() != Type::VoidTy) {
549 string tempostr = CW.getValueName(I);
550 CLV.addLocalVar(I->getType(), tempostr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000551 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000552 }
553
554 void visitBranchInst(BranchInst *I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000555 handleTerminator(I, 0);
556 if (I->isConditional())
557 handleTerminator(I, 1);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000558 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000559 };
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000560}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000561
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000562void InstLocalVarsVisitor::handleTerminator(TerminatorInst *tI,int indx) {
563 BasicBlock *bb = tI->getSuccessor(indx);
564
565 BasicBlock::const_iterator insIt = bb->begin();
566 while (insIt != bb->end()) {
567 if (const PHINode *pI = dyn_cast<PHINode>(*insIt)) {
568 // Its a phinode!
569 // Calculate the incoming index for this
570 assert(pI->getBasicBlockIndex(tI->getParent()) != -1);
571
572 CLV.addLocalVar(pI->getType(), CW.getValueName(pI));
573 } else
574 break;
575 insIt++;
576 }
577}
578
579namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000580 /* CLASS CInstPrintVisitor */
581
582 class CInstPrintVisitor: public InstVisitor<CInstPrintVisitor> {
583 CWriter& CW;
584 SlotCalculator& Table;
585 ostream &Out;
586 const Value *Operand;
587
588 void outputLValue(Instruction *);
589 void printPhiFromNextBlock(TerminatorInst *tI, int indx);
590
591 public:
592 CInstPrintVisitor (CWriter &cw, SlotCalculator& table, ostream& o)
593 : CW(cw), Table(table), Out(o) {
594
595 }
596
597 void visitCastInst(CastInst *I);
598 void visitCallInst(CallInst *I);
599 void visitShr(ShiftInst *I);
600 void visitShl(ShiftInst *I);
601 void visitReturnInst(ReturnInst *I);
602 void visitBranchInst(BranchInst *I);
603 void visitSwitchInst(SwitchInst *I);
604 void visitInvokeInst(InvokeInst *I) ;
605 void visitMallocInst(MallocInst *I);
606 void visitAllocaInst(AllocaInst *I);
607 void visitFreeInst(FreeInst *I);
608 void visitLoadInst(LoadInst *I);
609 void visitStoreInst(StoreInst *I);
610 void visitGetElementPtrInst(GetElementPtrInst *I);
611 void visitPHINode(PHINode *I);
612 void visitUnaryOperator (UnaryOperator *I);
613 void visitBinaryOperator(BinaryOperator *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000614 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000615}
616
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000617void CInstPrintVisitor::outputLValue(Instruction *I) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000618 Out << " " << CW.getValueName(I) << " = ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000619}
620
621void CInstPrintVisitor::printPhiFromNextBlock(TerminatorInst *tI, int indx) {
622 BasicBlock *bb = tI->getSuccessor(indx);
623 BasicBlock::const_iterator insIt = bb->begin();
624 while (insIt != bb->end()) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000625 if (PHINode *pI = dyn_cast<PHINode>(*insIt)) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000626 //Its a phinode!
627 //Calculate the incoming index for this
628 int incindex = pI->getBasicBlockIndex(tI->getParent());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000629 if (incindex != -1) {
630 //now we have to do the printing
631 outputLValue(pI);
632 CW.writeOperand(pI->getIncomingValue(incindex), Out);
633 Out << ";\n";
634 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000635 }
636 else break;
637 insIt++;
638 }
639}
640
641// Implement all "other" instructions, except for PHINode
642void CInstPrintVisitor::visitCastInst(CastInst *I) {
643 outputLValue(I);
644 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
645 Out << "(";
646 CW.printType(I->getType(), Out);
647 Out << ")";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000648 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000649 Out << ";\n";
650}
651
652void CInstPrintVisitor::visitCallInst(CallInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000653
654 if (I->getType() != Type::VoidTy)
655 outputLValue(I);
656
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000657 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
658 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
659 const FunctionType *MTy = PTy
660 ? dyn_cast<FunctionType>(PTy->getElementType()):0;
661 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
662
663 // If possible, print out the short form of the call instruction, but we can
664 // only do this if the first argument is a pointer to a nonvararg method,
665 // and if the value returned is not a pointer to a method.
666 //
667 if (RetTy && !MTy->isVarArg() &&
668 (!isa<PointerType>(RetTy)||
669 !isa<FunctionType>(cast<PointerType>(RetTy)))){
670 Out << " ";
671 Out << makeNameProper(Operand->getName());
672 } else {
673 Out << makeNameProper(Operand->getName());
674 }
675 Out << "(";
676 if (I->getNumOperands() > 1)
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000677 CW.writeOperand(I->getOperand(1), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000678 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
679 Out << ",";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000680 CW.writeOperand(I->getOperand(op), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000681 }
682
683 Out << " );\n";
684}
685
686void CInstPrintVisitor::visitShr(ShiftInst *I) {
687 outputLValue(I);
688 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
689 Out << "(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000690 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000691 Out << " >> ";
692 Out << "(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000693 CW.writeOperand(I->getOperand(1), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000694 Out << "));\n";
695}
696
697void CInstPrintVisitor::visitShl(ShiftInst *I) {
698 outputLValue(I);
699 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
700 Out << "(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000701 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000702 Out << " << ";
703 Out << "(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000704 CW.writeOperand(I->getOperand(1), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000705 Out << "));\n";
706}
707
708// Specific Instruction type classes... note that all of the casts are
709// neccesary because we use the instruction classes as opaque types...
710//
711void CInstPrintVisitor::visitReturnInst(ReturnInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000712 Out << "return ";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000713 if (I->getNumOperands())
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000714 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000715 Out << ";\n";
716}
717
718void CInstPrintVisitor::visitBranchInst(BranchInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000719 TerminatorInst *tI = cast<TerminatorInst>(I);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000720 if (I->isConditional()) {
721 Out << " if (";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000722 CW.writeOperand(I->getCondition(), Out);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000723 Out << ")\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000724 printPhiFromNextBlock(tI,0);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000725 Out << " goto ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000726 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000727 Out << ";\n";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000728 Out << " else\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000729 printPhiFromNextBlock(tI,1);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000730 Out << " goto ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000731 CW.writeOperand(I->getOperand(1), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000732 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000733 } else {
734 printPhiFromNextBlock(tI,0);
735 Out << " goto ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000736 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000737 Out << ";\n";
738 }
739 Out << "\n";
740}
741
742void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000743 assert(0 && "Switch not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000744}
745
746void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000747 assert(0 && "Invoke not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000748}
749
750void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
751 outputLValue(I);
752 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
753 string tempstr = "";
754 Out << "(";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000755 CW.printType(cast<PointerType>(I->getType())->getElementType(), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000756 Out << "*) malloc(sizeof(";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000757 CW.printTypeVar(cast<PointerType>(I->getType())->getElementType(),
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000758 tempstr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000759 Out << ")";
760 if (I->getNumOperands()) {
761 Out << " * " ;
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000762 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000763 }
764 Out << ");";
765}
766
767void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
768 outputLValue(I);
769 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
770 string tempstr = "";
771 Out << "(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000772 CW.printTypeVar(I->getType(), tempstr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000773 Out << ") alloca(sizeof(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000774 CW.printTypeVar(cast<PointerType>(I->getType())->getElementType(),
775 tempstr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000776 Out << ")";
777 if (I->getNumOperands()) {
778 Out << " * " ;
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000779 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000780 }
781 Out << ");\n";
782}
783
784void CInstPrintVisitor::visitFreeInst(FreeInst *I) {
785 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
786 Out << "free(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000787 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000788 Out << ");\n";
789}
790
791void CInstPrintVisitor::visitLoadInst(LoadInst *I) {
792 outputLValue(I);
793 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
794 if (I->getNumOperands() <= 1) {
795 Out << "*";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000796 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000797 }
798 else {
799 //Check if it is an array type or struct type ptr!
800 int arrtype = 1;
801 const PointerType *PTy = dyn_cast<PointerType>(I->getType());
802 if (cast<const PointerType>(Operand->getType())->getElementType()->getPrimitiveID() == Type::StructTyID)
803 arrtype = 0;
804 if (arrtype && isa<GlobalValue>(Operand))
805 Out << "(&";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000806 CW.writeOperand(Operand,Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000807 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
808 if (i == 1) {
809 if (arrtype || !isa<GlobalValue>(Operand)) {
810 Out << "[";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000811 CW.writeOperand(I->getOperand(i), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000812 Out << "]";
813 }
814 if (isa<GlobalValue>(Operand) && arrtype)
815 Out << ")";
816 }
817 else {
818 if (arrtype == 1) Out << "[";
819 else
820 Out << ".field";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000821 CW.writeOperand(I->getOperand(i), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000822 if (arrtype == 1) Out << "]";
823 }
824 }
825 }
826 Out << ";\n";
827}
828
829void CInstPrintVisitor::visitStoreInst(StoreInst *I) {
830 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
831 if (I->getNumOperands() <= 2) {
832 Out << "*";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000833 CW.writeOperand(I->getOperand(1), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000834 }
835 else {
836 //Check if it is an array type or struct type ptr!
837 int arrtype = 1;
838 if (cast<const PointerType>(I->getOperand(1)->getType())->getElementType()->getPrimitiveID() == Type::StructTyID)
839 arrtype = 0;
840 if (isa<GlobalValue>(I->getOperand(1)) && arrtype)
841 Out << "(&";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000842 CW.writeOperand(I->getOperand(1), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000843 for (unsigned i = 2, E = I->getNumOperands(); i != E; ++i) {
844 if (i == 2) {
845 if (arrtype || !isa<GlobalValue>(I->getOperand(1))) {
846 Out << "[";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000847 CW.writeOperand(I->getOperand(i), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000848 Out << "]";
849 }
850 if (isa<GlobalValue>(I->getOperand(1)) && arrtype)
851 Out << ")";
852 }
853 else {
854 if (arrtype == 1) Out << "[";
855 else
856 Out << ".field";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000857 CW.writeOperand(I->getOperand(i), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000858 if (arrtype == 1) Out << "]";
859 }
860 }
861 }
862 Out << " = ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000863 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000864 Out << ";\n";
865}
866
867void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) {
868 outputLValue(I);
869 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
870 Out << " &(";
871 if (I->getNumOperands() <= 1)
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000872 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000873 else {
874 //Check if it is an array type or struct type ptr!
875 int arrtype = 1;
876 if ((cast<const PointerType>(Operand->getType()))->getElementType()->getPrimitiveID() == Type::StructTyID)
877 arrtype = 0;
878 if ((isa<GlobalValue>(Operand)) && arrtype)
879 Out << "(&";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000880 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000881 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
882 if (i == 1) {
883 if (arrtype || !isa<GlobalValue>(Operand)){
884 Out << "[";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000885 CW.writeOperand(I->getOperand(i), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000886 Out << "]";
887 }
888 if (isa<GlobalValue>(Operand) && arrtype)
889 Out << ")";
890 }
891 else {
892 if (arrtype == 1) Out << "[";
893 else
894 Out << ".field";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000895 CW.writeOperand(I->getOperand(i), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000896 if (arrtype == 1) Out << "]";
897 }
898 }
899 }
900 Out << ");\n";
901}
902
903void CInstPrintVisitor::visitPHINode(PHINode *I) {
904
905}
906
907void CInstPrintVisitor::visitUnaryOperator (UnaryOperator *I) {
908 if (I->getOpcode() == Instruction::Not) {
909 outputLValue(I);
910 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
911 Out << "!(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000912 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000913 Out << ");\n";
914 }
915 else {
916 Out << "<bad unary inst>\n";
917 }
918}
919
920void CInstPrintVisitor::visitBinaryOperator(BinaryOperator *I) {
921 //binary instructions, shift instructions, setCond instructions.
922 outputLValue(I);
923 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
924 if (I->getType()->getPrimitiveID() == Type::PointerTyID) {
925 Out << "(";
926 CW.printType(I->getType(), Out);
927 Out << ")";
928 }
929 Out << "(";
930 if (Operand->getType()->getPrimitiveID() == Type::PointerTyID)
931 Out << "(long long)";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000932 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000933 Out << getOpcodeOperName(I);
934 // Need the extra parenthisis if the second operand is < 0
935 Out << '(';
936 if (I->getOperand(1)->getType()->getPrimitiveID() == Type::PointerTyID)
937 Out << "(long long)";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000938 CW.writeOperand(I->getOperand(1), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000939 Out << ')';
940 Out << ");\n";
941}
942
943/* END : CInstPrintVisitor implementation */
944
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000945string CWriter::getValueName(const Value *V) {
946 if (V->hasName()) // Print out the label if it exists...
947 return "llvm__" + makeNameProper(V->getName()) + "_" +
948 utostr(V->getType()->getUniqueID());
949
950 int Slot = Table.getValSlot(V);
951 assert(Slot >= 0 && "Invalid value!");
952 return "llvm__tmp_" + itostr(Slot) + "_" +
953 utostr(V->getType()->getUniqueID());
954}
955
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000956void CWriter::printModule(const Module *M) {
957 // printing stdlib inclusion
958 // Out << "#include <stdlib.h>\n";
959
Chris Lattner16c7bb22002-05-09 02:28:59 +0000960 // get declaration for alloca
961 Out << "/* Provide Declarations */\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000962 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000963
964 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000965 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000966 << "typedef unsigned char bool;\n"
967
968 << "\n\n/* Global Symbols */\n";
969
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000970 // Loop over the symbol table, emitting all named constants...
971 if (M->hasSymbolTable())
972 printSymbolTable(*M->getSymbolTable());
973
Chris Lattner16c7bb22002-05-09 02:28:59 +0000974 Out << "\n\n/* Global Data */\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000975 for_each(M->gbegin(), M->gend(),
976 bind_obj(this, &CWriter::printGlobal));
977
Chris Lattner16c7bb22002-05-09 02:28:59 +0000978 // First output all the declarations of the functions as C requires Functions
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000979 // be declared before they are used.
Chris Lattner16c7bb22002-05-09 02:28:59 +0000980 //
981 Out << "\n\n/* Function Declarations */\n";
982 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunctionDecl));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000983
Chris Lattner16c7bb22002-05-09 02:28:59 +0000984 // Output all of the functions...
985 Out << "\n\n/* Function Bodies */\n";
986 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000987}
988
989// prints the global constants
990void CWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000991 string tempostr = getValueName(GV);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000992 if (GV->hasInternalLinkage()) Out << "static ";
993
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000994 printTypeVar(GV->getType()->getElementType(), tempostr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000995
996 if (GV->hasInitializer()) {
997 Out << " = " ;
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000998 writeOperand(GV->getInitializer(), Out, false);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000999 }
1000
1001 Out << ";\n";
1002}
1003
1004// printSymbolTable - Run through symbol table looking for named constants
1005// if a named constant is found, emit it's declaration...
1006// Assuming that symbol table has only types and constants.
1007void CWriter::printSymbolTable(const SymbolTable &ST) {
1008 // GraphT G;
1009 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
1010 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
1011 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
1012
1013 // TODO
1014 // Need to run through all the used types in the program
1015 // FindUsedTypes &FUT = new FindUsedTypes();
1016 // const std::set<const Type *> &UsedTypes = FUT.getTypes();
1017 // Filter out the structures printing forward definitions for each of them
1018 // and creating the dependency graph.
1019 // Print forward definitions to all of them
1020 // print the typedefs topologically sorted
1021
1022 // But for now we have
1023 for (; I != End; ++I) {
1024 const Value *V = I->second;
1025 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
1026 printConstant(CPV);
1027 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
1028 string tempostr;
1029 string tempstr = "";
1030 Out << "typedef ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001031 tempostr = "llvm__" + I->first;
Chris Lattner16c7bb22002-05-09 02:28:59 +00001032 string TypeNameVar = calcTypeNameVar(Ty, TypeNames,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001033 tempostr, tempstr);
1034 Out << TypeNameVar << ";\n";
1035 if (!isa<PointerType>(Ty) ||
1036 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
1037 TypeNames.insert(std::make_pair(Ty, "llvm__"+I->first));
1038 }
1039 }
1040 }
1041}
1042
1043
1044// printConstant - Print out a constant pool entry...
1045//
1046void CWriter::printConstant(const Constant *CPV) {
1047 // TODO
1048 // Dinakar : Don't know what to do with unnamed constants
1049 // should do something about it later.
1050
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001051 string tempostr = getValueName(CPV);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001052
1053 // Print out the constant type...
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001054 printTypeVar(CPV->getType(), tempostr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001055
1056 Out << " = ";
1057 // Write the value out now...
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001058 writeOperand(CPV, Out, false);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001059
1060 Out << "\n";
1061}
1062
Chris Lattner16c7bb22002-05-09 02:28:59 +00001063// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001064//
Chris Lattner16c7bb22002-05-09 02:28:59 +00001065void CWriter::printFunctionDecl(const Function *F) {
1066 printFunctionSignature(F);
1067 Out << ";\n";
1068}
1069
1070void CWriter::printFunctionSignature(const Function *F) {
1071 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001072
1073 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +00001074 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001075
Chris Lattner16c7bb22002-05-09 02:28:59 +00001076 // Print out the return type and name...
1077 printType(F->getReturnType(), Out);
1078 Out << " " << makeNameProper(F->getName()) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001079
Chris Lattner16c7bb22002-05-09 02:28:59 +00001080 if (!F->isExternal()) {
1081 for_each(F->getArgumentList().begin(), F->getArgumentList().end(),
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001082 bind_obj(this, &CWriter::printFunctionArgument));
1083 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001084 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001085 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +00001086 FT->getParamTypes().begin(),
1087 E = FT->getParamTypes().end(); I != E; ++I) {
1088 if (I != FT->getParamTypes().begin()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001089 printType(*I, Out);
1090 }
1091 }
1092
1093 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +00001094 if (FT->isVarArg()) {
1095 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001096 Out << "..."; // Output varargs portion of signature!
1097 }
Chris Lattner16c7bb22002-05-09 02:28:59 +00001098 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001099}
1100
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001101
1102// printFunctionArgument - This member is called for every argument that
1103// is passed into the method. Simply print it out
1104//
1105void CWriter::printFunctionArgument(const Argument *Arg) {
1106 // Insert commas as we go... the first arg doesn't get a comma
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001107 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
1108
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001109 // Output type...
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001110 printTypeVar(Arg->getType(), getValueName(Arg));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001111}
1112
Chris Lattner16c7bb22002-05-09 02:28:59 +00001113void CWriter::printFunction(const Function *F) {
1114 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001115
Chris Lattnerf34ee812002-05-09 03:12:34 +00001116 Table.incorporateFunction(F);
1117
Chris Lattner16c7bb22002-05-09 02:28:59 +00001118 // Process each of the basic blocks, gather information and call the
1119 // output methods on the CLocalVars and Function* objects.
1120
1121 // gather local variable information for each basic block
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001122 InstLocalVarsVisitor ILV(*this);
Chris Lattner16c7bb22002-05-09 02:28:59 +00001123 ILV.visit((Function *)F);
1124
1125 printFunctionSignature(F);
1126 Out << " {\n";
1127
1128 // Loop over the symbol table, emitting all named constants...
1129 if (F->hasSymbolTable())
1130 printSymbolTable(*F->getSymbolTable());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001131
Chris Lattner16c7bb22002-05-09 02:28:59 +00001132 // print the local variables
1133 // we assume that every local variable is alloca'ed in the C code.
1134 std::map<const Type*, VarListType> &locals = ILV.CLV.LocalVars;
1135
1136 map<const Type*, VarListType>::iterator iter;
1137 for (iter = locals.begin(); iter != locals.end(); ++iter) {
1138 VarListType::iterator listiter;
1139 for (listiter = iter->second.begin(); listiter != iter->second.end();
1140 ++listiter) {
1141 Out << " ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001142 printTypeVar(iter->first, *listiter);
Chris Lattner16c7bb22002-05-09 02:28:59 +00001143 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001144 }
1145 }
Chris Lattner16c7bb22002-05-09 02:28:59 +00001146
1147 // print the basic blocks
1148 for_each(F->begin(), F->end(), bind_obj(this, &CWriter::outputBasicBlock));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001149
Chris Lattner16c7bb22002-05-09 02:28:59 +00001150 Out << "}\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +00001151 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001152}
1153
1154void CWriter::outputBasicBlock(const BasicBlock* BB) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001155 Out << getValueName(BB) << ":\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001156
1157 // Output all of the instructions in the basic block...
1158 // print the basic blocks
1159 CInstPrintVisitor CIPV(*this, Table, Out);
1160 CIPV.visit((BasicBlock *) BB);
1161}
1162
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001163// printType - Go to extreme measures to attempt to print out a short, symbolic
1164// version of a type name.
1165ostream& CWriter::printType(const Type *Ty, ostream &Out) {
1166 return printTypeInt(Out, Ty, TypeNames);
1167}
1168
1169
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001170void CWriter::writeOperand(const Value *Operand,
Chris Lattner16c7bb22002-05-09 02:28:59 +00001171 ostream &Out, bool PrintName = true) {
Chris Lattner16c7bb22002-05-09 02:28:59 +00001172 if (PrintName && Operand->hasName()) {
1173 // If Operand has a name.
1174 Out << "llvm__" << makeNameProper(Operand->getName()) << "_" <<
1175 Operand->getType()->getUniqueID();
1176 return;
1177 }
1178 else if (const Constant *CPV = dyn_cast<const Constant>(Operand)) {
1179 if (isa<ConstantPointerNull>(CPV))
1180 Out << "NULL";
1181 else
1182 Out << getConstStrValue(CPV);
1183 }
1184 else {
1185 int Slot = Table.getValSlot(Operand);
1186 if (Slot >= 0)
1187 Out << "llvm__tmp_" << Slot << "_" << Operand->getType()->getUniqueID();
1188 else if (PrintName)
1189 Out << "<badref>";
1190 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001191}
1192
1193
1194//===----------------------------------------------------------------------===//
1195// External Interface declaration
1196//===----------------------------------------------------------------------===//
1197
Chris Lattnerf34ee812002-05-09 03:12:34 +00001198void WriteToC(const Module *M, ostream &Out) {
1199 assert(M && "You can't write a null module!!");
1200 SlotCalculator SlotTable(M, false);
1201 CWriter W(Out, SlotTable, M);
1202 W.write(M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001203 Out.flush();
1204}