blob: 39ebfa4439905cd9cf8d01d91a56aec7391c6328 [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"
Chris Lattner93b6db32009-08-08 23:39:42 +000016#include "PIC16Section.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000017#include "PIC16TargetAsmInfo.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000018#include "llvm/DerivedTypes.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000019#include "llvm/Function.h"
20#include "llvm/Module.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000021#include "llvm/CodeGen/DwarfWriter.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +000023#include "llvm/CodeGen/DwarfWriter.h"
24#include "llvm/CodeGen/MachineModuleInfo.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"
Benjamin Kramerb8d222d2009-08-07 10:42:28 +000030#include <cstring>
Sanjiv Gupta0e687712008-05-13 09:02:57 +000031using namespace llvm;
32
Sanjiv Gupta0e687712008-05-13 09:02:57 +000033#include "PIC16GenAsmWriter.inc"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000034
Chris Lattnerf0144122009-07-28 03:13:23 +000035PIC16AsmPrinter::PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
36 const TargetAsmInfo *T, bool V)
37: AsmPrinter(O, TM, T, V), DbgInfo(O, T) {
Chris Lattner12b2bda2009-08-10 17:35:42 +000038 PTLI = static_cast<PIC16TargetLowering*>(TM.getTargetLowering());
Chris Lattnerf0144122009-07-28 03:13:23 +000039 PTAI = static_cast<const PIC16TargetAsmInfo*>(T);
Chris Lattnerf4b64f62009-07-28 03:20:34 +000040 PTOF = (PIC16TargetObjectFile*)&PTLI->getObjFileLowering();
Chris Lattnerf0144122009-07-28 03:13:23 +000041}
42
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000043bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000044 printInstruction(MI);
45 return true;
46}
47
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000048/// runOnMachineFunction - This emits the frame section, autos section and
49/// assembly for each instruction. Also takes care of function begin debug
50/// directive and file begin debug directive (if required) for the function.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000051///
52bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +000053 this->MF = &MF;
54
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000055 // This calls the base class function required to be called at beginning
56 // of runOnMachineFunction.
57 SetupMachineFunction(MF);
58
59 // Get the mangled name.
60 const Function *F = MF.getFunction();
Chris Lattnerb8158ac2009-07-14 18:17:16 +000061 CurrentFnName = Mang->getMangledName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000062
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000063 // Emit the function frame (args and temps).
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000064 EmitFunctionFrame(MF);
65
Sanjiv Guptabde79422009-06-16 09:45:18 +000066 DbgInfo.BeginFunction(MF);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000067
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000068 // Emit the autos section of function.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000069 EmitAutos(CurrentFnName);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000070
71 // Now emit the instructions of function in its code section.
Chris Lattnera87dea42009-07-31 18:48:30 +000072 const MCSection *fCodeSection =
Chris Lattner759b8882009-08-06 16:27:28 +000073 getObjFileLowering().getSectionForFunction(CurrentFnName);
Sanjiv Gupta211f3622009-05-10 05:23:47 +000074 // Start the Code Section.
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000075 O << "\n";
Chris Lattnerdc471462009-07-21 16:44:47 +000076 SwitchToSection(fCodeSection);
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000077
78 // Emit the frame address of the function at the beginning of code.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000079 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
80 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000081
Sanjiv Gupta211f3622009-05-10 05:23:47 +000082 // Emit function start label.
83 O << CurrentFnName << ":\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000084
Sanjiv Guptabde79422009-06-16 09:45:18 +000085 DebugLoc CurDL;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000086 O << "\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000087 // Print out code for the function.
88 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
89 I != E; ++I) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000090
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000091 // Print a label for the basic block.
92 if (I != MF.begin()) {
93 printBasicBlockLabel(I, true);
94 O << '\n';
95 }
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000096
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000097 // Print a basic block.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000098 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
99 II != E; ++II) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000100
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000101 // Emit the line directive if source line changed.
102 const DebugLoc DL = II->getDebugLoc();
Sanjiv Guptabde79422009-06-16 09:45:18 +0000103 if (!DL.isUnknown() && DL != CurDL) {
104 DbgInfo.ChangeDebugLoc(MF, DL);
105 CurDL = DL;
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000106 }
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000107
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000108 // Print the assembly for the instruction.
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000109 printMachineInstruction(II);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000110 }
111 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000112
113 // Emit function end debug directives.
Sanjiv Guptabde79422009-06-16 09:45:18 +0000114 DbgInfo.EndFunction(MF);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000115
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000116 return false; // we didn't modify anything.
117}
118
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000119
120// printOperand - print operand of insn.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000121void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000122 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000123
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000124 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000125 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000126 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000127 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000128 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000129 llvm_unreachable("not implemented");
Torok Edwinc25e7582009-07-11 20:10:48 +0000130 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000131
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000132 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000133 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000134 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000135
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000136 case MachineOperand::MO_GlobalAddress: {
Sanjiv Guptae409f0a2009-07-30 04:15:15 +0000137 std::string Sname = Mang->getMangledName(MO.getGlobal());
138 // FIXME: currently we do not have a memcpy def coming in the module
139 // by any chance, as we do not link in those as .bc lib. So these calls
140 // are always external and it is safe to emit an extern.
141 if (PAN::isMemIntrinsic(Sname)) {
142 LibcallDecls.push_back(createESName(Sname));
143 }
144
145 O << Sname;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000146 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000147 }
148 case MachineOperand::MO_ExternalSymbol: {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000149 const char *Sname = MO.getSymbolName();
150
151 // If its a libcall name, record it to decls section.
152 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000153 LibcallDecls.push_back(Sname);
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000154 }
155
Sanjiv Guptae409f0a2009-07-30 04:15:15 +0000156 // Record a call to intrinsic to print the extern declaration for it.
157 std::string Sym = Sname;
158 if (PAN::isMemIntrinsic(Sym)) {
159 Sym = PAN::addPrefix(Sym);
160 LibcallDecls.push_back(createESName(Sym));
161 }
162
163 O << Sym;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000164 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000165 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000166 case MachineOperand::MO_MachineBasicBlock:
167 printBasicBlockLabel(MO.getMBB());
168 return;
169
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000170 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000171 llvm_unreachable(" Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000172 }
173}
174
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000175/// printCCOperand - Print the cond code operand.
176///
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000177void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
178 int CC = (int)MI->getOperand(opNum).getImm();
179 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
180}
181
Sanjiv Guptabe7b97b2009-07-27 18:04:34 +0000182// This function is used to sort the decls list.
183// should return true if s1 should come before s2.
184static bool is_before(const char *s1, const char *s2) {
Benjamin Kramerb8d222d2009-08-07 10:42:28 +0000185 return strcmp(s1, s2) <= 0;
Sanjiv Guptabe7b97b2009-07-27 18:04:34 +0000186}
187
188// This is used by list::unique below.
189// unique will filter out duplicates if it knows them.
190static bool is_duplicate(const char *s1, const char *s2) {
Benjamin Kramerb8d222d2009-08-07 10:42:28 +0000191 return !strcmp(s1, s2);
Sanjiv Guptabe7b97b2009-07-27 18:04:34 +0000192}
193
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000194/// printLibcallDecls - print the extern declarations for compiler
195/// intrinsics.
196///
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000197void PIC16AsmPrinter::printLibcallDecls(void) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000198 // If no libcalls used, return.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000199 if (LibcallDecls.empty()) return;
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000200
Sanjiv Gupta42918572009-05-12 06:52:41 +0000201 O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000202 // Remove duplicate entries.
Sanjiv Guptabe7b97b2009-07-27 18:04:34 +0000203 LibcallDecls.sort(is_before);
204 LibcallDecls.unique(is_duplicate);
205
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000206 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
207 I != LibcallDecls.end(); I++) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000208 O << TAI->getExternDirective() << *I << "\n";
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000209 O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
210 O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000211 }
Sanjiv Gupta42918572009-05-12 06:52:41 +0000212 O << TAI->getCommentString() << "External decls for libcalls - END." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000213}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000214
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000215/// doInitialization - Perfrom Module level initializations here.
216/// One task that we do here is to sectionize all global variables.
217/// The MemSelOptimizer pass depends on the sectionizing.
218///
Chris Lattnerdc471462009-07-21 16:44:47 +0000219bool PIC16AsmPrinter::doInitialization(Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000220 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000221
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000222 // FIXME:: This is temporary solution to generate the include file.
223 // The processor should be passed to llc as in input and the header file
224 // should be generated accordingly.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000225 O << "\n\t#include P16F1937.INC\n";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000226
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000227 // Set the section names for all globals.
228 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerf0144122009-07-28 03:13:23 +0000229 I != E; ++I)
Chris Lattner93b6db32009-08-08 23:39:42 +0000230 if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) {
231 const MCSection *S = getObjFileLowering().SectionForGlobal(I, Mang, TM);
232
233 I->setSection(((const MCSectionPIC16*)S)->getName());
234 }
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000235
Sanjiv Guptabde79422009-06-16 09:45:18 +0000236 DbgInfo.BeginModule(M);
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000237 EmitFunctionDecls(M);
238 EmitUndefinedVars(M);
239 EmitDefinedVars(M);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000240 EmitIData(M);
241 EmitUData(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000242 EmitRomData(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000243 return Result;
244}
245
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000246/// Emit extern decls for functions imported from other modules, and emit
247/// global declarations for function defined in this module and which are
248/// available to other modules.
249///
Chris Lattnerdc471462009-07-21 16:44:47 +0000250void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000251 // Emit declarations for external functions.
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000252 O <<"\n"<<TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000253 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
Sanjiv Gupta0f009d22009-07-23 02:11:04 +0000254 if (I->isIntrinsic())
255 continue;
256
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000257 std::string Name = Mang->getMangledName(I);
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000258 if (Name.compare("@abort") == 0)
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000259 continue;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000260
Chris Lattnerdc471462009-07-21 16:44:47 +0000261 if (!I->isDeclaration() && !I->hasExternalLinkage())
Sanjiv Guptaaf3fdb52009-05-10 16:18:39 +0000262 continue;
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000263
Sanjiv Guptae409f0a2009-07-30 04:15:15 +0000264 // Do not emit memcpy, memset, and memmove here.
265 // Calls to these routines can be generated in two ways,
266 // 1. User calling the standard lib function
267 // 2. Codegen generating these calls for llvm intrinsics.
268 // In the first case a prototype is alread availale, while in
269 // second case the call is via and externalsym and the prototype is missing.
270 // So declarations for these are currently always getting printing by
271 // tracking both kind of references in printInstrunction.
272 if (I->isDeclaration() && PAN::isMemIntrinsic(Name)) continue;
273
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000274 const char *directive = I->isDeclaration() ? TAI->getExternDirective() :
275 TAI->getGlobalDirective();
276
277 O << directive << Name << "\n";
278 O << directive << PAN::getRetvalLabel(Name) << "\n";
279 O << directive << PAN::getArgsLabel(Name) << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000280 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000281
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000282 O << TAI->getCommentString() << "Function Declarations - END." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000283}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000284
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000285// Emit variables imported from other Modules.
Chris Lattnerdc471462009-07-21 16:44:47 +0000286void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000287 std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDecls->Items;
Chris Lattnerdc471462009-07-21 16:44:47 +0000288 if (!Items.size()) return;
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000289
290 O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
291 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000292 O << TAI->getExternDirective() << Mang->getMangledName(Items[j]) << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000293 }
294 O << TAI->getCommentString() << "Imported Variables - END" << "\n";
295}
296
297// Emit variables defined in this module and are available to other modules.
Chris Lattnerdc471462009-07-21 16:44:47 +0000298void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000299 std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDefs->Items;
Chris Lattnerdc471462009-07-21 16:44:47 +0000300 if (!Items.size()) return;
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000301
Chris Lattnera46cb522009-07-21 17:20:18 +0000302 O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000303 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000304 O << TAI->getGlobalDirective() << Mang->getMangledName(Items[j]) << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000305 }
306 O << TAI->getCommentString() << "Exported Variables - END" << "\n";
307}
308
309// Emit initialized data placed in ROM.
Chris Lattnerdc471462009-07-21 16:44:47 +0000310void PIC16AsmPrinter::EmitRomData(Module &M) {
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000311 // Print ROM Data section.
Chris Lattnerf0144122009-07-28 03:13:23 +0000312 const std::vector<PIC16Section*> &ROSections = PTOF->ROSections;
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000313 for (unsigned i = 0; i < ROSections.size(); i++) {
Chris Lattnerdc471462009-07-21 16:44:47 +0000314 const std::vector<const GlobalVariable*> &Items = ROSections[i]->Items;
315 if (!Items.size()) continue;
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000316 O << "\n";
Chris Lattnerf0144122009-07-28 03:13:23 +0000317 SwitchToSection(PTOF->ROSections[i]->S_);
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000318 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000319 O << Mang->getMangledName(Items[j]);
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000320 Constant *C = Items[j]->getInitializer();
321 int AddrSpace = Items[j]->getType()->getAddressSpace();
322 EmitGlobalConstant(C, AddrSpace);
323 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000324 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000325}
326
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000327bool PIC16AsmPrinter::doFinalization(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000328 printLibcallDecls();
Sanjiv Guptab157f252009-06-09 15:31:19 +0000329 EmitRemainingAutos();
Sanjiv Guptabde79422009-06-16 09:45:18 +0000330 DbgInfo.EndModule(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000331 O << "\n\t" << "END\n";
Chris Lattner40bbebd2009-07-21 18:38:57 +0000332 return AsmPrinter::doFinalization(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000333}
334
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000335void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000336 const Function *F = MF.getFunction();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000337 std::string FuncName = Mang->getMangledName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000338 const TargetData *TD = TM.getTargetData();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000339 // Emit the data section name.
340 O << "\n";
Chris Lattner759b8882009-08-06 16:27:28 +0000341
Chris Lattnera87dea42009-07-31 18:48:30 +0000342 const MCSection *fPDataSection =
Chris Lattner759b8882009-08-06 16:27:28 +0000343 getObjFileLowering().getSectionForFunctionFrame(CurrentFnName);
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000344 SwitchToSection(fPDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000345
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000346 // Emit function frame label
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000347 O << PAN::getFrameLabel(CurrentFnName) << ":\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000348
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000349 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000350 unsigned RetSize = 0;
351 if (RetType->getTypeID() != Type::VoidTyID)
Duncan Sands777d2302009-05-09 07:06:46 +0000352 RetSize = TD->getTypeAllocSize(RetType);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000353
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000354 //Emit function return value space
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000355 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
356 // we will need to avoid printing a global directive for Retval label
357 // in emitExternandGloblas.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000358 if(RetSize > 0)
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000359 O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000360 else
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000361 O << PAN::getRetvalLabel(CurrentFnName) << ": \n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000362
363 // Emit variable to hold the space for function arguments
364 unsigned ArgSize = 0;
365 for (Function::const_arg_iterator argi = F->arg_begin(),
366 arge = F->arg_end(); argi != arge ; ++argi) {
367 const Type *Ty = argi->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000368 ArgSize += TD->getTypeAllocSize(Ty);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000369 }
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000370
371 O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000372
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000373 // Emit temporary space
374 int TempSize = PTLI->GetTmpSize();
Chris Lattnerdc471462009-07-21 16:44:47 +0000375 if (TempSize > 0)
376 O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize << '\n';
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000377}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000378
Chris Lattnerdc471462009-07-21 16:44:47 +0000379void PIC16AsmPrinter::EmitIData(Module &M) {
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000380
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000381 // Print all IDATA sections.
Chris Lattnerf0144122009-07-28 03:13:23 +0000382 const std::vector<PIC16Section*> &IDATASections = PTOF->IDATASections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000383 for (unsigned i = 0; i < IDATASections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000384 O << "\n";
Sanjiv Gupta024e94c2009-07-06 18:07:06 +0000385 if (IDATASections[i]->S_->getName().find("llvm.") != std::string::npos)
386 continue;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000387 SwitchToSection(IDATASections[i]->S_);
388 std::vector<const GlobalVariable*> Items = IDATASections[i]->Items;
389 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000390 std::string Name = Mang->getMangledName(Items[j]);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000391 Constant *C = Items[j]->getInitializer();
392 int AddrSpace = Items[j]->getType()->getAddressSpace();
393 O << Name;
394 EmitGlobalConstant(C, AddrSpace);
395 }
396 }
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000397}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000398
Chris Lattnerdc471462009-07-21 16:44:47 +0000399void PIC16AsmPrinter::EmitUData(Module &M) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000400 const TargetData *TD = TM.getTargetData();
401
402 // Print all BSS sections.
Chris Lattnerf0144122009-07-28 03:13:23 +0000403 const std::vector<PIC16Section*> &BSSSections = PTOF->BSSSections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000404 for (unsigned i = 0; i < BSSSections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000405 O << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000406 SwitchToSection(BSSSections[i]->S_);
407 std::vector<const GlobalVariable*> Items = BSSSections[i]->Items;
408 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000409 std::string Name = Mang->getMangledName(Items[j]);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000410 Constant *C = Items[j]->getInitializer();
411 const Type *Ty = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000412 unsigned Size = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000413
Chris Lattnerdc471462009-07-21 16:44:47 +0000414 O << Name << " RES " << Size << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000415 }
416 }
417}
418
Chris Lattnerdc471462009-07-21 16:44:47 +0000419void PIC16AsmPrinter::EmitAutos(std::string FunctName) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000420 // Section names for all globals are already set.
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000421 const TargetData *TD = TM.getTargetData();
422
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000423 // Now print Autos section for this function.
424 std::string SectionName = PAN::getAutosSectionName(FunctName);
Chris Lattnerf0144122009-07-28 03:13:23 +0000425 const std::vector<PIC16Section*> &AutosSections = PTOF->AutosSections;
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000426 for (unsigned i = 0; i < AutosSections.size(); i++) {
427 O << "\n";
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000428 if (AutosSections[i]->S_->getName() == SectionName) {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000429 // Set the printing status to true
430 AutosSections[i]->setPrintedStatus(true);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000431 SwitchToSection(AutosSections[i]->S_);
Chris Lattnerdc471462009-07-21 16:44:47 +0000432 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000433 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000434 std::string VarName = Mang->getMangledName(Items[j]);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000435 Constant *C = Items[j]->getInitializer();
436 const Type *Ty = C->getType();
437 unsigned Size = TD->getTypeAllocSize(Ty);
438 // Emit memory reserve directive.
439 O << VarName << " RES " << Size << "\n";
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000440 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000441 break;
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000442 }
443 }
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000444}
445
Sanjiv Guptab157f252009-06-09 15:31:19 +0000446// Print autos that were not printed during the code printing of functions.
447// As the functions might themselves would have got deleted by the optimizer.
Chris Lattnerdc471462009-07-21 16:44:47 +0000448void PIC16AsmPrinter::EmitRemainingAutos() {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000449 const TargetData *TD = TM.getTargetData();
450
451 // Now print Autos section for this function.
Chris Lattnerf0144122009-07-28 03:13:23 +0000452 std::vector <PIC16Section *>AutosSections = PTOF->AutosSections;
Sanjiv Guptab157f252009-06-09 15:31:19 +0000453 for (unsigned i = 0; i < AutosSections.size(); i++) {
454
455 // if the section is already printed then don't print again
456 if (AutosSections[i]->isPrinted())
457 continue;
458
459 // Set status as printed
460 AutosSections[i]->setPrintedStatus(true);
461
462 O << "\n";
463 SwitchToSection(AutosSections[i]->S_);
Chris Lattnerdc471462009-07-21 16:44:47 +0000464 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
Sanjiv Guptab157f252009-06-09 15:31:19 +0000465 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000466 std::string VarName = Mang->getMangledName(Items[j]);
Sanjiv Guptab157f252009-06-09 15:31:19 +0000467 Constant *C = Items[j]->getInitializer();
468 const Type *Ty = C->getType();
469 unsigned Size = TD->getTypeAllocSize(Ty);
470 // Emit memory reserve directive.
471 O << VarName << " RES " << Size << "\n";
472 }
473 }
474}
475
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000476
477extern "C" void LLVMInitializePIC16Target() {
478 // Register the targets
479 RegisterTargetMachine<PIC16TargetMachine> A(ThePIC16Target);
480 RegisterTargetMachine<CooperTargetMachine> B(TheCooperTarget);
481 RegisterAsmPrinter<PIC16AsmPrinter> C(ThePIC16Target);
482 RegisterAsmPrinter<PIC16AsmPrinter> D(TheCooperTarget);
483}