blob: e98cf41b5400cd3b62da447fb6decfdb60cf7271 [file] [log] [blame]
Sanjiv Gupta0e687712008-05-13 09:02:57 +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 Guptab1b5ffd2008-11-19 11:00:54 +000015#include "PIC16AsmPrinter.h"
16#include "PIC16TargetAsmInfo.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000017#include "llvm/DerivedTypes.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000018#include "llvm/Function.h"
19#include "llvm/Module.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000020#include "llvm/CodeGen/DwarfWriter.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +000022#include "llvm/CodeGen/DwarfWriter.h"
23#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000024#include "llvm/MC/MCSection.h"
Daniel Dunbar0c795d62009-07-25 06:49:55 +000025#include "llvm/Target/TargetRegistry.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000026#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000027#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/FormattedStream.h"
29#include "llvm/Support/Mangler.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000030using namespace llvm;
31
Sanjiv Gupta0e687712008-05-13 09:02:57 +000032#include "PIC16GenAsmWriter.inc"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000033
Chris Lattnerf0144122009-07-28 03:13:23 +000034PIC16AsmPrinter::PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
35 const TargetAsmInfo *T, bool V)
36: AsmPrinter(O, TM, T, V), DbgInfo(O, T) {
37 PTLI = static_cast<const PIC16TargetLowering*>(TM.getTargetLowering());
38 PTAI = static_cast<const PIC16TargetAsmInfo*>(T);
Chris Lattnerf4b64f62009-07-28 03:20:34 +000039 PTOF = (PIC16TargetObjectFile*)&PTLI->getObjFileLowering();
Chris Lattnerf0144122009-07-28 03:13:23 +000040}
41
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000042bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000043 printInstruction(MI);
44 return true;
45}
46
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000047/// runOnMachineFunction - This emits the frame section, autos section and
48/// assembly for each instruction. Also takes care of function begin debug
49/// directive and file begin debug directive (if required) for the function.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000050///
51bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +000052 this->MF = &MF;
53
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000054 // This calls the base class function required to be called at beginning
55 // of runOnMachineFunction.
56 SetupMachineFunction(MF);
57
58 // Get the mangled name.
59 const Function *F = MF.getFunction();
Chris Lattnerb8158ac2009-07-14 18:17:16 +000060 CurrentFnName = Mang->getMangledName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000061
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000062 // Emit the function frame (args and temps).
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000063 EmitFunctionFrame(MF);
64
Sanjiv Guptabde79422009-06-16 09:45:18 +000065 DbgInfo.BeginFunction(MF);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000066
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000067 // Emit the autos section of function.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000068 EmitAutos(CurrentFnName);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000069
70 // Now emit the instructions of function in its code section.
Chris Lattnerf0144122009-07-28 03:13:23 +000071 std::string T = PAN::getCodeSectionName(CurrentFnName);
72 const char *codeSection = T.c_str();
Sanjiv Gupta211f3622009-05-10 05:23:47 +000073
Chris Lattnera87dea42009-07-31 18:48:30 +000074 const MCSection *fCodeSection =
Chris Lattnerf0144122009-07-28 03:13:23 +000075 getObjFileLowering().getOrCreateSection(codeSection, false,
Chris Lattner1ef9be22009-08-02 00:02:44 +000076 SectionKind::getText());
Sanjiv Gupta211f3622009-05-10 05:23:47 +000077 // Start the Code Section.
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000078 O << "\n";
Chris Lattnerdc471462009-07-21 16:44:47 +000079 SwitchToSection(fCodeSection);
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000080
81 // Emit the frame address of the function at the beginning of code.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000082 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
83 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000084
Sanjiv Gupta211f3622009-05-10 05:23:47 +000085 // Emit function start label.
86 O << CurrentFnName << ":\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000087
Sanjiv Guptabde79422009-06-16 09:45:18 +000088 DebugLoc CurDL;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000089 O << "\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000090 // Print out code for the function.
91 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
92 I != E; ++I) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000093
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000094 // Print a label for the basic block.
95 if (I != MF.begin()) {
96 printBasicBlockLabel(I, true);
97 O << '\n';
98 }
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000099
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000100 // Print a basic block.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000101 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
102 II != E; ++II) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000103
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000104 // Emit the line directive if source line changed.
105 const DebugLoc DL = II->getDebugLoc();
Sanjiv Guptabde79422009-06-16 09:45:18 +0000106 if (!DL.isUnknown() && DL != CurDL) {
107 DbgInfo.ChangeDebugLoc(MF, DL);
108 CurDL = DL;
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000109 }
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000110
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000111 // Print the assembly for the instruction.
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000112 printMachineInstruction(II);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000113 }
114 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000115
116 // Emit function end debug directives.
Sanjiv Guptabde79422009-06-16 09:45:18 +0000117 DbgInfo.EndFunction(MF);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000118
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000119 return false; // we didn't modify anything.
120}
121
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000122
123// printOperand - print operand of insn.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000124void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000125 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000126
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000127 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000128 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000129 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000130 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000131 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000132 llvm_unreachable("not implemented");
Torok Edwinc25e7582009-07-11 20:10:48 +0000133 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000134
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000135 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000136 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000137 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000138
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000139 case MachineOperand::MO_GlobalAddress: {
Sanjiv Guptae409f0a2009-07-30 04:15:15 +0000140 std::string Sname = Mang->getMangledName(MO.getGlobal());
141 // FIXME: currently we do not have a memcpy def coming in the module
142 // by any chance, as we do not link in those as .bc lib. So these calls
143 // are always external and it is safe to emit an extern.
144 if (PAN::isMemIntrinsic(Sname)) {
145 LibcallDecls.push_back(createESName(Sname));
146 }
147
148 O << Sname;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000149 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000150 }
151 case MachineOperand::MO_ExternalSymbol: {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000152 const char *Sname = MO.getSymbolName();
153
154 // If its a libcall name, record it to decls section.
155 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000156 LibcallDecls.push_back(Sname);
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000157 }
158
Sanjiv Guptae409f0a2009-07-30 04:15:15 +0000159 // Record a call to intrinsic to print the extern declaration for it.
160 std::string Sym = Sname;
161 if (PAN::isMemIntrinsic(Sym)) {
162 Sym = PAN::addPrefix(Sym);
163 LibcallDecls.push_back(createESName(Sym));
164 }
165
166 O << Sym;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000167 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000168 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000169 case MachineOperand::MO_MachineBasicBlock:
170 printBasicBlockLabel(MO.getMBB());
171 return;
172
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000173 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000174 llvm_unreachable(" Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000175 }
176}
177
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000178/// printCCOperand - Print the cond code operand.
179///
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000180void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
181 int CC = (int)MI->getOperand(opNum).getImm();
182 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
183}
184
Sanjiv Guptabe7b97b2009-07-27 18:04:34 +0000185// This function is used to sort the decls list.
186// should return true if s1 should come before s2.
187static bool is_before(const char *s1, const char *s2) {
188 std::string str1 = s1;
189 std::string str2 = s2;
190 int i = str1.compare(str2);
191 // Return true if s1 is smaller or equal.
192 if (i <= 0) return true;
193 // false if s1 should come after s2.
194 return false;
195}
196
197// This is used by list::unique below.
198// unique will filter out duplicates if it knows them.
199static bool is_duplicate(const char *s1, const char *s2) {
200 std::string str1 = s1;
201 std::string str2 = s2;
202 return str1 == str2;
203}
204
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000205/// printLibcallDecls - print the extern declarations for compiler
206/// intrinsics.
207///
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000208void PIC16AsmPrinter::printLibcallDecls(void) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000209 // If no libcalls used, return.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000210 if (LibcallDecls.empty()) return;
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000211
Sanjiv Gupta42918572009-05-12 06:52:41 +0000212 O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000213 // Remove duplicate entries.
Sanjiv Guptabe7b97b2009-07-27 18:04:34 +0000214 LibcallDecls.sort(is_before);
215 LibcallDecls.unique(is_duplicate);
216
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000217 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
218 I != LibcallDecls.end(); I++) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000219 O << TAI->getExternDirective() << *I << "\n";
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000220 O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
221 O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000222 }
Sanjiv Gupta42918572009-05-12 06:52:41 +0000223 O << TAI->getCommentString() << "External decls for libcalls - END." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000224}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000225
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000226/// doInitialization - Perfrom Module level initializations here.
227/// One task that we do here is to sectionize all global variables.
228/// The MemSelOptimizer pass depends on the sectionizing.
229///
Chris Lattnerdc471462009-07-21 16:44:47 +0000230bool PIC16AsmPrinter::doInitialization(Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000231 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000232
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000233 // FIXME:: This is temporary solution to generate the include file.
234 // The processor should be passed to llc as in input and the header file
235 // should be generated accordingly.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000236 O << "\n\t#include P16F1937.INC\n";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000237
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000238 // Set the section names for all globals.
239 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerf0144122009-07-28 03:13:23 +0000240 I != E; ++I)
Dan Gohman18f54c02009-08-02 01:18:44 +0000241 if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage())
Chris Lattner6f411df2009-08-02 01:02:43 +0000242 I->setSection(getObjFileLowering().
243 SectionForGlobal(I, Mang,TM)->getName());
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000244
Sanjiv Guptabde79422009-06-16 09:45:18 +0000245 DbgInfo.BeginModule(M);
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000246 EmitFunctionDecls(M);
247 EmitUndefinedVars(M);
248 EmitDefinedVars(M);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000249 EmitIData(M);
250 EmitUData(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000251 EmitRomData(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000252 return Result;
253}
254
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000255/// Emit extern decls for functions imported from other modules, and emit
256/// global declarations for function defined in this module and which are
257/// available to other modules.
258///
Chris Lattnerdc471462009-07-21 16:44:47 +0000259void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000260 // Emit declarations for external functions.
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000261 O <<"\n"<<TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000262 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
Sanjiv Gupta0f009d22009-07-23 02:11:04 +0000263 if (I->isIntrinsic())
264 continue;
265
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000266 std::string Name = Mang->getMangledName(I);
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000267 if (Name.compare("@abort") == 0)
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000268 continue;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000269
Chris Lattnerdc471462009-07-21 16:44:47 +0000270 if (!I->isDeclaration() && !I->hasExternalLinkage())
Sanjiv Guptaaf3fdb52009-05-10 16:18:39 +0000271 continue;
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000272
Sanjiv Guptae409f0a2009-07-30 04:15:15 +0000273 // Do not emit memcpy, memset, and memmove here.
274 // Calls to these routines can be generated in two ways,
275 // 1. User calling the standard lib function
276 // 2. Codegen generating these calls for llvm intrinsics.
277 // In the first case a prototype is alread availale, while in
278 // second case the call is via and externalsym and the prototype is missing.
279 // So declarations for these are currently always getting printing by
280 // tracking both kind of references in printInstrunction.
281 if (I->isDeclaration() && PAN::isMemIntrinsic(Name)) continue;
282
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000283 const char *directive = I->isDeclaration() ? TAI->getExternDirective() :
284 TAI->getGlobalDirective();
285
286 O << directive << Name << "\n";
287 O << directive << PAN::getRetvalLabel(Name) << "\n";
288 O << directive << PAN::getArgsLabel(Name) << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000289 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000290
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000291 O << TAI->getCommentString() << "Function Declarations - END." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000292}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000293
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000294// Emit variables imported from other Modules.
Chris Lattnerdc471462009-07-21 16:44:47 +0000295void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000296 std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDecls->Items;
Chris Lattnerdc471462009-07-21 16:44:47 +0000297 if (!Items.size()) return;
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000298
299 O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
300 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000301 O << TAI->getExternDirective() << Mang->getMangledName(Items[j]) << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000302 }
303 O << TAI->getCommentString() << "Imported Variables - END" << "\n";
304}
305
306// Emit variables defined in this module and are available to other modules.
Chris Lattnerdc471462009-07-21 16:44:47 +0000307void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000308 std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDefs->Items;
Chris Lattnerdc471462009-07-21 16:44:47 +0000309 if (!Items.size()) return;
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000310
Chris Lattnera46cb522009-07-21 17:20:18 +0000311 O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000312 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000313 O << TAI->getGlobalDirective() << Mang->getMangledName(Items[j]) << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000314 }
315 O << TAI->getCommentString() << "Exported Variables - END" << "\n";
316}
317
318// Emit initialized data placed in ROM.
Chris Lattnerdc471462009-07-21 16:44:47 +0000319void PIC16AsmPrinter::EmitRomData(Module &M) {
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000320 // Print ROM Data section.
Chris Lattnerf0144122009-07-28 03:13:23 +0000321 const std::vector<PIC16Section*> &ROSections = PTOF->ROSections;
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000322 for (unsigned i = 0; i < ROSections.size(); i++) {
Chris Lattnerdc471462009-07-21 16:44:47 +0000323 const std::vector<const GlobalVariable*> &Items = ROSections[i]->Items;
324 if (!Items.size()) continue;
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000325 O << "\n";
Chris Lattnerf0144122009-07-28 03:13:23 +0000326 SwitchToSection(PTOF->ROSections[i]->S_);
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000327 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000328 O << Mang->getMangledName(Items[j]);
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000329 Constant *C = Items[j]->getInitializer();
330 int AddrSpace = Items[j]->getType()->getAddressSpace();
331 EmitGlobalConstant(C, AddrSpace);
332 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000333 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000334}
335
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000336bool PIC16AsmPrinter::doFinalization(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000337 printLibcallDecls();
Sanjiv Guptab157f252009-06-09 15:31:19 +0000338 EmitRemainingAutos();
Sanjiv Guptabde79422009-06-16 09:45:18 +0000339 DbgInfo.EndModule(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000340 O << "\n\t" << "END\n";
Chris Lattner40bbebd2009-07-21 18:38:57 +0000341 return AsmPrinter::doFinalization(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000342}
343
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000344void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000345 const Function *F = MF.getFunction();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000346 std::string FuncName = Mang->getMangledName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000347 const TargetData *TD = TM.getTargetData();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000348 // Emit the data section name.
349 O << "\n";
Chris Lattnerf0144122009-07-28 03:13:23 +0000350 std::string T = PAN::getFrameSectionName(CurrentFnName);
351 const char *SectionName = T.c_str();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000352
Chris Lattnera87dea42009-07-31 18:48:30 +0000353 const MCSection *fPDataSection =
Chris Lattnerf0144122009-07-28 03:13:23 +0000354 getObjFileLowering().getOrCreateSection(SectionName, false,
Chris Lattner1ef9be22009-08-02 00:02:44 +0000355 SectionKind::getDataRel());
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000356 SwitchToSection(fPDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000357
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000358 // Emit function frame label
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000359 O << PAN::getFrameLabel(CurrentFnName) << ":\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000360
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000361 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000362 unsigned RetSize = 0;
363 if (RetType->getTypeID() != Type::VoidTyID)
Duncan Sands777d2302009-05-09 07:06:46 +0000364 RetSize = TD->getTypeAllocSize(RetType);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000365
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000366 //Emit function return value space
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000367 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
368 // we will need to avoid printing a global directive for Retval label
369 // in emitExternandGloblas.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000370 if(RetSize > 0)
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000371 O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000372 else
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000373 O << PAN::getRetvalLabel(CurrentFnName) << ": \n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000374
375 // Emit variable to hold the space for function arguments
376 unsigned ArgSize = 0;
377 for (Function::const_arg_iterator argi = F->arg_begin(),
378 arge = F->arg_end(); argi != arge ; ++argi) {
379 const Type *Ty = argi->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000380 ArgSize += TD->getTypeAllocSize(Ty);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000381 }
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000382
383 O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000384
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000385 // Emit temporary space
386 int TempSize = PTLI->GetTmpSize();
Chris Lattnerdc471462009-07-21 16:44:47 +0000387 if (TempSize > 0)
388 O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize << '\n';
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000389}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000390
Chris Lattnerdc471462009-07-21 16:44:47 +0000391void PIC16AsmPrinter::EmitIData(Module &M) {
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000392
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000393 // Print all IDATA sections.
Chris Lattnerf0144122009-07-28 03:13:23 +0000394 const std::vector<PIC16Section*> &IDATASections = PTOF->IDATASections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000395 for (unsigned i = 0; i < IDATASections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000396 O << "\n";
Sanjiv Gupta024e94c2009-07-06 18:07:06 +0000397 if (IDATASections[i]->S_->getName().find("llvm.") != std::string::npos)
398 continue;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000399 SwitchToSection(IDATASections[i]->S_);
400 std::vector<const GlobalVariable*> Items = IDATASections[i]->Items;
401 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000402 std::string Name = Mang->getMangledName(Items[j]);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000403 Constant *C = Items[j]->getInitializer();
404 int AddrSpace = Items[j]->getType()->getAddressSpace();
405 O << Name;
406 EmitGlobalConstant(C, AddrSpace);
407 }
408 }
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000409}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000410
Chris Lattnerdc471462009-07-21 16:44:47 +0000411void PIC16AsmPrinter::EmitUData(Module &M) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000412 const TargetData *TD = TM.getTargetData();
413
414 // Print all BSS sections.
Chris Lattnerf0144122009-07-28 03:13:23 +0000415 const std::vector<PIC16Section*> &BSSSections = PTOF->BSSSections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000416 for (unsigned i = 0; i < BSSSections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000417 O << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000418 SwitchToSection(BSSSections[i]->S_);
419 std::vector<const GlobalVariable*> Items = BSSSections[i]->Items;
420 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000421 std::string Name = Mang->getMangledName(Items[j]);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000422 Constant *C = Items[j]->getInitializer();
423 const Type *Ty = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000424 unsigned Size = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000425
Chris Lattnerdc471462009-07-21 16:44:47 +0000426 O << Name << " RES " << Size << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000427 }
428 }
429}
430
Chris Lattnerdc471462009-07-21 16:44:47 +0000431void PIC16AsmPrinter::EmitAutos(std::string FunctName) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000432 // Section names for all globals are already set.
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000433 const TargetData *TD = TM.getTargetData();
434
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000435 // Now print Autos section for this function.
436 std::string SectionName = PAN::getAutosSectionName(FunctName);
Chris Lattnerf0144122009-07-28 03:13:23 +0000437 const std::vector<PIC16Section*> &AutosSections = PTOF->AutosSections;
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000438 for (unsigned i = 0; i < AutosSections.size(); i++) {
439 O << "\n";
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000440 if (AutosSections[i]->S_->getName() == SectionName) {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000441 // Set the printing status to true
442 AutosSections[i]->setPrintedStatus(true);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000443 SwitchToSection(AutosSections[i]->S_);
Chris Lattnerdc471462009-07-21 16:44:47 +0000444 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000445 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000446 std::string VarName = Mang->getMangledName(Items[j]);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000447 Constant *C = Items[j]->getInitializer();
448 const Type *Ty = C->getType();
449 unsigned Size = TD->getTypeAllocSize(Ty);
450 // Emit memory reserve directive.
451 O << VarName << " RES " << Size << "\n";
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000452 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000453 break;
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000454 }
455 }
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000456}
457
Sanjiv Guptab157f252009-06-09 15:31:19 +0000458// Print autos that were not printed during the code printing of functions.
459// As the functions might themselves would have got deleted by the optimizer.
Chris Lattnerdc471462009-07-21 16:44:47 +0000460void PIC16AsmPrinter::EmitRemainingAutos() {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000461 const TargetData *TD = TM.getTargetData();
462
463 // Now print Autos section for this function.
Chris Lattnerf0144122009-07-28 03:13:23 +0000464 std::vector <PIC16Section *>AutosSections = PTOF->AutosSections;
Sanjiv Guptab157f252009-06-09 15:31:19 +0000465 for (unsigned i = 0; i < AutosSections.size(); i++) {
466
467 // if the section is already printed then don't print again
468 if (AutosSections[i]->isPrinted())
469 continue;
470
471 // Set status as printed
472 AutosSections[i]->setPrintedStatus(true);
473
474 O << "\n";
475 SwitchToSection(AutosSections[i]->S_);
Chris Lattnerdc471462009-07-21 16:44:47 +0000476 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
Sanjiv Guptab157f252009-06-09 15:31:19 +0000477 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000478 std::string VarName = Mang->getMangledName(Items[j]);
Sanjiv Guptab157f252009-06-09 15:31:19 +0000479 Constant *C = Items[j]->getInitializer();
480 const Type *Ty = C->getType();
481 unsigned Size = TD->getTypeAllocSize(Ty);
482 // Emit memory reserve directive.
483 O << VarName << " RES " << Size << "\n";
484 }
485 }
486}
487
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000488
489extern "C" void LLVMInitializePIC16Target() {
490 // Register the targets
491 RegisterTargetMachine<PIC16TargetMachine> A(ThePIC16Target);
492 RegisterTargetMachine<CooperTargetMachine> B(TheCooperTarget);
493 RegisterAsmPrinter<PIC16AsmPrinter> C(ThePIC16Target);
494 RegisterAsmPrinter<PIC16AsmPrinter> D(TheCooperTarget);
495}