blob: f20c8044077dc1a472ef81733684ba34e943f6f7 [file] [log] [blame]
Sanjiv Gupta8aae07e2009-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
Sanjiv Guptaac2620e2009-10-15 19:26:25 +000015#include "PIC16ABINames.h"
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +000016#include "PIC16AsmPrinter.h"
Sanjiv Guptaac2620e2009-10-15 19:26:25 +000017#include "PIC16Section.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000018#include "PIC16MCAsmInfo.h"
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +000019#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
21#include "llvm/Module.h"
22#include "llvm/CodeGen/DwarfWriter.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/DwarfWriter.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner73266f92009-08-19 05:49:37 +000026#include "llvm/MC/MCStreamer.h"
Chris Lattnerc6f802d2009-09-13 17:14:04 +000027#include "llvm/MC/MCSymbol.h"
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +000028#include "llvm/Target/TargetRegistry.h"
29#include "llvm/Target/TargetLoweringObjectFile.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/FormattedStream.h"
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +000032#include <cstring>
33using namespace llvm;
34
35#include "PIC16GenAsmWriter.inc"
36
37PIC16AsmPrinter::PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +000038 const MCAsmInfo *T, bool V)
Chris Lattner181c7cb2009-08-22 20:56:12 +000039: AsmPrinter(O, TM, T, V), DbgInfo(O, T) {
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +000040 PTLI = static_cast<PIC16TargetLowering*>(TM.getTargetLowering());
Chris Lattnera5ef4d32009-08-22 21:43:10 +000041 PMAI = static_cast<const PIC16MCAsmInfo*>(T);
Sanjiv Guptaac2620e2009-10-15 19:26:25 +000042 PTOF = (PIC16TargetObjectFile *)&PTLI->getObjFileLowering();
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +000043}
44
45bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Devang Patel5450fc12009-10-06 02:19:11 +000046 processDebugLoc(MI, true);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +000047 printInstruction(MI);
David Greeneca9b04b2009-11-13 21:34:57 +000048 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +000049 EmitComments(*MI);
50 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +000051 processDebugLoc(MI, false);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +000052 return true;
53}
54
Sanjiv Gupta00e8f5a2009-10-21 10:42:44 +000055static int getFunctionColor(const Function *F) {
56 if (F->hasSection()) {
57 std::string Sectn = F->getSection();
58 std::string StrToFind = "Overlay=";
Chris Lattner62b547a2009-10-22 03:42:27 +000059 std::string::size_type Pos = Sectn.find(StrToFind);
Sanjiv Gupta00e8f5a2009-10-21 10:42:44 +000060
61 // Retreive the color number if the key is found.
62 if (Pos != std::string::npos) {
63 Pos += StrToFind.length();
64 std::string Color = "";
65 char c = Sectn.at(Pos);
66 // A Color can only consist of digits.
67 while (c >= '0' && c<= '9') {
68 Color.append(1,c);
69 Pos++;
70 if (Pos >= Sectn.length())
71 break;
72 c = Sectn.at(Pos);
73 }
74 return atoi(Color.c_str());
75 }
76 }
77
78 // Color was not set for function, so return -1.
79 return -1;
80}
81
82// Color the Auto section of the given function.
83void PIC16AsmPrinter::ColorAutoSection(const Function *F) {
Chris Lattner0e9e07a2010-01-16 01:21:04 +000084 std::string SectionName = PAN::getAutosSectionName(CurrentFnSym->getName());
Sanjiv Gupta00e8f5a2009-10-21 10:42:44 +000085 PIC16Section* Section = PTOF->findPIC16Section(SectionName);
86 if (Section != NULL) {
87 int Color = getFunctionColor(F);
88 if (Color >= 0)
89 Section->setColor(Color);
90 }
91}
92
93
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +000094/// runOnMachineFunction - This emits the frame section, autos section and
95/// assembly for each instruction. Also takes care of function begin debug
96/// directive and file begin debug directive (if required) for the function.
97///
98bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +000099 // This calls the base class function required to be called at beginning
100 // of runOnMachineFunction.
101 SetupMachineFunction(MF);
102
Sanjiv Gupta00e8f5a2009-10-21 10:42:44 +0000103 // Put the color information from function to its auto section.
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000104 const Function *F = MF.getFunction();
Sanjiv Gupta00e8f5a2009-10-21 10:42:44 +0000105 ColorAutoSection(F);
106
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000107 // Emit the function frame (args and temps).
108 EmitFunctionFrame(MF);
109
110 DbgInfo.BeginFunction(MF);
111
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000112 // Now emit the instructions of function in its code section.
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000113 const MCSection *fCodeSection
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000114 = getObjFileLowering().SectionForCode(CurrentFnSym->getName());
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000115
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000116 // Start the Code Section.
117 O << "\n";
Chris Lattner73266f92009-08-19 05:49:37 +0000118 OutStreamer.SwitchSection(fCodeSection);
Chris Lattner7f504882009-08-21 23:12:15 +0000119
120 // Emit the frame address of the function at the beginning of code.
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000121 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnSym->getName()) << ")\n";
122 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnSym->getName()) << ")\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000123
124 // Emit function start label.
Chris Lattnerce409842010-01-17 21:43:43 +0000125 O << *CurrentFnSym << ":\n";
Chris Lattner7f504882009-08-21 23:12:15 +0000126
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000127 DebugLoc CurDL;
128 O << "\n";
129 // Print out code for the function.
130 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
131 I != E; ++I) {
132
133 // Print a label for the basic block.
134 if (I != MF.begin()) {
Chris Lattnerda5fb6d2009-09-14 03:15:54 +0000135 EmitBasicBlockStart(I);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000136 }
137
138 // Print a basic block.
139 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
140 II != E; ++II) {
141
142 // Emit the line directive if source line changed.
143 const DebugLoc DL = II->getDebugLoc();
144 if (!DL.isUnknown() && DL != CurDL) {
145 DbgInfo.ChangeDebugLoc(MF, DL);
146 CurDL = DL;
147 }
148
149 // Print the assembly for the instruction.
150 printMachineInstruction(II);
151 }
152 }
153
154 // Emit function end debug directives.
155 DbgInfo.EndFunction(MF);
156
157 return false; // we didn't modify anything.
158}
159
160
161// printOperand - print operand of insn.
162void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
163 const MachineOperand &MO = MI->getOperand(opNum);
164
165 switch (MO.getType()) {
166 case MachineOperand::MO_Register:
Sanjiv Gupta20606ae2009-12-19 08:26:25 +0000167 {
168 // For indirect load/store insns, the fsr name is printed as INDF.
169 std::string RegName = getRegisterName(MO.getReg());
170 if ((MI->getOpcode() == PIC16::load_indirect) ||
171 (MI->getOpcode() == PIC16::store_indirect))
172 {
173 RegName.replace (0, 3, "INDF");
174 }
175 O << RegName;
176 }
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000177 return;
178
179 case MachineOperand::MO_Immediate:
180 O << (int)MO.getImm();
181 return;
182
183 case MachineOperand::MO_GlobalAddress: {
Chris Lattner86974d42010-01-16 01:40:07 +0000184 MCSymbol *Sym = GetGlobalValueSymbol(MO.getGlobal());
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000185 // FIXME: currently we do not have a memcpy def coming in the module
186 // by any chance, as we do not link in those as .bc lib. So these calls
187 // are always external and it is safe to emit an extern.
Chris Lattner86974d42010-01-16 01:40:07 +0000188 if (PAN::isMemIntrinsic(Sym->getName()))
189 LibcallDecls.push_back(createESName(Sym->getName()));
Chris Lattner7f504882009-08-21 23:12:15 +0000190
Chris Lattnerce409842010-01-17 21:43:43 +0000191 O << *Sym;
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000192 break;
193 }
194 case MachineOperand::MO_ExternalSymbol: {
195 const char *Sname = MO.getSymbolName();
196
197 // If its a libcall name, record it to decls section.
Chris Lattner86974d42010-01-16 01:40:07 +0000198 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL)
Chris Lattner7f504882009-08-21 23:12:15 +0000199 LibcallDecls.push_back(Sname);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000200
201 // Record a call to intrinsic to print the extern declaration for it.
202 std::string Sym = Sname;
203 if (PAN::isMemIntrinsic(Sym)) {
204 Sym = PAN::addPrefix(Sym);
205 LibcallDecls.push_back(createESName(Sym));
206 }
Chris Lattner7f504882009-08-21 23:12:15 +0000207
Chris Lattner86974d42010-01-16 01:40:07 +0000208 O << Sym;
Chris Lattner7f504882009-08-21 23:12:15 +0000209 break;
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000210 }
211 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerce409842010-01-17 21:43:43 +0000212 O << *GetMBBSymbol(MO.getMBB()->getNumber());
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000213 return;
214
215 default:
216 llvm_unreachable(" Operand type not supported.");
217 }
218}
219
220/// printCCOperand - Print the cond code operand.
221///
222void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
223 int CC = (int)MI->getOperand(opNum).getImm();
224 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
225}
226
227// This function is used to sort the decls list.
228// should return true if s1 should come before s2.
229static bool is_before(const char *s1, const char *s2) {
230 return strcmp(s1, s2) <= 0;
231}
232
233// This is used by list::unique below.
234// unique will filter out duplicates if it knows them.
235static bool is_duplicate(const char *s1, const char *s2) {
236 return !strcmp(s1, s2);
237}
238
239/// printLibcallDecls - print the extern declarations for compiler
240/// intrinsics.
241///
242void PIC16AsmPrinter::printLibcallDecls() {
243 // If no libcalls used, return.
244 if (LibcallDecls.empty()) return;
245
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000246 O << MAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000247 // Remove duplicate entries.
248 LibcallDecls.sort(is_before);
249 LibcallDecls.unique(is_duplicate);
250
251 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
252 I != LibcallDecls.end(); I++) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000253 O << MAI->getExternDirective() << *I << "\n";
254 O << MAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
255 O << MAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000256 }
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000257 O << MAI->getCommentString() << "External decls for libcalls - END." <<"\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000258}
259
Bob Wilson4901f432009-09-30 21:44:42 +0000260/// doInitialization - Perform Module level initializations here.
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000261/// One task that we do here is to sectionize all global variables.
262/// The MemSelOptimizer pass depends on the sectionizing.
263///
264bool PIC16AsmPrinter::doInitialization(Module &M) {
265 bool Result = AsmPrinter::doInitialization(M);
266
Sanjiv Gupta20606ae2009-12-19 08:26:25 +0000267 // Every asmbly contains these std headers.
268 O << "\n#include p16f1xxx.inc";
269 O << "\n#include stdmacros.inc";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000270
271 // Set the section names for all globals.
272 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000273 I != E; ++I) {
274
275 // Record External Var Decls.
276 if (I->isDeclaration()) {
277 ExternalVarDecls.push_back(I);
278 continue;
279 }
280
281 // Record Exteranl Var Defs.
282 if (I->hasExternalLinkage() || I->hasCommonLinkage()) {
283 ExternalVarDefs.push_back(I);
284 }
285
286 // Sectionify actual data.
287 if (!I->hasAvailableExternallyLinkage()) {
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000288 const MCSection *S = getObjFileLowering().SectionForGlobal(I, Mang, TM);
289
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000290 I->setSection(((const PIC16Section *)S)->getName());
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000291 }
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000292 }
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000293
294 DbgInfo.BeginModule(M);
295 EmitFunctionDecls(M);
296 EmitUndefinedVars(M);
297 EmitDefinedVars(M);
298 EmitIData(M);
299 EmitUData(M);
300 EmitRomData(M);
Sanjiv Gupta27e801c2009-10-25 08:14:11 +0000301 EmitSharedUdata(M);
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000302 EmitUserSections(M);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000303 return Result;
304}
305
306/// Emit extern decls for functions imported from other modules, and emit
307/// global declarations for function defined in this module and which are
308/// available to other modules.
309///
310void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
311 // Emit declarations for external functions.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000312 O <<"\n"<<MAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000313 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
Chris Lattner86974d42010-01-16 01:40:07 +0000314 if (I->isIntrinsic() || I->getName() == "@abort")
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000315 continue;
316
317 if (!I->isDeclaration() && !I->hasExternalLinkage())
318 continue;
319
Chris Lattner86974d42010-01-16 01:40:07 +0000320 MCSymbol *Sym = GetGlobalValueSymbol(I);
321
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000322 // Do not emit memcpy, memset, and memmove here.
323 // Calls to these routines can be generated in two ways,
324 // 1. User calling the standard lib function
325 // 2. Codegen generating these calls for llvm intrinsics.
326 // In the first case a prototype is alread availale, while in
327 // second case the call is via and externalsym and the prototype is missing.
328 // So declarations for these are currently always getting printing by
329 // tracking both kind of references in printInstrunction.
Chris Lattner86974d42010-01-16 01:40:07 +0000330 if (I->isDeclaration() && PAN::isMemIntrinsic(Sym->getName())) continue;
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000331
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000332 const char *directive = I->isDeclaration() ? MAI->getExternDirective() :
333 MAI->getGlobalDirective();
Chris Lattner7f504882009-08-21 23:12:15 +0000334
Chris Lattner86974d42010-01-16 01:40:07 +0000335 O << directive << Sym->getName() << "\n";
336 O << directive << PAN::getRetvalLabel(Sym->getName()) << "\n";
337 O << directive << PAN::getArgsLabel(Sym->getName()) << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000338 }
339
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000340 O << MAI->getCommentString() << "Function Declarations - END." <<"\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000341}
342
343// Emit variables imported from other Modules.
344void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000345 std::vector<const GlobalVariable*> Items = ExternalVarDecls;
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000346 if (!Items.size()) return;
347
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000348 O << "\n" << MAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
Chris Lattnerce409842010-01-17 21:43:43 +0000349 for (unsigned j = 0; j < Items.size(); j++)
350 O << MAI->getExternDirective() << *GetGlobalValueSymbol(Items[j]) << "\n";
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000351 O << MAI->getCommentString() << "Imported Variables - END" << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000352}
353
354// Emit variables defined in this module and are available to other modules.
355void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000356 std::vector<const GlobalVariable*> Items = ExternalVarDefs;
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000357 if (!Items.size()) return;
358
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000359 O << "\n" << MAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
Chris Lattnerce409842010-01-17 21:43:43 +0000360 for (unsigned j = 0; j < Items.size(); j++)
361 O << MAI->getGlobalDirective() << *GetGlobalValueSymbol(Items[j]) << "\n";
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000362 O << MAI->getCommentString() << "Exported Variables - END" << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000363}
364
365// Emit initialized data placed in ROM.
366void PIC16AsmPrinter::EmitRomData(Module &M) {
Sanjiv Gupta018db662009-10-16 08:58:34 +0000367 EmitSingleSection(PTOF->ROMDATASection());
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000368}
369
Sanjiv Gupta27e801c2009-10-25 08:14:11 +0000370// Emit Shared section udata.
371void PIC16AsmPrinter::EmitSharedUdata(Module &M) {
372 EmitSingleSection(PTOF->SHAREDUDATASection());
373}
374
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000375bool PIC16AsmPrinter::doFinalization(Module &M) {
Sanjiv Gupta00e8f5a2009-10-21 10:42:44 +0000376 EmitAllAutos(M);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000377 printLibcallDecls();
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000378 DbgInfo.EndModule(M);
379 O << "\n\t" << "END\n";
380 return AsmPrinter::doFinalization(M);
381}
382
383void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
384 const Function *F = MF.getFunction();
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000385 const TargetData *TD = TM.getTargetData();
386 // Emit the data section name.
387 O << "\n";
Chris Lattner08e63732009-08-21 23:08:09 +0000388
Chris Lattnerc0c8d7d2010-01-16 01:17:26 +0000389 PIC16Section *fPDataSection =
390 const_cast<PIC16Section *>(getObjFileLowering().
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000391 SectionForFrame(CurrentFnSym->getName()));
Sanjiv Gupta00e8f5a2009-10-21 10:42:44 +0000392
393 fPDataSection->setColor(getFunctionColor(F));
Chris Lattner73266f92009-08-19 05:49:37 +0000394 OutStreamer.SwitchSection(fPDataSection);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000395
396 // Emit function frame label
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000397 O << PAN::getFrameLabel(CurrentFnSym->getName()) << ":\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000398
399 const Type *RetType = F->getReturnType();
400 unsigned RetSize = 0;
401 if (RetType->getTypeID() != Type::VoidTyID)
402 RetSize = TD->getTypeAllocSize(RetType);
403
404 //Emit function return value space
405 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
406 // we will need to avoid printing a global directive for Retval label
407 // in emitExternandGloblas.
408 if(RetSize > 0)
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000409 O << PAN::getRetvalLabel(CurrentFnSym->getName())
410 << " RES " << RetSize << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000411 else
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000412 O << PAN::getRetvalLabel(CurrentFnSym->getName()) << ": \n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000413
414 // Emit variable to hold the space for function arguments
415 unsigned ArgSize = 0;
416 for (Function::const_arg_iterator argi = F->arg_begin(),
417 arge = F->arg_end(); argi != arge ; ++argi) {
418 const Type *Ty = argi->getType();
419 ArgSize += TD->getTypeAllocSize(Ty);
420 }
421
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000422 O << PAN::getArgsLabel(CurrentFnSym->getName()) << " RES " << ArgSize << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000423
424 // Emit temporary space
425 int TempSize = PTLI->GetTmpSize();
426 if (TempSize > 0)
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000427 O << PAN::getTempdataLabel(CurrentFnSym->getName()) << " RES "
428 << TempSize << '\n';
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000429}
430
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000431
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000432void PIC16AsmPrinter::EmitInitializedDataSection(const PIC16Section *S) {
433 /// Emit Section header.
434 OutStreamer.SwitchSection(S);
435
436 std::vector<const GlobalVariable*> Items = S->Items;
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000437 for (unsigned j = 0; j < Items.size(); j++) {
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000438 Constant *C = Items[j]->getInitializer();
439 int AddrSpace = Items[j]->getType()->getAddressSpace();
Chris Lattnerce409842010-01-17 21:43:43 +0000440 O << *GetGlobalValueSymbol(Items[j]);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000441 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000442 }
Sanjiv Gupta1c138172009-10-15 10:10:43 +0000443}
444
Sanjiv Gupta018db662009-10-16 08:58:34 +0000445// Print all IDATA sections.
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000446void PIC16AsmPrinter::EmitIData(Module &M) {
Sanjiv Gupta018db662009-10-16 08:58:34 +0000447 EmitSectionList (M, PTOF->IDATASections());
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000448}
449
Sanjiv Gupta018db662009-10-16 08:58:34 +0000450void PIC16AsmPrinter::
451EmitUninitializedDataSection(const PIC16Section *S) {
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000452 const TargetData *TD = TM.getTargetData();
453 OutStreamer.SwitchSection(S);
454 std::vector<const GlobalVariable*> Items = S->Items;
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000455 for (unsigned j = 0; j < Items.size(); j++) {
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000456 Constant *C = Items[j]->getInitializer();
457 const Type *Ty = C->getType();
458 unsigned Size = TD->getTypeAllocSize(Ty);
Chris Lattnerce409842010-01-17 21:43:43 +0000459 O << *GetGlobalValueSymbol(Items[j]) << " RES " << Size << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000460 }
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000461}
462
Sanjiv Gupta018db662009-10-16 08:58:34 +0000463// Print all UDATA sections.
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000464void PIC16AsmPrinter::EmitUData(Module &M) {
Sanjiv Gupta018db662009-10-16 08:58:34 +0000465 EmitSectionList (M, PTOF->UDATASections());
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000466}
467
Sanjiv Gupta018db662009-10-16 08:58:34 +0000468// Print all USER sections.
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000469void PIC16AsmPrinter::EmitUserSections(Module &M) {
Sanjiv Gupta018db662009-10-16 08:58:34 +0000470 EmitSectionList (M, PTOF->USERSections());
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000471}
472
Sanjiv Gupta018db662009-10-16 08:58:34 +0000473// Print all AUTO sections.
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000474void PIC16AsmPrinter::EmitAllAutos(Module &M) {
Sanjiv Gupta018db662009-10-16 08:58:34 +0000475 EmitSectionList (M, PTOF->AUTOSections());
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000476}
477
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000478extern "C" void LLVMInitializePIC16AsmPrinter() {
479 RegisterAsmPrinter<PIC16AsmPrinter> X(ThePIC16Target);
480}
481
Sanjiv Gupta018db662009-10-16 08:58:34 +0000482// Emit one data section using correct section emitter based on section type.
483void PIC16AsmPrinter::EmitSingleSection(const PIC16Section *S) {
484 if (S == NULL) return;
485
486 switch (S->getType()) {
487 default: llvm_unreachable ("unknow user section type");
488 case UDATA:
489 case UDATA_SHR:
490 case UDATA_OVR:
491 EmitUninitializedDataSection(S);
492 break;
493 case IDATA:
494 case ROMDATA:
495 EmitInitializedDataSection(S);
496 break;
497 }
498}
499
500// Emit a list of sections.
501void PIC16AsmPrinter::
502EmitSectionList(Module &M, const std::vector<PIC16Section *> &SList) {
503 for (unsigned i = 0; i < SList.size(); i++) {
504 // Exclude llvm specific metadata sections.
505 if (SList[i]->getName().find("llvm.") != std::string::npos)
506 continue;
507 O << "\n";
508 EmitSingleSection(SList[i]);
509 }
510}
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000511