blob: f9b8044c4a013a612e48566259ffb3dde3001ba0 [file] [log] [blame]
Sanjiv Gupta27806092009-08-13 16:37:05 +00001//===-- PIC16AsmPrinter.cpp - PIC16 LLVM assembly writer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to PIC16 assembly language.
12//
13//===----------------------------------------------------------------------===//
14
15#include "PIC16AsmPrinter.h"
Chris Lattnerc0776212009-08-15 06:13:40 +000016#include "MCSectionPIC16.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000017#include "PIC16MCAsmInfo.h"
Sanjiv Gupta27806092009-08-13 16:37:05 +000018#include "llvm/DerivedTypes.h"
19#include "llvm/Function.h"
20#include "llvm/Module.h"
21#include "llvm/CodeGen/DwarfWriter.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/DwarfWriter.h"
24#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000025#include "llvm/MC/MCStreamer.h"
Sanjiv Gupta27806092009-08-13 16:37:05 +000026#include "llvm/Target/TargetRegistry.h"
27#include "llvm/Target/TargetLoweringObjectFile.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/FormattedStream.h"
30#include "llvm/Support/Mangler.h"
31#include <cstring>
32using namespace llvm;
33
34#include "PIC16GenAsmWriter.inc"
35
36PIC16AsmPrinter::PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattneraf76e592009-08-22 20:48:53 +000037 const MCAsmInfo *T, bool V)
Chris Lattnere4d110b2009-08-22 20:56:12 +000038: AsmPrinter(O, TM, T, V), DbgInfo(O, T) {
Sanjiv Gupta27806092009-08-13 16:37:05 +000039 PTLI = static_cast<PIC16TargetLowering*>(TM.getTargetLowering());
Chris Lattner33adcfb2009-08-22 21:43:10 +000040 PMAI = static_cast<const PIC16MCAsmInfo*>(T);
Sanjiv Gupta27806092009-08-13 16:37:05 +000041 PTOF = (PIC16TargetObjectFile*)&PTLI->getObjFileLowering();
42}
43
44bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerc5ea2632009-09-09 23:14:36 +000045 processDebugLoc(MI->getDebugLoc());
46
Sanjiv Gupta27806092009-08-13 16:37:05 +000047 printInstruction(MI);
Chris Lattnerc5ea2632009-09-09 23:14:36 +000048
49 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
50 EmitComments(*MI);
51 O << '\n';
Sanjiv Gupta27806092009-08-13 16:37:05 +000052 return true;
53}
54
55/// runOnMachineFunction - This emits the frame section, autos section and
56/// assembly for each instruction. Also takes care of function begin debug
57/// directive and file begin debug directive (if required) for the function.
58///
59bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
60 this->MF = &MF;
61
62 // This calls the base class function required to be called at beginning
63 // of runOnMachineFunction.
64 SetupMachineFunction(MF);
65
66 // Get the mangled name.
67 const Function *F = MF.getFunction();
68 CurrentFnName = Mang->getMangledName(F);
69
70 // Emit the function frame (args and temps).
71 EmitFunctionFrame(MF);
72
73 DbgInfo.BeginFunction(MF);
74
75 // Emit the autos section of function.
Chris Lattnerec3579f2009-08-21 23:08:09 +000076 EmitAutos(CurrentFnName);
Sanjiv Gupta27806092009-08-13 16:37:05 +000077
78 // Now emit the instructions of function in its code section.
79 const MCSection *fCodeSection =
Chris Lattner35a27c82009-08-21 23:12:15 +000080 getObjFileLowering().getSectionForFunction(CurrentFnName);
Sanjiv Gupta27806092009-08-13 16:37:05 +000081 // Start the Code Section.
82 O << "\n";
Chris Lattner6c2f9e12009-08-19 05:49:37 +000083 OutStreamer.SwitchSection(fCodeSection);
Chris Lattner35a27c82009-08-21 23:12:15 +000084
85 // Emit the frame address of the function at the beginning of code.
86 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
87 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
Sanjiv Gupta27806092009-08-13 16:37:05 +000088
89 // Emit function start label.
90 O << CurrentFnName << ":\n";
Chris Lattner35a27c82009-08-21 23:12:15 +000091
Sanjiv Gupta27806092009-08-13 16:37:05 +000092 DebugLoc CurDL;
93 O << "\n";
94 // Print out code for the function.
95 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
96 I != E; ++I) {
97
98 // Print a label for the basic block.
99 if (I != MF.begin()) {
100 printBasicBlockLabel(I, true);
101 O << '\n';
102 }
103
104 // Print a basic block.
105 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
106 II != E; ++II) {
107
108 // Emit the line directive if source line changed.
109 const DebugLoc DL = II->getDebugLoc();
110 if (!DL.isUnknown() && DL != CurDL) {
111 DbgInfo.ChangeDebugLoc(MF, DL);
112 CurDL = DL;
113 }
114
115 // Print the assembly for the instruction.
116 printMachineInstruction(II);
117 }
118 }
119
120 // Emit function end debug directives.
121 DbgInfo.EndFunction(MF);
122
123 return false; // we didn't modify anything.
124}
125
126
127// printOperand - print operand of insn.
128void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
129 const MachineOperand &MO = MI->getOperand(opNum);
130
131 switch (MO.getType()) {
132 case MachineOperand::MO_Register:
133 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
134 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
135 else
136 llvm_unreachable("not implemented");
137 return;
138
139 case MachineOperand::MO_Immediate:
140 O << (int)MO.getImm();
141 return;
142
143 case MachineOperand::MO_GlobalAddress: {
144 std::string Sname = Mang->getMangledName(MO.getGlobal());
145 // FIXME: currently we do not have a memcpy def coming in the module
146 // by any chance, as we do not link in those as .bc lib. So these calls
147 // are always external and it is safe to emit an extern.
148 if (PAN::isMemIntrinsic(Sname)) {
149 LibcallDecls.push_back(createESName(Sname));
150 }
Chris Lattner35a27c82009-08-21 23:12:15 +0000151
Sanjiv Gupta27806092009-08-13 16:37:05 +0000152 O << Sname;
153 break;
154 }
155 case MachineOperand::MO_ExternalSymbol: {
156 const char *Sname = MO.getSymbolName();
157
158 // If its a libcall name, record it to decls section.
159 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) {
Chris Lattner35a27c82009-08-21 23:12:15 +0000160 LibcallDecls.push_back(Sname);
Sanjiv Gupta27806092009-08-13 16:37:05 +0000161 }
162
163 // Record a call to intrinsic to print the extern declaration for it.
164 std::string Sym = Sname;
165 if (PAN::isMemIntrinsic(Sym)) {
166 Sym = PAN::addPrefix(Sym);
167 LibcallDecls.push_back(createESName(Sym));
168 }
Chris Lattner35a27c82009-08-21 23:12:15 +0000169
170 O << Sym;
171 break;
Sanjiv Gupta27806092009-08-13 16:37:05 +0000172 }
173 case MachineOperand::MO_MachineBasicBlock:
174 printBasicBlockLabel(MO.getMBB());
175 return;
176
177 default:
178 llvm_unreachable(" Operand type not supported.");
179 }
180}
181
182/// printCCOperand - Print the cond code operand.
183///
184void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
185 int CC = (int)MI->getOperand(opNum).getImm();
186 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
187}
188
189// This function is used to sort the decls list.
190// should return true if s1 should come before s2.
191static bool is_before(const char *s1, const char *s2) {
192 return strcmp(s1, s2) <= 0;
193}
194
195// This is used by list::unique below.
196// unique will filter out duplicates if it knows them.
197static bool is_duplicate(const char *s1, const char *s2) {
198 return !strcmp(s1, s2);
199}
200
201/// printLibcallDecls - print the extern declarations for compiler
202/// intrinsics.
203///
204void PIC16AsmPrinter::printLibcallDecls() {
205 // If no libcalls used, return.
206 if (LibcallDecls.empty()) return;
207
Chris Lattner33adcfb2009-08-22 21:43:10 +0000208 O << MAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
Sanjiv Gupta27806092009-08-13 16:37:05 +0000209 // Remove duplicate entries.
210 LibcallDecls.sort(is_before);
211 LibcallDecls.unique(is_duplicate);
212
213 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
214 I != LibcallDecls.end(); I++) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000215 O << MAI->getExternDirective() << *I << "\n";
216 O << MAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
217 O << MAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
Sanjiv Gupta27806092009-08-13 16:37:05 +0000218 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000219 O << MAI->getCommentString() << "External decls for libcalls - END." <<"\n";
Sanjiv Gupta27806092009-08-13 16:37:05 +0000220}
221
222/// doInitialization - Perfrom Module level initializations here.
223/// One task that we do here is to sectionize all global variables.
224/// The MemSelOptimizer pass depends on the sectionizing.
225///
226bool PIC16AsmPrinter::doInitialization(Module &M) {
227 bool Result = AsmPrinter::doInitialization(M);
228
229 // FIXME:: This is temporary solution to generate the include file.
230 // The processor should be passed to llc as in input and the header file
231 // should be generated accordingly.
232 O << "\n\t#include P16F1937.INC\n";
233
234 // Set the section names for all globals.
235 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
236 I != E; ++I)
237 if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) {
238 const MCSection *S = getObjFileLowering().SectionForGlobal(I, Mang, TM);
239
240 I->setSection(((const MCSectionPIC16*)S)->getName());
241 }
242
243 DbgInfo.BeginModule(M);
244 EmitFunctionDecls(M);
245 EmitUndefinedVars(M);
246 EmitDefinedVars(M);
247 EmitIData(M);
248 EmitUData(M);
249 EmitRomData(M);
250 return Result;
251}
252
253/// Emit extern decls for functions imported from other modules, and emit
254/// global declarations for function defined in this module and which are
255/// available to other modules.
256///
257void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
258 // Emit declarations for external functions.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000259 O <<"\n"<<MAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
Sanjiv Gupta27806092009-08-13 16:37:05 +0000260 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
261 if (I->isIntrinsic())
262 continue;
263
264 std::string Name = Mang->getMangledName(I);
265 if (Name.compare("@abort") == 0)
266 continue;
267
268 if (!I->isDeclaration() && !I->hasExternalLinkage())
269 continue;
270
271 // Do not emit memcpy, memset, and memmove here.
272 // Calls to these routines can be generated in two ways,
273 // 1. User calling the standard lib function
274 // 2. Codegen generating these calls for llvm intrinsics.
275 // In the first case a prototype is alread availale, while in
276 // second case the call is via and externalsym and the prototype is missing.
277 // So declarations for these are currently always getting printing by
278 // tracking both kind of references in printInstrunction.
279 if (I->isDeclaration() && PAN::isMemIntrinsic(Name)) continue;
280
Chris Lattner33adcfb2009-08-22 21:43:10 +0000281 const char *directive = I->isDeclaration() ? MAI->getExternDirective() :
282 MAI->getGlobalDirective();
Chris Lattner35a27c82009-08-21 23:12:15 +0000283
Sanjiv Gupta27806092009-08-13 16:37:05 +0000284 O << directive << Name << "\n";
285 O << directive << PAN::getRetvalLabel(Name) << "\n";
286 O << directive << PAN::getArgsLabel(Name) << "\n";
287 }
288
Chris Lattner33adcfb2009-08-22 21:43:10 +0000289 O << MAI->getCommentString() << "Function Declarations - END." <<"\n";
Sanjiv Gupta27806092009-08-13 16:37:05 +0000290}
291
292// Emit variables imported from other Modules.
293void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
294 std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDecls->Items;
295 if (!Items.size()) return;
296
Chris Lattner33adcfb2009-08-22 21:43:10 +0000297 O << "\n" << MAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
Sanjiv Gupta27806092009-08-13 16:37:05 +0000298 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000299 O << MAI->getExternDirective() << Mang->getMangledName(Items[j]) << "\n";
Sanjiv Gupta27806092009-08-13 16:37:05 +0000300 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000301 O << MAI->getCommentString() << "Imported Variables - END" << "\n";
Sanjiv Gupta27806092009-08-13 16:37:05 +0000302}
303
304// Emit variables defined in this module and are available to other modules.
305void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
306 std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDefs->Items;
307 if (!Items.size()) return;
308
Chris Lattner33adcfb2009-08-22 21:43:10 +0000309 O << "\n" << MAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
Sanjiv Gupta27806092009-08-13 16:37:05 +0000310 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000311 O << MAI->getGlobalDirective() << Mang->getMangledName(Items[j]) << "\n";
Sanjiv Gupta27806092009-08-13 16:37:05 +0000312 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000313 O << MAI->getCommentString() << "Exported Variables - END" << "\n";
Sanjiv Gupta27806092009-08-13 16:37:05 +0000314}
315
316// Emit initialized data placed in ROM.
317void PIC16AsmPrinter::EmitRomData(Module &M) {
318 // Print ROM Data section.
319 const std::vector<PIC16Section*> &ROSections = PTOF->ROSections;
320 for (unsigned i = 0; i < ROSections.size(); i++) {
321 const std::vector<const GlobalVariable*> &Items = ROSections[i]->Items;
322 if (!Items.size()) continue;
323 O << "\n";
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000324 OutStreamer.SwitchSection(PTOF->ROSections[i]->S_);
Sanjiv Gupta27806092009-08-13 16:37:05 +0000325 for (unsigned j = 0; j < Items.size(); j++) {
326 O << Mang->getMangledName(Items[j]);
327 Constant *C = Items[j]->getInitializer();
328 int AddrSpace = Items[j]->getType()->getAddressSpace();
329 EmitGlobalConstant(C, AddrSpace);
330 }
331 }
332}
333
334bool PIC16AsmPrinter::doFinalization(Module &M) {
335 printLibcallDecls();
336 EmitRemainingAutos();
337 DbgInfo.EndModule(M);
338 O << "\n\t" << "END\n";
339 return AsmPrinter::doFinalization(M);
340}
341
342void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
343 const Function *F = MF.getFunction();
344 std::string FuncName = Mang->getMangledName(F);
345 const TargetData *TD = TM.getTargetData();
346 // Emit the data section name.
347 O << "\n";
Chris Lattnerec3579f2009-08-21 23:08:09 +0000348
Sanjiv Gupta27806092009-08-13 16:37:05 +0000349 const MCSection *fPDataSection =
Chris Lattnerec3579f2009-08-21 23:08:09 +0000350 getObjFileLowering().getSectionForFunctionFrame(CurrentFnName);
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000351 OutStreamer.SwitchSection(fPDataSection);
Sanjiv Gupta27806092009-08-13 16:37:05 +0000352
353 // Emit function frame label
354 O << PAN::getFrameLabel(CurrentFnName) << ":\n";
355
356 const Type *RetType = F->getReturnType();
357 unsigned RetSize = 0;
358 if (RetType->getTypeID() != Type::VoidTyID)
359 RetSize = TD->getTypeAllocSize(RetType);
360
361 //Emit function return value space
362 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
363 // we will need to avoid printing a global directive for Retval label
364 // in emitExternandGloblas.
365 if(RetSize > 0)
366 O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n";
367 else
368 O << PAN::getRetvalLabel(CurrentFnName) << ": \n";
369
370 // Emit variable to hold the space for function arguments
371 unsigned ArgSize = 0;
372 for (Function::const_arg_iterator argi = F->arg_begin(),
373 arge = F->arg_end(); argi != arge ; ++argi) {
374 const Type *Ty = argi->getType();
375 ArgSize += TD->getTypeAllocSize(Ty);
376 }
377
378 O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n";
379
380 // Emit temporary space
381 int TempSize = PTLI->GetTmpSize();
382 if (TempSize > 0)
383 O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize << '\n';
384}
385
386void PIC16AsmPrinter::EmitIData(Module &M) {
387
388 // Print all IDATA sections.
389 const std::vector<PIC16Section*> &IDATASections = PTOF->IDATASections;
390 for (unsigned i = 0; i < IDATASections.size(); i++) {
391 O << "\n";
392 if (IDATASections[i]->S_->getName().find("llvm.") != std::string::npos)
393 continue;
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000394 OutStreamer.SwitchSection(IDATASections[i]->S_);
Sanjiv Gupta27806092009-08-13 16:37:05 +0000395 std::vector<const GlobalVariable*> Items = IDATASections[i]->Items;
396 for (unsigned j = 0; j < Items.size(); j++) {
397 std::string Name = Mang->getMangledName(Items[j]);
398 Constant *C = Items[j]->getInitializer();
399 int AddrSpace = Items[j]->getType()->getAddressSpace();
400 O << Name;
401 EmitGlobalConstant(C, AddrSpace);
402 }
403 }
404}
405
406void PIC16AsmPrinter::EmitUData(Module &M) {
407 const TargetData *TD = TM.getTargetData();
408
409 // Print all BSS sections.
410 const std::vector<PIC16Section*> &BSSSections = PTOF->BSSSections;
411 for (unsigned i = 0; i < BSSSections.size(); i++) {
412 O << "\n";
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000413 OutStreamer.SwitchSection(BSSSections[i]->S_);
Sanjiv Gupta27806092009-08-13 16:37:05 +0000414 std::vector<const GlobalVariable*> Items = BSSSections[i]->Items;
415 for (unsigned j = 0; j < Items.size(); j++) {
416 std::string Name = Mang->getMangledName(Items[j]);
417 Constant *C = Items[j]->getInitializer();
418 const Type *Ty = C->getType();
419 unsigned Size = TD->getTypeAllocSize(Ty);
420
421 O << Name << " RES " << Size << "\n";
422 }
423 }
424}
425
Chris Lattnerec3579f2009-08-21 23:08:09 +0000426void PIC16AsmPrinter::EmitAutos(std::string FunctName) {
Sanjiv Gupta27806092009-08-13 16:37:05 +0000427 // Section names for all globals are already set.
428 const TargetData *TD = TM.getTargetData();
429
430 // Now print Autos section for this function.
Chris Lattnerec3579f2009-08-21 23:08:09 +0000431 std::string SectionName = PAN::getAutosSectionName(FunctName);
Sanjiv Gupta27806092009-08-13 16:37:05 +0000432 const std::vector<PIC16Section*> &AutosSections = PTOF->AutosSections;
433 for (unsigned i = 0; i < AutosSections.size(); i++) {
434 O << "\n";
435 if (AutosSections[i]->S_->getName() == SectionName) {
436 // Set the printing status to true
437 AutosSections[i]->setPrintedStatus(true);
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000438 OutStreamer.SwitchSection(AutosSections[i]->S_);
Sanjiv Gupta27806092009-08-13 16:37:05 +0000439 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
440 for (unsigned j = 0; j < Items.size(); j++) {
441 std::string VarName = Mang->getMangledName(Items[j]);
442 Constant *C = Items[j]->getInitializer();
443 const Type *Ty = C->getType();
444 unsigned Size = TD->getTypeAllocSize(Ty);
445 // Emit memory reserve directive.
446 O << VarName << " RES " << Size << "\n";
447 }
448 break;
449 }
450 }
451}
452
453// Print autos that were not printed during the code printing of functions.
454// As the functions might themselves would have got deleted by the optimizer.
455void PIC16AsmPrinter::EmitRemainingAutos() {
456 const TargetData *TD = TM.getTargetData();
457
458 // Now print Autos section for this function.
459 std::vector <PIC16Section *>AutosSections = PTOF->AutosSections;
460 for (unsigned i = 0; i < AutosSections.size(); i++) {
461
462 // if the section is already printed then don't print again
463 if (AutosSections[i]->isPrinted())
464 continue;
465
466 // Set status as printed
467 AutosSections[i]->setPrintedStatus(true);
468
469 O << "\n";
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000470 OutStreamer.SwitchSection(AutosSections[i]->S_);
Sanjiv Gupta27806092009-08-13 16:37:05 +0000471 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
472 for (unsigned j = 0; j < Items.size(); j++) {
473 std::string VarName = Mang->getMangledName(Items[j]);
474 Constant *C = Items[j]->getInitializer();
475 const Type *Ty = C->getType();
476 unsigned Size = TD->getTypeAllocSize(Ty);
477 // Emit memory reserve directive.
478 O << VarName << " RES " << Size << "\n";
479 }
480 }
481}
482
483
484extern "C" void LLVMInitializePIC16AsmPrinter() {
485 RegisterAsmPrinter<PIC16AsmPrinter> X(ThePIC16Target);
486}
487
488