blob: ec86585c1dba278eacd76af8c38d9b98cbb789b6 [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"
David Greene71847812009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000023#include "llvm/Support/Mangler.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +000025#include "llvm/CodeGen/DwarfWriter.h"
26#include "llvm/CodeGen/MachineModuleInfo.h"
Daniel Dunbar0c795d62009-07-25 06:49:55 +000027#include "llvm/Target/TargetRegistry.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000028
29using namespace llvm;
30
Sanjiv Gupta0e687712008-05-13 09:02:57 +000031#include "PIC16GenAsmWriter.inc"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000032
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000033bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000034 printInstruction(MI);
35 return true;
36}
37
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000038/// runOnMachineFunction - This emits the frame section, autos section and
39/// assembly for each instruction. Also takes care of function begin debug
40/// directive and file begin debug directive (if required) for the function.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000041///
42bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +000043 this->MF = &MF;
44
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000045 // This calls the base class function required to be called at beginning
46 // of runOnMachineFunction.
47 SetupMachineFunction(MF);
48
49 // Get the mangled name.
50 const Function *F = MF.getFunction();
Chris Lattnerb8158ac2009-07-14 18:17:16 +000051 CurrentFnName = Mang->getMangledName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000052
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000053 // Emit the function frame (args and temps).
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000054 EmitFunctionFrame(MF);
55
Sanjiv Guptabde79422009-06-16 09:45:18 +000056 DbgInfo.BeginFunction(MF);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000057
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000058 // Emit the autos section of function.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000059 EmitAutos(CurrentFnName);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000060
61 // Now emit the instructions of function in its code section.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000062 const char *codeSection = PAN::getCodeSectionName(CurrentFnName).c_str();
63
Chris Lattner5fe575f2009-07-27 05:32:16 +000064 const Section *fCodeSection =
Chris Lattnere3466942009-07-27 06:17:14 +000065 TAI->getOrCreateSection(codeSection, false, SectionKind::Text);
Sanjiv Gupta211f3622009-05-10 05:23:47 +000066 // Start the Code Section.
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000067 O << "\n";
Chris Lattnerdc471462009-07-21 16:44:47 +000068 SwitchToSection(fCodeSection);
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000069
70 // Emit the frame address of the function at the beginning of code.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000071 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
72 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000073
Sanjiv Gupta211f3622009-05-10 05:23:47 +000074 // Emit function start label.
75 O << CurrentFnName << ":\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000076
Sanjiv Guptabde79422009-06-16 09:45:18 +000077 DebugLoc CurDL;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000078 O << "\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000079 // Print out code for the function.
80 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
81 I != E; ++I) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000082
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000083 // Print a label for the basic block.
84 if (I != MF.begin()) {
85 printBasicBlockLabel(I, true);
86 O << '\n';
87 }
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000088
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000089 // Print a basic block.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000090 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
91 II != E; ++II) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000092
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000093 // Emit the line directive if source line changed.
94 const DebugLoc DL = II->getDebugLoc();
Sanjiv Guptabde79422009-06-16 09:45:18 +000095 if (!DL.isUnknown() && DL != CurDL) {
96 DbgInfo.ChangeDebugLoc(MF, DL);
97 CurDL = DL;
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000098 }
Sanjiv Gupta0608b492009-05-11 06:01:38 +000099
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000100 // Print the assembly for the instruction.
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000101 printMachineInstruction(II);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000102 }
103 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000104
105 // Emit function end debug directives.
Sanjiv Guptabde79422009-06-16 09:45:18 +0000106 DbgInfo.EndFunction(MF);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000107
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000108 return false; // we didn't modify anything.
109}
110
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000111
112// printOperand - print operand of insn.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000113void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000114 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000115
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000116 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000117 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000118 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000119 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000120 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000121 llvm_unreachable("not implemented");
Torok Edwinc25e7582009-07-11 20:10:48 +0000122 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000123
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000124 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000125 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000126 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000127
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000128 case MachineOperand::MO_GlobalAddress: {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000129 O << Mang->getMangledName(MO.getGlobal());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000130 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000131 }
132 case MachineOperand::MO_ExternalSymbol: {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000133 const char *Sname = MO.getSymbolName();
134
135 // If its a libcall name, record it to decls section.
136 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000137 LibcallDecls.push_back(Sname);
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000138 }
139
140 O << Sname;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000141 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000142 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000143 case MachineOperand::MO_MachineBasicBlock:
144 printBasicBlockLabel(MO.getMBB());
145 return;
146
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000147 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000148 llvm_unreachable(" Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000149 }
150}
151
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000152/// printCCOperand - Print the cond code operand.
153///
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000154void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
155 int CC = (int)MI->getOperand(opNum).getImm();
156 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
157}
158
Sanjiv Guptabe7b97b2009-07-27 18:04:34 +0000159// This function is used to sort the decls list.
160// should return true if s1 should come before s2.
161static bool is_before(const char *s1, const char *s2) {
162 std::string str1 = s1;
163 std::string str2 = s2;
164 int i = str1.compare(str2);
165 // Return true if s1 is smaller or equal.
166 if (i <= 0) return true;
167 // false if s1 should come after s2.
168 return false;
169}
170
171// This is used by list::unique below.
172// unique will filter out duplicates if it knows them.
173static bool is_duplicate(const char *s1, const char *s2) {
174 std::string str1 = s1;
175 std::string str2 = s2;
176 return str1 == str2;
177}
178
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000179/// printLibcallDecls - print the extern declarations for compiler
180/// intrinsics.
181///
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000182void PIC16AsmPrinter::printLibcallDecls(void) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000183 // If no libcalls used, return.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000184 if (LibcallDecls.empty()) return;
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000185
Sanjiv Gupta42918572009-05-12 06:52:41 +0000186 O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000187 // Remove duplicate entries.
Sanjiv Guptabe7b97b2009-07-27 18:04:34 +0000188 LibcallDecls.sort(is_before);
189 LibcallDecls.unique(is_duplicate);
190
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000191 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
192 I != LibcallDecls.end(); I++) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000193 O << TAI->getExternDirective() << *I << "\n";
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000194 O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
195 O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000196 }
Sanjiv Gupta42918572009-05-12 06:52:41 +0000197 O << TAI->getCommentString() << "External decls for libcalls - END." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000198}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000199
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000200/// doInitialization - Perfrom Module level initializations here.
201/// One task that we do here is to sectionize all global variables.
202/// The MemSelOptimizer pass depends on the sectionizing.
203///
Chris Lattnerdc471462009-07-21 16:44:47 +0000204bool PIC16AsmPrinter::doInitialization(Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000205 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000206
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000207 // FIXME:: This is temporary solution to generate the include file.
208 // The processor should be passed to llc as in input and the header file
209 // should be generated accordingly.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000210 O << "\n\t#include P16F1937.INC\n";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000211
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000212 // Set the section names for all globals.
213 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
214 I != E; ++I) {
215 I->setSection(TAI->SectionForGlobal(I)->getName());
216 }
217
Sanjiv Guptabde79422009-06-16 09:45:18 +0000218 DbgInfo.BeginModule(M);
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000219 EmitFunctionDecls(M);
220 EmitUndefinedVars(M);
221 EmitDefinedVars(M);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000222 EmitIData(M);
223 EmitUData(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000224 EmitRomData(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000225 return Result;
226}
227
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000228/// Emit extern decls for functions imported from other modules, and emit
229/// global declarations for function defined in this module and which are
230/// available to other modules.
231///
Chris Lattnerdc471462009-07-21 16:44:47 +0000232void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000233 // Emit declarations for external functions.
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000234 O <<"\n"<<TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000235 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
Sanjiv Gupta0f009d22009-07-23 02:11:04 +0000236 if (I->isIntrinsic())
237 continue;
238
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000239 std::string Name = Mang->getMangledName(I);
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000240 if (Name.compare("@abort") == 0)
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000241 continue;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000242
Chris Lattnerdc471462009-07-21 16:44:47 +0000243 if (!I->isDeclaration() && !I->hasExternalLinkage())
Sanjiv Guptaaf3fdb52009-05-10 16:18:39 +0000244 continue;
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000245
246 const char *directive = I->isDeclaration() ? TAI->getExternDirective() :
247 TAI->getGlobalDirective();
248
249 O << directive << Name << "\n";
250 O << directive << PAN::getRetvalLabel(Name) << "\n";
251 O << directive << PAN::getArgsLabel(Name) << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000252 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000253
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000254 O << TAI->getCommentString() << "Function Declarations - END." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000255}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000256
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000257// Emit variables imported from other Modules.
Chris Lattnerdc471462009-07-21 16:44:47 +0000258void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000259 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDecls->Items;
Chris Lattnerdc471462009-07-21 16:44:47 +0000260 if (!Items.size()) return;
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000261
262 O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
263 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000264 O << TAI->getExternDirective() << Mang->getMangledName(Items[j]) << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000265 }
266 O << TAI->getCommentString() << "Imported Variables - END" << "\n";
267}
268
269// Emit variables defined in this module and are available to other modules.
Chris Lattnerdc471462009-07-21 16:44:47 +0000270void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000271 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDefs->Items;
Chris Lattnerdc471462009-07-21 16:44:47 +0000272 if (!Items.size()) return;
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000273
Chris Lattnera46cb522009-07-21 17:20:18 +0000274 O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000275 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000276 O << TAI->getGlobalDirective() << Mang->getMangledName(Items[j]) << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000277 }
278 O << TAI->getCommentString() << "Exported Variables - END" << "\n";
279}
280
281// Emit initialized data placed in ROM.
Chris Lattnerdc471462009-07-21 16:44:47 +0000282void PIC16AsmPrinter::EmitRomData(Module &M) {
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000283 // Print ROM Data section.
Chris Lattnerdc471462009-07-21 16:44:47 +0000284 const std::vector<PIC16Section*> &ROSections = PTAI->ROSections;
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000285 for (unsigned i = 0; i < ROSections.size(); i++) {
Chris Lattnerdc471462009-07-21 16:44:47 +0000286 const std::vector<const GlobalVariable*> &Items = ROSections[i]->Items;
287 if (!Items.size()) continue;
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000288 O << "\n";
289 SwitchToSection(PTAI->ROSections[i]->S_);
290 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000291 O << Mang->getMangledName(Items[j]);
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000292 Constant *C = Items[j]->getInitializer();
293 int AddrSpace = Items[j]->getType()->getAddressSpace();
294 EmitGlobalConstant(C, AddrSpace);
295 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000296 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000297}
298
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000299bool PIC16AsmPrinter::doFinalization(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000300 printLibcallDecls();
Sanjiv Guptab157f252009-06-09 15:31:19 +0000301 EmitRemainingAutos();
Sanjiv Guptabde79422009-06-16 09:45:18 +0000302 DbgInfo.EndModule(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000303 O << "\n\t" << "END\n";
Chris Lattner40bbebd2009-07-21 18:38:57 +0000304 return AsmPrinter::doFinalization(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000305}
306
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000307void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000308 const Function *F = MF.getFunction();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000309 std::string FuncName = Mang->getMangledName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000310 const TargetData *TD = TM.getTargetData();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000311 // Emit the data section name.
312 O << "\n";
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000313 const char *SectionName = PAN::getFrameSectionName(CurrentFnName).c_str();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000314
Chris Lattner5fe575f2009-07-27 05:32:16 +0000315 const Section *fPDataSection =
Chris Lattnere3466942009-07-27 06:17:14 +0000316 TAI->getOrCreateSection(SectionName, false, SectionKind::DataRel);
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000317 SwitchToSection(fPDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000318
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000319 // Emit function frame label
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000320 O << PAN::getFrameLabel(CurrentFnName) << ":\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000321
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000322 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000323 unsigned RetSize = 0;
324 if (RetType->getTypeID() != Type::VoidTyID)
Duncan Sands777d2302009-05-09 07:06:46 +0000325 RetSize = TD->getTypeAllocSize(RetType);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000326
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000327 //Emit function return value space
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000328 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
329 // we will need to avoid printing a global directive for Retval label
330 // in emitExternandGloblas.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000331 if(RetSize > 0)
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000332 O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000333 else
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000334 O << PAN::getRetvalLabel(CurrentFnName) << ": \n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000335
336 // Emit variable to hold the space for function arguments
337 unsigned ArgSize = 0;
338 for (Function::const_arg_iterator argi = F->arg_begin(),
339 arge = F->arg_end(); argi != arge ; ++argi) {
340 const Type *Ty = argi->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000341 ArgSize += TD->getTypeAllocSize(Ty);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000342 }
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000343
344 O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000345
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000346 // Emit temporary space
347 int TempSize = PTLI->GetTmpSize();
Chris Lattnerdc471462009-07-21 16:44:47 +0000348 if (TempSize > 0)
349 O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize << '\n';
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000350}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000351
Chris Lattnerdc471462009-07-21 16:44:47 +0000352void PIC16AsmPrinter::EmitIData(Module &M) {
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000353
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000354 // Print all IDATA sections.
Chris Lattnerdc471462009-07-21 16:44:47 +0000355 const std::vector<PIC16Section*> &IDATASections = PTAI->IDATASections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000356 for (unsigned i = 0; i < IDATASections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000357 O << "\n";
Sanjiv Gupta024e94c2009-07-06 18:07:06 +0000358 if (IDATASections[i]->S_->getName().find("llvm.") != std::string::npos)
359 continue;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000360 SwitchToSection(IDATASections[i]->S_);
361 std::vector<const GlobalVariable*> Items = IDATASections[i]->Items;
362 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000363 std::string Name = Mang->getMangledName(Items[j]);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000364 Constant *C = Items[j]->getInitializer();
365 int AddrSpace = Items[j]->getType()->getAddressSpace();
366 O << Name;
367 EmitGlobalConstant(C, AddrSpace);
368 }
369 }
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000370}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000371
Chris Lattnerdc471462009-07-21 16:44:47 +0000372void PIC16AsmPrinter::EmitUData(Module &M) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000373 const TargetData *TD = TM.getTargetData();
374
375 // Print all BSS sections.
Chris Lattnerdc471462009-07-21 16:44:47 +0000376 const std::vector<PIC16Section*> &BSSSections = PTAI->BSSSections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000377 for (unsigned i = 0; i < BSSSections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000378 O << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000379 SwitchToSection(BSSSections[i]->S_);
380 std::vector<const GlobalVariable*> Items = BSSSections[i]->Items;
381 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000382 std::string Name = Mang->getMangledName(Items[j]);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000383 Constant *C = Items[j]->getInitializer();
384 const Type *Ty = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000385 unsigned Size = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000386
Chris Lattnerdc471462009-07-21 16:44:47 +0000387 O << Name << " RES " << Size << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000388 }
389 }
390}
391
Chris Lattnerdc471462009-07-21 16:44:47 +0000392void PIC16AsmPrinter::EmitAutos(std::string FunctName) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000393 // Section names for all globals are already set.
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000394 const TargetData *TD = TM.getTargetData();
395
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000396 // Now print Autos section for this function.
397 std::string SectionName = PAN::getAutosSectionName(FunctName);
Chris Lattnerdc471462009-07-21 16:44:47 +0000398 const std::vector<PIC16Section*> &AutosSections = PTAI->AutosSections;
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000399 for (unsigned i = 0; i < AutosSections.size(); i++) {
400 O << "\n";
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000401 if (AutosSections[i]->S_->getName() == SectionName) {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000402 // Set the printing status to true
403 AutosSections[i]->setPrintedStatus(true);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000404 SwitchToSection(AutosSections[i]->S_);
Chris Lattnerdc471462009-07-21 16:44:47 +0000405 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000406 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000407 std::string VarName = Mang->getMangledName(Items[j]);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000408 Constant *C = Items[j]->getInitializer();
409 const Type *Ty = C->getType();
410 unsigned Size = TD->getTypeAllocSize(Ty);
411 // Emit memory reserve directive.
412 O << VarName << " RES " << Size << "\n";
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000413 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000414 break;
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000415 }
416 }
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000417}
418
Sanjiv Guptab157f252009-06-09 15:31:19 +0000419// Print autos that were not printed during the code printing of functions.
420// As the functions might themselves would have got deleted by the optimizer.
Chris Lattnerdc471462009-07-21 16:44:47 +0000421void PIC16AsmPrinter::EmitRemainingAutos() {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000422 const TargetData *TD = TM.getTargetData();
423
424 // Now print Autos section for this function.
425 std::vector <PIC16Section *>AutosSections = PTAI->AutosSections;
426 for (unsigned i = 0; i < AutosSections.size(); i++) {
427
428 // if the section is already printed then don't print again
429 if (AutosSections[i]->isPrinted())
430 continue;
431
432 // Set status as printed
433 AutosSections[i]->setPrintedStatus(true);
434
435 O << "\n";
436 SwitchToSection(AutosSections[i]->S_);
Chris Lattnerdc471462009-07-21 16:44:47 +0000437 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
Sanjiv Guptab157f252009-06-09 15:31:19 +0000438 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000439 std::string VarName = Mang->getMangledName(Items[j]);
Sanjiv Guptab157f252009-06-09 15:31:19 +0000440 Constant *C = Items[j]->getInitializer();
441 const Type *Ty = C->getType();
442 unsigned Size = TD->getTypeAllocSize(Ty);
443 // Emit memory reserve directive.
444 O << VarName << " RES " << Size << "\n";
445 }
446 }
447}
448
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000449
450extern "C" void LLVMInitializePIC16Target() {
451 // Register the targets
452 RegisterTargetMachine<PIC16TargetMachine> A(ThePIC16Target);
453 RegisterTargetMachine<CooperTargetMachine> B(TheCooperTarget);
454 RegisterAsmPrinter<PIC16AsmPrinter> C(ThePIC16Target);
455 RegisterAsmPrinter<PIC16AsmPrinter> D(TheCooperTarget);
456}