blob: af2169ef52092cdde89758427f00aac43e62fe68 [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) {
99 this->MF = &MF;
100
101 // This calls the base class function required to be called at beginning
102 // of runOnMachineFunction.
103 SetupMachineFunction(MF);
104
Sanjiv Gupta00e8f5a2009-10-21 10:42:44 +0000105 // Put the color information from function to its auto section.
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000106 const Function *F = MF.getFunction();
Sanjiv Gupta00e8f5a2009-10-21 10:42:44 +0000107 ColorAutoSection(F);
108
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000109 // Emit the function frame (args and temps).
110 EmitFunctionFrame(MF);
111
112 DbgInfo.BeginFunction(MF);
113
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000114 // Now emit the instructions of function in its code section.
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000115 const MCSection *fCodeSection
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000116 = getObjFileLowering().SectionForCode(CurrentFnSym->getName());
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000117
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000118 // Start the Code Section.
119 O << "\n";
Chris Lattner73266f92009-08-19 05:49:37 +0000120 OutStreamer.SwitchSection(fCodeSection);
Chris Lattner7f504882009-08-21 23:12:15 +0000121
122 // Emit the frame address of the function at the beginning of code.
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000123 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnSym->getName()) << ")\n";
124 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnSym->getName()) << ")\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000125
126 // Emit function start label.
Chris Lattnerc0c8d7d2010-01-16 01:17:26 +0000127 CurrentFnSym->print(O, MAI);
128 O << ":\n";
Chris Lattner7f504882009-08-21 23:12:15 +0000129
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000130 DebugLoc CurDL;
131 O << "\n";
132 // Print out code for the function.
133 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
134 I != E; ++I) {
135
136 // Print a label for the basic block.
137 if (I != MF.begin()) {
Chris Lattnerda5fb6d2009-09-14 03:15:54 +0000138 EmitBasicBlockStart(I);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000139 }
140
141 // Print a basic block.
142 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
143 II != E; ++II) {
144
145 // Emit the line directive if source line changed.
146 const DebugLoc DL = II->getDebugLoc();
147 if (!DL.isUnknown() && DL != CurDL) {
148 DbgInfo.ChangeDebugLoc(MF, DL);
149 CurDL = DL;
150 }
151
152 // Print the assembly for the instruction.
153 printMachineInstruction(II);
154 }
155 }
156
157 // Emit function end debug directives.
158 DbgInfo.EndFunction(MF);
159
160 return false; // we didn't modify anything.
161}
162
163
164// printOperand - print operand of insn.
165void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
166 const MachineOperand &MO = MI->getOperand(opNum);
167
168 switch (MO.getType()) {
169 case MachineOperand::MO_Register:
Sanjiv Gupta20606ae2009-12-19 08:26:25 +0000170 {
171 // For indirect load/store insns, the fsr name is printed as INDF.
172 std::string RegName = getRegisterName(MO.getReg());
173 if ((MI->getOpcode() == PIC16::load_indirect) ||
174 (MI->getOpcode() == PIC16::store_indirect))
175 {
176 RegName.replace (0, 3, "INDF");
177 }
178 O << RegName;
179 }
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000180 return;
181
182 case MachineOperand::MO_Immediate:
183 O << (int)MO.getImm();
184 return;
185
186 case MachineOperand::MO_GlobalAddress: {
Chris Lattner86974d42010-01-16 01:40:07 +0000187 MCSymbol *Sym = GetGlobalValueSymbol(MO.getGlobal());
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000188 // FIXME: currently we do not have a memcpy def coming in the module
189 // by any chance, as we do not link in those as .bc lib. So these calls
190 // are always external and it is safe to emit an extern.
Chris Lattner86974d42010-01-16 01:40:07 +0000191 if (PAN::isMemIntrinsic(Sym->getName()))
192 LibcallDecls.push_back(createESName(Sym->getName()));
Chris Lattner7f504882009-08-21 23:12:15 +0000193
Chris Lattner86974d42010-01-16 01:40:07 +0000194 Sym->print(O, MAI);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000195 break;
196 }
197 case MachineOperand::MO_ExternalSymbol: {
198 const char *Sname = MO.getSymbolName();
199
200 // If its a libcall name, record it to decls section.
Chris Lattner86974d42010-01-16 01:40:07 +0000201 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL)
Chris Lattner7f504882009-08-21 23:12:15 +0000202 LibcallDecls.push_back(Sname);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000203
204 // Record a call to intrinsic to print the extern declaration for it.
205 std::string Sym = Sname;
206 if (PAN::isMemIntrinsic(Sym)) {
207 Sym = PAN::addPrefix(Sym);
208 LibcallDecls.push_back(createESName(Sym));
209 }
Chris Lattner7f504882009-08-21 23:12:15 +0000210
Chris Lattner86974d42010-01-16 01:40:07 +0000211 O << Sym;
Chris Lattner7f504882009-08-21 23:12:15 +0000212 break;
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000213 }
214 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000215 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000216 return;
217
218 default:
219 llvm_unreachable(" Operand type not supported.");
220 }
221}
222
223/// printCCOperand - Print the cond code operand.
224///
225void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
226 int CC = (int)MI->getOperand(opNum).getImm();
227 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
228}
229
230// This function is used to sort the decls list.
231// should return true if s1 should come before s2.
232static bool is_before(const char *s1, const char *s2) {
233 return strcmp(s1, s2) <= 0;
234}
235
236// This is used by list::unique below.
237// unique will filter out duplicates if it knows them.
238static bool is_duplicate(const char *s1, const char *s2) {
239 return !strcmp(s1, s2);
240}
241
242/// printLibcallDecls - print the extern declarations for compiler
243/// intrinsics.
244///
245void PIC16AsmPrinter::printLibcallDecls() {
246 // If no libcalls used, return.
247 if (LibcallDecls.empty()) return;
248
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000249 O << MAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000250 // Remove duplicate entries.
251 LibcallDecls.sort(is_before);
252 LibcallDecls.unique(is_duplicate);
253
254 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
255 I != LibcallDecls.end(); I++) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000256 O << MAI->getExternDirective() << *I << "\n";
257 O << MAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
258 O << MAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000259 }
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000260 O << MAI->getCommentString() << "External decls for libcalls - END." <<"\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000261}
262
Bob Wilson4901f432009-09-30 21:44:42 +0000263/// doInitialization - Perform Module level initializations here.
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000264/// One task that we do here is to sectionize all global variables.
265/// The MemSelOptimizer pass depends on the sectionizing.
266///
267bool PIC16AsmPrinter::doInitialization(Module &M) {
268 bool Result = AsmPrinter::doInitialization(M);
269
Sanjiv Gupta20606ae2009-12-19 08:26:25 +0000270 // Every asmbly contains these std headers.
271 O << "\n#include p16f1xxx.inc";
272 O << "\n#include stdmacros.inc";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000273
274 // Set the section names for all globals.
275 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000276 I != E; ++I) {
277
278 // Record External Var Decls.
279 if (I->isDeclaration()) {
280 ExternalVarDecls.push_back(I);
281 continue;
282 }
283
284 // Record Exteranl Var Defs.
285 if (I->hasExternalLinkage() || I->hasCommonLinkage()) {
286 ExternalVarDefs.push_back(I);
287 }
288
289 // Sectionify actual data.
290 if (!I->hasAvailableExternallyLinkage()) {
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000291 const MCSection *S = getObjFileLowering().SectionForGlobal(I, Mang, TM);
292
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000293 I->setSection(((const PIC16Section *)S)->getName());
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000294 }
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000295 }
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000296
297 DbgInfo.BeginModule(M);
298 EmitFunctionDecls(M);
299 EmitUndefinedVars(M);
300 EmitDefinedVars(M);
301 EmitIData(M);
302 EmitUData(M);
303 EmitRomData(M);
Sanjiv Gupta27e801c2009-10-25 08:14:11 +0000304 EmitSharedUdata(M);
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000305 EmitUserSections(M);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000306 return Result;
307}
308
309/// Emit extern decls for functions imported from other modules, and emit
310/// global declarations for function defined in this module and which are
311/// available to other modules.
312///
313void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
314 // Emit declarations for external functions.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000315 O <<"\n"<<MAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000316 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
Chris Lattner86974d42010-01-16 01:40:07 +0000317 if (I->isIntrinsic() || I->getName() == "@abort")
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000318 continue;
319
320 if (!I->isDeclaration() && !I->hasExternalLinkage())
321 continue;
322
Chris Lattner86974d42010-01-16 01:40:07 +0000323 MCSymbol *Sym = GetGlobalValueSymbol(I);
324
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000325 // Do not emit memcpy, memset, and memmove here.
326 // Calls to these routines can be generated in two ways,
327 // 1. User calling the standard lib function
328 // 2. Codegen generating these calls for llvm intrinsics.
329 // In the first case a prototype is alread availale, while in
330 // second case the call is via and externalsym and the prototype is missing.
331 // So declarations for these are currently always getting printing by
332 // tracking both kind of references in printInstrunction.
Chris Lattner86974d42010-01-16 01:40:07 +0000333 if (I->isDeclaration() && PAN::isMemIntrinsic(Sym->getName())) continue;
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000334
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000335 const char *directive = I->isDeclaration() ? MAI->getExternDirective() :
336 MAI->getGlobalDirective();
Chris Lattner7f504882009-08-21 23:12:15 +0000337
Chris Lattner86974d42010-01-16 01:40:07 +0000338 O << directive << Sym->getName() << "\n";
339 O << directive << PAN::getRetvalLabel(Sym->getName()) << "\n";
340 O << directive << PAN::getArgsLabel(Sym->getName()) << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000341 }
342
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000343 O << MAI->getCommentString() << "Function Declarations - END." <<"\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000344}
345
346// Emit variables imported from other Modules.
347void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000348 std::vector<const GlobalVariable*> Items = ExternalVarDecls;
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000349 if (!Items.size()) return;
350
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000351 O << "\n" << MAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000352 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattner86974d42010-01-16 01:40:07 +0000353 O << MAI->getExternDirective();
354 GetGlobalValueSymbol(Items[j])->print(O, MAI);
355 O << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000356 }
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000357 O << MAI->getCommentString() << "Imported Variables - END" << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000358}
359
360// Emit variables defined in this module and are available to other modules.
361void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000362 std::vector<const GlobalVariable*> Items = ExternalVarDefs;
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000363 if (!Items.size()) return;
364
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000365 O << "\n" << MAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000366 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattner86974d42010-01-16 01:40:07 +0000367 O << MAI->getGlobalDirective();
368 GetGlobalValueSymbol(Items[j])->print(O, MAI);
369 O << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000370 }
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000371 O << MAI->getCommentString() << "Exported Variables - END" << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000372}
373
374// Emit initialized data placed in ROM.
375void PIC16AsmPrinter::EmitRomData(Module &M) {
Sanjiv Gupta018db662009-10-16 08:58:34 +0000376 EmitSingleSection(PTOF->ROMDATASection());
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000377}
378
Sanjiv Gupta27e801c2009-10-25 08:14:11 +0000379// Emit Shared section udata.
380void PIC16AsmPrinter::EmitSharedUdata(Module &M) {
381 EmitSingleSection(PTOF->SHAREDUDATASection());
382}
383
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000384bool PIC16AsmPrinter::doFinalization(Module &M) {
Sanjiv Gupta00e8f5a2009-10-21 10:42:44 +0000385 EmitAllAutos(M);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000386 printLibcallDecls();
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000387 DbgInfo.EndModule(M);
388 O << "\n\t" << "END\n";
389 return AsmPrinter::doFinalization(M);
390}
391
392void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
393 const Function *F = MF.getFunction();
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000394 const TargetData *TD = TM.getTargetData();
395 // Emit the data section name.
396 O << "\n";
Chris Lattner08e63732009-08-21 23:08:09 +0000397
Chris Lattnerc0c8d7d2010-01-16 01:17:26 +0000398 PIC16Section *fPDataSection =
399 const_cast<PIC16Section *>(getObjFileLowering().
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000400 SectionForFrame(CurrentFnSym->getName()));
Sanjiv Gupta00e8f5a2009-10-21 10:42:44 +0000401
402 fPDataSection->setColor(getFunctionColor(F));
Chris Lattner73266f92009-08-19 05:49:37 +0000403 OutStreamer.SwitchSection(fPDataSection);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000404
405 // Emit function frame label
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000406 O << PAN::getFrameLabel(CurrentFnSym->getName()) << ":\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000407
408 const Type *RetType = F->getReturnType();
409 unsigned RetSize = 0;
410 if (RetType->getTypeID() != Type::VoidTyID)
411 RetSize = TD->getTypeAllocSize(RetType);
412
413 //Emit function return value space
414 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
415 // we will need to avoid printing a global directive for Retval label
416 // in emitExternandGloblas.
417 if(RetSize > 0)
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000418 O << PAN::getRetvalLabel(CurrentFnSym->getName())
419 << " RES " << RetSize << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000420 else
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000421 O << PAN::getRetvalLabel(CurrentFnSym->getName()) << ": \n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000422
423 // Emit variable to hold the space for function arguments
424 unsigned ArgSize = 0;
425 for (Function::const_arg_iterator argi = F->arg_begin(),
426 arge = F->arg_end(); argi != arge ; ++argi) {
427 const Type *Ty = argi->getType();
428 ArgSize += TD->getTypeAllocSize(Ty);
429 }
430
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000431 O << PAN::getArgsLabel(CurrentFnSym->getName()) << " RES " << ArgSize << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000432
433 // Emit temporary space
434 int TempSize = PTLI->GetTmpSize();
435 if (TempSize > 0)
Chris Lattner0e9e07a2010-01-16 01:21:04 +0000436 O << PAN::getTempdataLabel(CurrentFnSym->getName()) << " RES "
437 << TempSize << '\n';
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000438}
439
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000440
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000441void PIC16AsmPrinter::EmitInitializedDataSection(const PIC16Section *S) {
442 /// Emit Section header.
443 OutStreamer.SwitchSection(S);
444
445 std::vector<const GlobalVariable*> Items = S->Items;
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000446 for (unsigned j = 0; j < Items.size(); j++) {
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000447 Constant *C = Items[j]->getInitializer();
448 int AddrSpace = Items[j]->getType()->getAddressSpace();
Chris Lattner86974d42010-01-16 01:40:07 +0000449 GetGlobalValueSymbol(Items[j])->print(O, MAI);
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000450 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000451 }
Sanjiv Gupta1c138172009-10-15 10:10:43 +0000452}
453
Sanjiv Gupta018db662009-10-16 08:58:34 +0000454// Print all IDATA sections.
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000455void PIC16AsmPrinter::EmitIData(Module &M) {
Sanjiv Gupta018db662009-10-16 08:58:34 +0000456 EmitSectionList (M, PTOF->IDATASections());
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000457}
458
Sanjiv Gupta018db662009-10-16 08:58:34 +0000459void PIC16AsmPrinter::
460EmitUninitializedDataSection(const PIC16Section *S) {
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000461 const TargetData *TD = TM.getTargetData();
462 OutStreamer.SwitchSection(S);
463 std::vector<const GlobalVariable*> Items = S->Items;
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000464 for (unsigned j = 0; j < Items.size(); j++) {
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000465 Constant *C = Items[j]->getInitializer();
466 const Type *Ty = C->getType();
467 unsigned Size = TD->getTypeAllocSize(Ty);
Chris Lattner86974d42010-01-16 01:40:07 +0000468 GetGlobalValueSymbol(Items[j])->print(O, MAI);
469 O << " RES " << Size << "\n";
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000470 }
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000471}
472
Sanjiv Gupta018db662009-10-16 08:58:34 +0000473// Print all UDATA sections.
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000474void PIC16AsmPrinter::EmitUData(Module &M) {
Sanjiv Gupta018db662009-10-16 08:58:34 +0000475 EmitSectionList (M, PTOF->UDATASections());
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000476}
477
Sanjiv Gupta018db662009-10-16 08:58:34 +0000478// Print all USER sections.
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000479void PIC16AsmPrinter::EmitUserSections(Module &M) {
Sanjiv Gupta018db662009-10-16 08:58:34 +0000480 EmitSectionList (M, PTOF->USERSections());
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000481}
482
Sanjiv Gupta018db662009-10-16 08:58:34 +0000483// Print all AUTO sections.
Sanjiv Guptaac2620e2009-10-15 19:26:25 +0000484void PIC16AsmPrinter::EmitAllAutos(Module &M) {
Sanjiv Gupta018db662009-10-16 08:58:34 +0000485 EmitSectionList (M, PTOF->AUTOSections());
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000486}
487
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000488extern "C" void LLVMInitializePIC16AsmPrinter() {
489 RegisterAsmPrinter<PIC16AsmPrinter> X(ThePIC16Target);
490}
491
Sanjiv Gupta018db662009-10-16 08:58:34 +0000492// Emit one data section using correct section emitter based on section type.
493void PIC16AsmPrinter::EmitSingleSection(const PIC16Section *S) {
494 if (S == NULL) return;
495
496 switch (S->getType()) {
497 default: llvm_unreachable ("unknow user section type");
498 case UDATA:
499 case UDATA_SHR:
500 case UDATA_OVR:
501 EmitUninitializedDataSection(S);
502 break;
503 case IDATA:
504 case ROMDATA:
505 EmitInitializedDataSection(S);
506 break;
507 }
508}
509
510// Emit a list of sections.
511void PIC16AsmPrinter::
512EmitSectionList(Module &M, const std::vector<PIC16Section *> &SList) {
513 for (unsigned i = 0; i < SList.size(); i++) {
514 // Exclude llvm specific metadata sections.
515 if (SList[i]->getName().find("llvm.") != std::string::npos)
516 continue;
517 O << "\n";
518 EmitSingleSection(SList[i]);
519 }
520}
Sanjiv Gupta8aae07e2009-08-13 16:37:05 +0000521