blob: f4d83f7c2d2674340956b60158a18e62b675e1ce [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 +000058static std::string getConstArrayStrValue(const Constant* CPV) {
59 std::string Result;
60
61 // As a special case, print the array as a string if it is an array of
62 // ubytes or an array of sbytes with positive values.
63 //
64 const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType();
65 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
66
67 if (ETy == Type::SByteTy) {
68 for (unsigned i = 0; i < CPV->getNumOperands(); ++i)
69 if (ETy == Type::SByteTy &&
70 cast<ConstantSInt>(CPV->getOperand(i))->getValue() < 0) {
71 isString = false;
72 break;
73 }
74 }
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000075 if (isString) {
76 // Make sure the last character is a null char, as automatically added by C
77 if (CPV->getNumOperands() == 0 ||
78 !cast<Constant>(*(CPV->op_end()-1))->isNullValue())
79 isString = false;
80 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000081
82 if (isString) {
83 Result = "\"";
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000084 // Do not include the last character, which we know is null
85 for (unsigned i = 0, e = CPV->getNumOperands()-1; i != e; ++i) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000086 unsigned char C = (ETy == Type::SByteTy) ?
87 (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() :
88 (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue();
89
90 if (isprint(C)) {
91 Result += C;
92 } else {
Chris Lattner2f5eb4e2002-05-09 03:56:52 +000093 switch (C) {
94 case '\n': Result += "\\n"; break;
95 case '\t': Result += "\\t"; break;
96 case '\r': Result += "\\r"; break;
97 case '\v': Result += "\\v"; break;
98 case '\a': Result += "\\a"; break;
99 default:
100 Result += "\\x";
101 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
102 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
103 break;
104 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000105 }
106 }
107 Result += "\"";
108
109 } else {
110 Result = "{";
111 if (CPV->getNumOperands()) {
112 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
113 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
114 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
115 }
116 Result += " }";
117 }
118
119 return Result;
120}
121
122static std::string getConstStructStrValue(const Constant* CPV) {
123 std::string Result = "{";
124 if (CPV->getNumOperands()) {
125 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
126 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
127 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
128 }
129
130 return Result + " }";
131}
132
133// our own getStrValue function for constant initializers
134static std::string getConstStrValue(const Constant* CPV) {
135 // Does not handle null pointers, that needs to be checked explicitly
136 string tempstr;
137 if (CPV == ConstantBool::False)
138 return "0";
139 else if (CPV == ConstantBool::True)
140 return "1";
141
142 else if (isa<ConstantArray>(CPV)) {
143 tempstr = getConstArrayStrValue(CPV);
144 }
145 else if (isa<ConstantStruct>(CPV)) {
146 tempstr = getConstStructStrValue(CPV);
147 }
148 else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CPV)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000149 tempstr = utostr(CUI->getValue());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000150 }
151 else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CPV)) {
152 tempstr = itostr(CSI->getValue());
153 }
154 else if (ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) {
155 tempstr = ftostr(CFP->getValue());
156 }
157
158 if (CPV->getType() == Type::ULongTy)
159 tempstr += "ull";
160 else if (CPV->getType() == Type::LongTy)
161 tempstr += "ll";
162 else if (CPV->getType() == Type::UIntTy ||
163 CPV->getType() == Type::UShortTy)
164 tempstr += "u";
165
166 return tempstr;
167
168}
169
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000170// Internal function
171// Essentially pass the Type* variable, an empty typestack and this prints
172// out the C type
Chris Lattner16c7bb22002-05-09 02:28:59 +0000173static string calcTypeName(const Type *Ty, map<const Type *, string> &TypeNames,
174 string &FunctionInfo) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000175
176 // Takin' care of the fact that boolean would be int in C
177 // and that ushort would be unsigned short etc.
178
179 // Base Case
180 if (Ty->isPrimitiveType())
181 switch (Ty->getPrimitiveID()) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000182 case Type::VoidTyID: return "void";
183 case Type::BoolTyID: return "bool";
184 case Type::UByteTyID: return "unsigned char";
185 case Type::SByteTyID: return "signed char";
186 case Type::UShortTyID: return "unsigned short";
187 case Type::ShortTyID: return "short";
188 case Type::UIntTyID: return "unsigned";
189 case Type::IntTyID: return "int";
190 case Type::ULongTyID: return "unsigned long long";
191 case Type::LongTyID: return "signed long long";
192 case Type::FloatTyID: return "float";
193 case Type::DoubleTyID: return "double";
194 default : assert(0 && "Unknown primitive type!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000195 }
196
197 // Check to see if the type is named.
198 map<const Type *, string>::iterator I = TypeNames.find(Ty);
199 if (I != TypeNames.end())
200 return I->second;
201
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000202 string Result;
203 string MInfo = "";
204 switch (Ty->getPrimitiveID()) {
205 case Type::FunctionTyID: {
206 const FunctionType *MTy = cast<const FunctionType>(Ty);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000207 Result = calcTypeName(MTy->getReturnType(), TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000208 if (MInfo != "")
209 Result += ") " + MInfo;
210 Result += "(";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000211 FunctionInfo += " (";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000212 for (FunctionType::ParamTypes::const_iterator
213 I = MTy->getParamTypes().begin(),
214 E = MTy->getParamTypes().end(); I != E; ++I) {
215 if (I != MTy->getParamTypes().begin())
Chris Lattner16c7bb22002-05-09 02:28:59 +0000216 FunctionInfo += ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000217 MInfo = "";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000218 FunctionInfo += calcTypeName(*I, TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000219 if (MInfo != "")
220 Result += ") " + MInfo;
221 }
222 if (MTy->isVarArg()) {
223 if (!MTy->getParamTypes().empty())
Chris Lattner16c7bb22002-05-09 02:28:59 +0000224 FunctionInfo += ", ";
225 FunctionInfo += "...";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000226 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000227 FunctionInfo += ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000228 break;
229 }
230 case Type::StructTyID: {
231 string tempstr = "";
232 const StructType *STy = cast<const StructType>(Ty);
233 Result = " struct {\n ";
234 int indx = 0;
235 for (StructType::ElementTypes::const_iterator
236 I = STy->getElementTypes().begin(),
237 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000238 Result += calcTypeNameVar(*I, TypeNames,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000239 "field" + itostr(indx++), tempstr);
240 Result += ";\n ";
241 }
242 Result += " } ";
243 break;
244 }
245 case Type::PointerTyID:
246 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner16c7bb22002-05-09 02:28:59 +0000247 TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000248 Result += "*";
249 break;
250 case Type::ArrayTyID: {
251 const ArrayType *ATy = cast<const ArrayType>(Ty);
252 int NumElements = ATy->getNumElements();
Chris Lattner16c7bb22002-05-09 02:28:59 +0000253 Result = calcTypeName(ATy->getElementType(), TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000254 Result += "*";
255 break;
256 }
257 default:
258 assert(0 && "Unhandled case in getTypeProps!");
259 Result = "<error>";
260 }
261
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000262 return Result;
263}
264
265// Internal function
266// Pass the Type* variable and and the variable name and this prints out the
267// variable declaration.
268// This is different from calcTypeName because if you need to declare an array
269// the size of the array would appear after the variable name itself
270// For eg. int a[10];
Chris Lattner16c7bb22002-05-09 02:28:59 +0000271static string calcTypeNameVar(const Type *Ty,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000272 map<const Type *, string> &TypeNames,
273 string VariableName, string NameSoFar) {
274 if (Ty->isPrimitiveType())
275 switch (Ty->getPrimitiveID()) {
276 case Type::BoolTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000277 return "bool " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000278 case Type::UByteTyID:
279 return "unsigned char " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000280 case Type::SByteTyID:
281 return "signed char " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000282 case Type::UShortTyID:
283 return "unsigned long long " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000284 case Type::ULongTyID:
285 return "unsigned long long " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000286 case Type::LongTyID:
287 return "signed long long " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000288 case Type::UIntTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000289 return "unsigned " + NameSoFar + VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000290 default :
291 return Ty->getDescription() + " " + NameSoFar + VariableName;
292 }
293
294 // Check to see if the type is named.
295 map<const Type *, string>::iterator I = TypeNames.find(Ty);
296 if (I != TypeNames.end())
297 return I->second + " " + NameSoFar + VariableName;
298
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000299 string Result;
300 string tempstr = "";
301
302 switch (Ty->getPrimitiveID()) {
303 case Type::FunctionTyID: {
304 string MInfo = "";
305 const FunctionType *MTy = cast<const FunctionType>(Ty);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000306 Result += calcTypeName(MTy->getReturnType(), TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000307 if (MInfo != "")
308 Result += ") " + MInfo;
309 Result += " " + NameSoFar + VariableName;
310 Result += " (";
311 for (FunctionType::ParamTypes::const_iterator
312 I = MTy->getParamTypes().begin(),
313 E = MTy->getParamTypes().end(); I != E; ++I) {
314 if (I != MTy->getParamTypes().begin())
315 Result += ", ";
316 MInfo = "";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000317 Result += calcTypeName(*I, TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000318 if (MInfo != "")
319 Result += ") " + MInfo;
320 }
321 if (MTy->isVarArg()) {
322 if (!MTy->getParamTypes().empty())
323 Result += ", ";
324 Result += "...";
325 }
326 Result += ")";
327 break;
328 }
329 case Type::StructTyID: {
330 const StructType *STy = cast<const StructType>(Ty);
331 Result = " struct {\n ";
332 int indx = 0;
333 for (StructType::ElementTypes::const_iterator
334 I = STy->getElementTypes().begin(),
335 E = STy->getElementTypes().end(); I != E; ++I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000336 Result += calcTypeNameVar(*I, TypeNames,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000337 "field" + itostr(indx++), "");
338 Result += ";\n ";
339 }
340 Result += " }";
341 Result += " " + NameSoFar + VariableName;
342 break;
343 }
344
345 case Type::PointerTyID: {
346 Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner16c7bb22002-05-09 02:28:59 +0000347 TypeNames, tempstr,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000348 "(*" + NameSoFar + VariableName + ")");
349 break;
350 }
351
352 case Type::ArrayTyID: {
353 const ArrayType *ATy = cast<const ArrayType>(Ty);
354 int NumElements = ATy->getNumElements();
Chris Lattner16c7bb22002-05-09 02:28:59 +0000355 Result = calcTypeNameVar(ATy->getElementType(), TypeNames,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000356 tempstr, NameSoFar + VariableName + "[" +
357 itostr(NumElements) + "]");
358 break;
359 }
360 default:
361 assert(0 && "Unhandled case in getTypeProps!");
362 Result = "<error>";
363 }
364
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000365 return Result;
366}
367
368// printTypeVarInt - The internal guts of printing out a type that has a
369// potentially named portion and the variable associated with the type.
370static ostream &printTypeVarInt(ostream &Out, const Type *Ty,
371 map<const Type *, string> &TypeNames,
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000372 const string &VariableName) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000373 // Primitive types always print out their description, regardless of whether
374 // they have been named or not.
375
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000376 if (Ty->isPrimitiveType())
377 switch (Ty->getPrimitiveID()) {
378 case Type::BoolTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000379 return Out << "bool " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000380 case Type::UByteTyID:
381 return Out << "unsigned char " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000382 case Type::SByteTyID:
383 return Out << "signed char " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000384 case Type::UShortTyID:
385 return Out << "unsigned long long " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000386 case Type::ULongTyID:
387 return Out << "unsigned long long " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000388 case Type::LongTyID:
389 return Out << "signed long long " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000390 case Type::UIntTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000391 return Out << "unsigned " << VariableName;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000392 default :
393 return Out << Ty->getDescription() << " " << VariableName;
394 }
395
396 // Check to see if the type is named.
397 map<const Type *, string>::iterator I = TypeNames.find(Ty);
398 if (I != TypeNames.end()) return Out << I->second << " " << VariableName;
399
400 // Otherwise we have a type that has not been named but is a derived type.
401 // Carefully recurse the type hierarchy to print out any contained symbolic
402 // names.
403 //
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000404 string TypeNameVar, tempstr = "";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000405 TypeNameVar = calcTypeNameVar(Ty, TypeNames, VariableName, tempstr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000406 return Out << TypeNameVar;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000407}
408
409// Internal guts of printing a type name
410static ostream &printTypeInt(ostream &Out, const Type *Ty,
411 map<const Type *, string> &TypeNames) {
412 // Primitive types always print out their description, regardless of whether
413 // they have been named or not.
414
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000415 if (Ty->isPrimitiveType())
416 switch (Ty->getPrimitiveID()) {
417 case Type::BoolTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000418 return Out << "bool";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000419 case Type::UByteTyID:
420 return Out << "unsigned char";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000421 case Type::SByteTyID:
422 return Out << "signed char";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000423 case Type::UShortTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000424 return Out << "unsigned short";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000425 case Type::ULongTyID:
426 return Out << "unsigned long long";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000427 case Type::LongTyID:
428 return Out << "signed long long";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000429 case Type::UIntTyID:
Chris Lattner16c7bb22002-05-09 02:28:59 +0000430 return Out << "unsigned";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000431 default :
432 return Out << Ty->getDescription();
433 }
434
435 // Check to see if the type is named.
436 map<const Type *, string>::iterator I = TypeNames.find(Ty);
437 if (I != TypeNames.end()) return Out << I->second;
438
439 // Otherwise we have a type that has not been named but is a derived type.
440 // Carefully recurse the type hierarchy to print out any contained symbolic
441 // names.
442 //
Chris Lattner16c7bb22002-05-09 02:28:59 +0000443 string MInfo;
444 string TypeName = calcTypeName(Ty, TypeNames, MInfo);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000445 // TypeNames.insert(std::make_pair(Ty, TypeName));
446 //Cache type name for later use
447 if (MInfo != "")
448 return Out << TypeName << ")" << MInfo;
449 else
450 return Out << TypeName;
451}
452
453namespace {
454
455 //Internal CWriter class mimics AssemblyWriter.
456 class CWriter {
457 ostream& Out;
458 SlotCalculator &Table;
459 const Module *TheModule;
460 map<const Type *, string> TypeNames;
461 public:
462 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
463 : Out(o), Table(Tab), TheModule(M) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000464 }
465
Chris Lattner16c7bb22002-05-09 02:28:59 +0000466 inline void write(const Module *M) { printModule(M); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000467
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000468 ostream& printTypeVar(const Type *Ty, const string &VariableName) {
469 return printTypeVarInt(Out, Ty, TypeNames, VariableName);
470 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000471
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000472
473
474 ostream& printType(const Type *Ty, ostream &Out);
475 void writeOperand(const Value *Operand, ostream &Out,bool PrintName = true);
476
477 string getValueName(const Value *V);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000478 private :
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000479
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000480 void printModule(const Module *M);
481 void printSymbolTable(const SymbolTable &ST);
482 void printConstant(const Constant *CPV);
483 void printGlobal(const GlobalVariable *GV);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000484 void printFunctionSignature(const Function *F);
485 void printFunctionDecl(const Function *F); // Print just the forward decl
486 void printFunctionArgument(const Argument *FA);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000487
488 void printFunction(const Function *);
489
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000490 void outputBasicBlock(const BasicBlock *);
491 };
492 /* END class CWriter */
493
494
495 /* CLASS InstLocalVarsVisitor */
496 class InstLocalVarsVisitor : public InstVisitor<InstLocalVarsVisitor> {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000497 CWriter& CW;
Chris Lattner16c7bb22002-05-09 02:28:59 +0000498 void handleTerminator(TerminatorInst *tI, int indx);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000499 public:
500 CLocalVars CLV;
501
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000502 InstLocalVarsVisitor(CWriter &cw) : CW(cw) {}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000503
504 void visitInstruction(Instruction *I) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000505 if (I->getType() != Type::VoidTy) {
506 string tempostr = CW.getValueName(I);
507 CLV.addLocalVar(I->getType(), tempostr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000508 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000509 }
510
511 void visitBranchInst(BranchInst *I) {
Chris Lattner16c7bb22002-05-09 02:28:59 +0000512 handleTerminator(I, 0);
513 if (I->isConditional())
514 handleTerminator(I, 1);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000515 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000516 };
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000517}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000518
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000519void InstLocalVarsVisitor::handleTerminator(TerminatorInst *tI,int indx) {
520 BasicBlock *bb = tI->getSuccessor(indx);
521
522 BasicBlock::const_iterator insIt = bb->begin();
523 while (insIt != bb->end()) {
524 if (const PHINode *pI = dyn_cast<PHINode>(*insIt)) {
525 // Its a phinode!
526 // Calculate the incoming index for this
527 assert(pI->getBasicBlockIndex(tI->getParent()) != -1);
528
529 CLV.addLocalVar(pI->getType(), CW.getValueName(pI));
530 } else
531 break;
532 insIt++;
533 }
534}
535
536namespace {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000537 /* CLASS CInstPrintVisitor */
538
539 class CInstPrintVisitor: public InstVisitor<CInstPrintVisitor> {
540 CWriter& CW;
541 SlotCalculator& Table;
542 ostream &Out;
543 const Value *Operand;
544
545 void outputLValue(Instruction *);
546 void printPhiFromNextBlock(TerminatorInst *tI, int indx);
Chris Lattner44408262002-05-09 03:50:42 +0000547 void printIndexingExpr(MemAccessInst *MAI);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000548
549 public:
550 CInstPrintVisitor (CWriter &cw, SlotCalculator& table, ostream& o)
Chris Lattner44408262002-05-09 03:50:42 +0000551 : CW(cw), Table(table), Out(o) {}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000552
553 void visitCastInst(CastInst *I);
554 void visitCallInst(CallInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000555 void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000556 void visitReturnInst(ReturnInst *I);
557 void visitBranchInst(BranchInst *I);
558 void visitSwitchInst(SwitchInst *I);
559 void visitInvokeInst(InvokeInst *I) ;
560 void visitMallocInst(MallocInst *I);
561 void visitAllocaInst(AllocaInst *I);
562 void visitFreeInst(FreeInst *I);
563 void visitLoadInst(LoadInst *I);
564 void visitStoreInst(StoreInst *I);
565 void visitGetElementPtrInst(GetElementPtrInst *I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000566 void visitPHINode(PHINode *I) {}
567
568 void visitNot(GenericUnaryInst *I);
569 void visitBinaryOperator(Instruction *I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000570 };
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000571}
572
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000573void CInstPrintVisitor::outputLValue(Instruction *I) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000574 Out << " " << CW.getValueName(I) << " = ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000575}
576
577void CInstPrintVisitor::printPhiFromNextBlock(TerminatorInst *tI, int indx) {
578 BasicBlock *bb = tI->getSuccessor(indx);
579 BasicBlock::const_iterator insIt = bb->begin();
580 while (insIt != bb->end()) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000581 if (PHINode *pI = dyn_cast<PHINode>(*insIt)) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000582 //Its a phinode!
583 //Calculate the incoming index for this
584 int incindex = pI->getBasicBlockIndex(tI->getParent());
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000585 if (incindex != -1) {
586 //now we have to do the printing
587 outputLValue(pI);
588 CW.writeOperand(pI->getIncomingValue(incindex), Out);
589 Out << ";\n";
590 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000591 }
592 else break;
593 insIt++;
594 }
595}
596
597// Implement all "other" instructions, except for PHINode
598void CInstPrintVisitor::visitCastInst(CastInst *I) {
599 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000600 Out << "(";
601 CW.printType(I->getType(), Out);
602 Out << ")";
Chris Lattner2f499022002-05-09 04:21:21 +0000603 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000604 Out << ";\n";
605}
606
607void CInstPrintVisitor::visitCallInst(CallInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000608 if (I->getType() != Type::VoidTy)
609 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000610 else
611 Out << " ";
Chris Lattnerf34ee812002-05-09 03:12:34 +0000612
Chris Lattner2f499022002-05-09 04:21:21 +0000613 const PointerType *PTy = cast<PointerType>(I->getCalledValue()->getType());
614 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
615 const Type *RetTy = FTy->getReturnType();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000616
Chris Lattner2f499022002-05-09 04:21:21 +0000617 Out << CW.getValueName(I->getOperand(0)) << "(";
618
619 if (I->getNumOperands() != 0) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000620 CW.writeOperand(I->getOperand(1), Out);
Chris Lattner2f499022002-05-09 04:21:21 +0000621
622 for (unsigned op = 2, Eop = I->getNumOperands(); op != Eop; ++op) {
623 Out << ", ";
624 CW.writeOperand(I->getOperand(op), Out);
625 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000626 }
Chris Lattner2f499022002-05-09 04:21:21 +0000627 Out << ");\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000628}
629
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000630// Specific Instruction type classes... note that all of the casts are
631// neccesary because we use the instruction classes as opaque types...
632//
633void CInstPrintVisitor::visitReturnInst(ReturnInst *I) {
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000634 Out << " return ";
Chris Lattner16c7bb22002-05-09 02:28:59 +0000635 if (I->getNumOperands())
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000636 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000637 Out << ";\n";
638}
639
640void CInstPrintVisitor::visitBranchInst(BranchInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000641 TerminatorInst *tI = cast<TerminatorInst>(I);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000642 if (I->isConditional()) {
643 Out << " if (";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000644 CW.writeOperand(I->getCondition(), Out);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000645 Out << ") {\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000646 printPhiFromNextBlock(tI,0);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000647 Out << " goto ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000648 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000649 Out << ";\n";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000650 Out << " } else {\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000651 printPhiFromNextBlock(tI,1);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000652 Out << " goto ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000653 CW.writeOperand(I->getOperand(1), Out);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000654 Out << ";\n }\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000655 } else {
656 printPhiFromNextBlock(tI,0);
657 Out << " goto ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000658 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000659 Out << ";\n";
660 }
661 Out << "\n";
662}
663
664void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000665 assert(0 && "Switch not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000666}
667
668void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
Chris Lattnerf34ee812002-05-09 03:12:34 +0000669 assert(0 && "Invoke not implemented!");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000670}
671
672void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
673 outputLValue(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000674 Out << "(";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000675 CW.printType(I->getType()->getElementType(), Out);
676 Out << "*)malloc(sizeof(";
677 CW.printTypeVar(I->getType()->getElementType(), "");
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000678 Out << ")";
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000679
680 if (I->isArrayAllocation()) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000681 Out << " * " ;
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000682 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000683 }
684 Out << ");";
685}
686
687void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
688 outputLValue(I);
689 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
690 string tempstr = "";
691 Out << "(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000692 CW.printTypeVar(I->getType(), tempstr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000693 Out << ") alloca(sizeof(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000694 CW.printTypeVar(cast<PointerType>(I->getType())->getElementType(),
695 tempstr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000696 Out << ")";
697 if (I->getNumOperands()) {
698 Out << " * " ;
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000699 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000700 }
701 Out << ");\n";
702}
703
704void CInstPrintVisitor::visitFreeInst(FreeInst *I) {
705 Operand = I->getNumOperands() ? I->getOperand(0) : 0;
706 Out << "free(";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000707 CW.writeOperand(Operand, Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000708 Out << ");\n";
709}
710
Chris Lattner44408262002-05-09 03:50:42 +0000711void CInstPrintVisitor::printIndexingExpr(MemAccessInst *MAI) {
712 CW.writeOperand(MAI->getPointerOperand(), Out);
713
714 for (MemAccessInst::op_iterator I = MAI->idx_begin(), E = MAI->idx_end();
715 I != E; ++I)
716 if ((*I)->getType() == Type::UIntTy) {
717 Out << "[";
718 CW.writeOperand(*I, Out);
719 Out << "]";
720 } else {
721 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000722 }
Chris Lattner44408262002-05-09 03:50:42 +0000723}
724
725void CInstPrintVisitor::visitLoadInst(LoadInst *I) {
726 outputLValue(I);
727 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000728 Out << ";\n";
729}
730
Chris Lattner44408262002-05-09 03:50:42 +0000731void CInstPrintVisitor::visitStoreInst(StoreInst *I) {
732 Out << " ";
733 printIndexingExpr(I);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000734 Out << " = ";
Chris Lattner44408262002-05-09 03:50:42 +0000735 CW.writeOperand(I->getOperand(0), Out);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000736 Out << ";\n";
737}
738
739void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) {
740 outputLValue(I);
Chris Lattner44408262002-05-09 03:50:42 +0000741 Out << "&";
742 printIndexingExpr(I);
743 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000744}
745
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000746void CInstPrintVisitor::visitNot(GenericUnaryInst *I) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000747 outputLValue(I);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000748 Out << "~";
749 CW.writeOperand(I->getOperand(0), Out);
750 Out << ";\n";
751}
752
753void CInstPrintVisitor::visitBinaryOperator(Instruction *I) {
754 // binary instructions, shift instructions, setCond instructions.
755 outputLValue(I);
756 if (isa<PointerType>(I->getType())) {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000757 Out << "(";
758 CW.printType(I->getType(), Out);
759 Out << ")";
760 }
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000761
762 if (isa<PointerType>(I->getType())) Out << "(long long)";
763 CW.writeOperand(I->getOperand(0), Out);
764
765 switch (I->getOpcode()) {
766 case Instruction::Add: Out << "+"; break;
767 case Instruction::Sub: Out << "-"; break;
768 case Instruction::Mul: Out << "*"; break;
769 case Instruction::Div: Out << "/"; break;
770 case Instruction::Rem: Out << "%"; break;
771 case Instruction::And: Out << "&"; break;
772 case Instruction::Or: Out << "|"; break;
773 case Instruction::Xor: Out << "^"; break;
774 case Instruction::SetEQ: Out << "=="; break;
775 case Instruction::SetNE: Out << "!="; break;
776 case Instruction::SetLE: Out << "<="; break;
777 case Instruction::SetGE: Out << ">="; break;
778 case Instruction::SetLT: Out << "<"; break;
779 case Instruction::SetGT: Out << ">"; break;
780 case Instruction::Shl : Out << "<<"; break;
781 case Instruction::Shr : Out << ">>"; break;
782 default: cerr << "Invalid operator type!" << I; abort();
783 }
784
785 if (isa<PointerType>(I->getType())) Out << "(long long)";
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000786 CW.writeOperand(I->getOperand(1), Out);
Chris Lattner2f5f51a2002-05-09 03:28:37 +0000787 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000788}
789
790/* END : CInstPrintVisitor implementation */
791
Chris Lattner2f499022002-05-09 04:21:21 +0000792// We dont want identifier names with ., space, - in them.
793// So we replace them with _
794static string makeNameProper(string x) {
795 string tmp;
796 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
797 switch (*sI) {
798 case '.': tmp += "_d"; break;
799 case ' ': tmp += "_s"; break;
800 case '-': tmp += "_D"; break;
801 case '_': tmp += "__"; break;
802 default: tmp += *sI;
803 }
804
805 return tmp;
806}
807
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000808string CWriter::getValueName(const Value *V) {
Chris Lattner2f499022002-05-09 04:21:21 +0000809 if (V->hasName()) { // Print out the label if it exists...
810 if (isa<GlobalValue>(V)) // Do not mangle globals...
811 return makeNameProper(V->getName());
812
Chris Lattner44408262002-05-09 03:50:42 +0000813 return "l_" + makeNameProper(V->getName()) + "_" +
Chris Lattner2f499022002-05-09 04:21:21 +0000814 utostr(V->getType()->getUniqueID());
815 }
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000816
817 int Slot = Table.getValSlot(V);
818 assert(Slot >= 0 && "Invalid value!");
Chris Lattner44408262002-05-09 03:50:42 +0000819 return "ltmp_" + itostr(Slot) + "_" +
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000820 utostr(V->getType()->getUniqueID());
821}
822
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000823void CWriter::printModule(const Module *M) {
824 // printing stdlib inclusion
825 // Out << "#include <stdlib.h>\n";
826
Chris Lattner16c7bb22002-05-09 02:28:59 +0000827 // get declaration for alloca
828 Out << "/* Provide Declarations */\n"
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000829 << "#include <alloca.h>\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000830
831 // Provide a definition for null if one does not already exist.
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000832 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
Chris Lattner16c7bb22002-05-09 02:28:59 +0000833 << "typedef unsigned char bool;\n"
834
835 << "\n\n/* Global Symbols */\n";
836
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000837 // Loop over the symbol table, emitting all named constants...
838 if (M->hasSymbolTable())
839 printSymbolTable(*M->getSymbolTable());
840
Chris Lattner16c7bb22002-05-09 02:28:59 +0000841 Out << "\n\n/* Global Data */\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000842 for_each(M->gbegin(), M->gend(),
843 bind_obj(this, &CWriter::printGlobal));
844
Chris Lattner16c7bb22002-05-09 02:28:59 +0000845 // First output all the declarations of the functions as C requires Functions
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000846 // be declared before they are used.
Chris Lattner16c7bb22002-05-09 02:28:59 +0000847 //
848 Out << "\n\n/* Function Declarations */\n";
849 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunctionDecl));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000850
Chris Lattner16c7bb22002-05-09 02:28:59 +0000851 // Output all of the functions...
852 Out << "\n\n/* Function Bodies */\n";
853 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000854}
855
856// prints the global constants
857void CWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000858 string tempostr = getValueName(GV);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000859 if (GV->hasInternalLinkage()) Out << "static ";
860
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000861 printTypeVar(GV->getType()->getElementType(), tempostr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000862
863 if (GV->hasInitializer()) {
864 Out << " = " ;
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000865 writeOperand(GV->getInitializer(), Out, false);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000866 }
867
868 Out << ";\n";
869}
870
871// printSymbolTable - Run through symbol table looking for named constants
872// if a named constant is found, emit it's declaration...
873// Assuming that symbol table has only types and constants.
874void CWriter::printSymbolTable(const SymbolTable &ST) {
875 // GraphT G;
876 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
877 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
878 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
879
880 // TODO
881 // Need to run through all the used types in the program
882 // FindUsedTypes &FUT = new FindUsedTypes();
883 // const std::set<const Type *> &UsedTypes = FUT.getTypes();
884 // Filter out the structures printing forward definitions for each of them
885 // and creating the dependency graph.
886 // Print forward definitions to all of them
887 // print the typedefs topologically sorted
888
889 // But for now we have
890 for (; I != End; ++I) {
891 const Value *V = I->second;
892 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
893 printConstant(CPV);
894 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
895 string tempostr;
896 string tempstr = "";
897 Out << "typedef ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000898 tempostr = "llvm__" + I->first;
Chris Lattner16c7bb22002-05-09 02:28:59 +0000899 string TypeNameVar = calcTypeNameVar(Ty, TypeNames,
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000900 tempostr, tempstr);
901 Out << TypeNameVar << ";\n";
902 if (!isa<PointerType>(Ty) ||
903 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
904 TypeNames.insert(std::make_pair(Ty, "llvm__"+I->first));
905 }
906 }
907 }
908}
909
910
911// printConstant - Print out a constant pool entry...
912//
913void CWriter::printConstant(const Constant *CPV) {
914 // TODO
915 // Dinakar : Don't know what to do with unnamed constants
916 // should do something about it later.
917
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000918 string tempostr = getValueName(CPV);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000919
920 // Print out the constant type...
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000921 printTypeVar(CPV->getType(), tempostr);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000922
923 Out << " = ";
924 // Write the value out now...
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000925 writeOperand(CPV, Out, false);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000926
927 Out << "\n";
928}
929
Chris Lattner16c7bb22002-05-09 02:28:59 +0000930// printFunctionDecl - Print function declaration
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000931//
Chris Lattner16c7bb22002-05-09 02:28:59 +0000932void CWriter::printFunctionDecl(const Function *F) {
933 printFunctionSignature(F);
934 Out << ";\n";
935}
936
937void CWriter::printFunctionSignature(const Function *F) {
938 if (F->hasInternalLinkage()) Out << "static ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000939
940 // Loop over the arguments, printing them...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000941 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000942
Chris Lattner16c7bb22002-05-09 02:28:59 +0000943 // Print out the return type and name...
944 printType(F->getReturnType(), Out);
Chris Lattner2f499022002-05-09 04:21:21 +0000945 Out << " " << getValueName(F) << "(";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000946
Chris Lattner16c7bb22002-05-09 02:28:59 +0000947 if (!F->isExternal()) {
948 for_each(F->getArgumentList().begin(), F->getArgumentList().end(),
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000949 bind_obj(this, &CWriter::printFunctionArgument));
950 } else {
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000951 // Loop over the arguments, printing them...
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000952 for (FunctionType::ParamTypes::const_iterator I =
Chris Lattner16c7bb22002-05-09 02:28:59 +0000953 FT->getParamTypes().begin(),
954 E = FT->getParamTypes().end(); I != E; ++I) {
955 if (I != FT->getParamTypes().begin()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000956 printType(*I, Out);
957 }
958 }
959
960 // Finish printing arguments...
Chris Lattner16c7bb22002-05-09 02:28:59 +0000961 if (FT->isVarArg()) {
962 if (FT->getParamTypes().size()) Out << ", ";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000963 Out << "..."; // Output varargs portion of signature!
964 }
Chris Lattner16c7bb22002-05-09 02:28:59 +0000965 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000966}
967
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000968
969// printFunctionArgument - This member is called for every argument that
970// is passed into the method. Simply print it out
971//
972void CWriter::printFunctionArgument(const Argument *Arg) {
973 // Insert commas as we go... the first arg doesn't get a comma
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000974 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
975
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000976 // Output type...
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000977 printTypeVar(Arg->getType(), getValueName(Arg));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000978}
979
Chris Lattner16c7bb22002-05-09 02:28:59 +0000980void CWriter::printFunction(const Function *F) {
981 if (F->isExternal()) return;
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000982
Chris Lattnerf34ee812002-05-09 03:12:34 +0000983 Table.incorporateFunction(F);
984
Chris Lattner16c7bb22002-05-09 02:28:59 +0000985 // Process each of the basic blocks, gather information and call the
986 // output methods on the CLocalVars and Function* objects.
987
988 // gather local variable information for each basic block
Chris Lattnerb5af06a2002-05-09 03:06:06 +0000989 InstLocalVarsVisitor ILV(*this);
Chris Lattner16c7bb22002-05-09 02:28:59 +0000990 ILV.visit((Function *)F);
991
992 printFunctionSignature(F);
993 Out << " {\n";
994
995 // Loop over the symbol table, emitting all named constants...
996 if (F->hasSymbolTable())
997 printSymbolTable(*F->getSymbolTable());
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000998
Chris Lattner16c7bb22002-05-09 02:28:59 +0000999 // print the local variables
1000 // we assume that every local variable is alloca'ed in the C code.
1001 std::map<const Type*, VarListType> &locals = ILV.CLV.LocalVars;
1002
1003 map<const Type*, VarListType>::iterator iter;
1004 for (iter = locals.begin(); iter != locals.end(); ++iter) {
1005 VarListType::iterator listiter;
1006 for (listiter = iter->second.begin(); listiter != iter->second.end();
1007 ++listiter) {
1008 Out << " ";
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001009 printTypeVar(iter->first, *listiter);
Chris Lattner16c7bb22002-05-09 02:28:59 +00001010 Out << ";\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001011 }
1012 }
Chris Lattner16c7bb22002-05-09 02:28:59 +00001013
1014 // print the basic blocks
1015 for_each(F->begin(), F->end(), bind_obj(this, &CWriter::outputBasicBlock));
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001016
Chris Lattner16c7bb22002-05-09 02:28:59 +00001017 Out << "}\n";
Chris Lattnerf34ee812002-05-09 03:12:34 +00001018 Table.purgeFunction();
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001019}
1020
1021void CWriter::outputBasicBlock(const BasicBlock* BB) {
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001022 Out << getValueName(BB) << ":\n";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001023
1024 // Output all of the instructions in the basic block...
1025 // print the basic blocks
1026 CInstPrintVisitor CIPV(*this, Table, Out);
1027 CIPV.visit((BasicBlock *) BB);
1028}
1029
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001030// printType - Go to extreme measures to attempt to print out a short, symbolic
1031// version of a type name.
1032ostream& CWriter::printType(const Type *Ty, ostream &Out) {
1033 return printTypeInt(Out, Ty, TypeNames);
1034}
1035
1036
Chris Lattnerb5af06a2002-05-09 03:06:06 +00001037void CWriter::writeOperand(const Value *Operand,
Chris Lattner16c7bb22002-05-09 02:28:59 +00001038 ostream &Out, bool PrintName = true) {
Chris Lattner44408262002-05-09 03:50:42 +00001039 if (isa<GlobalValue>(Operand))
1040 Out << "(&"; // Global values are references as their addresses by llvm
1041
Chris Lattner16c7bb22002-05-09 02:28:59 +00001042 if (PrintName && Operand->hasName()) {
Chris Lattner2f499022002-05-09 04:21:21 +00001043 Out << getValueName(Operand);
Chris Lattner44408262002-05-09 03:50:42 +00001044 } else if (const Constant *CPV = dyn_cast<const Constant>(Operand)) {
Chris Lattner16c7bb22002-05-09 02:28:59 +00001045 if (isa<ConstantPointerNull>(CPV))
1046 Out << "NULL";
1047 else
1048 Out << getConstStrValue(CPV);
Chris Lattner44408262002-05-09 03:50:42 +00001049 } else {
Chris Lattner16c7bb22002-05-09 02:28:59 +00001050 int Slot = Table.getValSlot(Operand);
1051 if (Slot >= 0)
Chris Lattner44408262002-05-09 03:50:42 +00001052 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
Chris Lattner16c7bb22002-05-09 02:28:59 +00001053 else if (PrintName)
1054 Out << "<badref>";
1055 }
Chris Lattner44408262002-05-09 03:50:42 +00001056
1057 if (isa<GlobalValue>(Operand))
1058 Out << ")";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001059}
1060
1061
1062//===----------------------------------------------------------------------===//
1063// External Interface declaration
1064//===----------------------------------------------------------------------===//
1065
Chris Lattnerf34ee812002-05-09 03:12:34 +00001066void WriteToC(const Module *M, ostream &Out) {
1067 assert(M && "You can't write a null module!!");
1068 SlotCalculator SlotTable(M, false);
1069 CWriter W(Out, SlotTable, M);
1070 W.write(M);
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +00001071 Out.flush();
1072}