blob: 084038d6dd85f73fa5a97b0c7d4b0d258131c693 [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"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000027
28using namespace llvm;
29
Sanjiv Gupta0e687712008-05-13 09:02:57 +000030#include "PIC16GenAsmWriter.inc"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000031
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000032bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000033 printInstruction(MI);
34 return true;
35}
36
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000037/// runOnMachineFunction - This emits the frame section, autos section and
38/// assembly for each instruction. Also takes care of function begin debug
39/// directive and file begin debug directive (if required) for the function.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000040///
41bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +000042 this->MF = &MF;
43
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000044 // This calls the base class function required to be called at beginning
45 // of runOnMachineFunction.
46 SetupMachineFunction(MF);
47
48 // Get the mangled name.
49 const Function *F = MF.getFunction();
Chris Lattnerb8158ac2009-07-14 18:17:16 +000050 CurrentFnName = Mang->getMangledName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000051
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000052 // Emit the function frame (args and temps).
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000053 EmitFunctionFrame(MF);
54
Sanjiv Guptabde79422009-06-16 09:45:18 +000055 DbgInfo.BeginFunction(MF);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000056
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000057 // Emit the autos section of function.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000058 EmitAutos(CurrentFnName);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000059
60 // Now emit the instructions of function in its code section.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000061 const char *codeSection = PAN::getCodeSectionName(CurrentFnName).c_str();
62
63 const Section *fCodeSection = TAI->getNamedSection(codeSection,
64 SectionFlags::Code);
Sanjiv Gupta211f3622009-05-10 05:23:47 +000065 // Start the Code Section.
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000066 O << "\n";
Chris Lattnerdc471462009-07-21 16:44:47 +000067 SwitchToSection(fCodeSection);
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000068
69 // Emit the frame address of the function at the beginning of code.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000070 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
71 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000072
Sanjiv Gupta211f3622009-05-10 05:23:47 +000073 // Emit function start label.
74 O << CurrentFnName << ":\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000075
Sanjiv Guptabde79422009-06-16 09:45:18 +000076 DebugLoc CurDL;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000077 O << "\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000078 // Print out code for the function.
79 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
80 I != E; ++I) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000081
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000082 // Print a label for the basic block.
83 if (I != MF.begin()) {
84 printBasicBlockLabel(I, true);
85 O << '\n';
86 }
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000087
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000088 // Print a basic block.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000089 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
90 II != E; ++II) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000091
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000092 // Emit the line directive if source line changed.
93 const DebugLoc DL = II->getDebugLoc();
Sanjiv Guptabde79422009-06-16 09:45:18 +000094 if (!DL.isUnknown() && DL != CurDL) {
95 DbgInfo.ChangeDebugLoc(MF, DL);
96 CurDL = DL;
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000097 }
Sanjiv Gupta0608b492009-05-11 06:01:38 +000098
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000099 // Print the assembly for the instruction.
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000100 printMachineInstruction(II);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000101 }
102 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000103
104 // Emit function end debug directives.
Sanjiv Guptabde79422009-06-16 09:45:18 +0000105 DbgInfo.EndFunction(MF);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000106
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000107 return false; // we didn't modify anything.
108}
109
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000110/// createPIC16CodePrinterPass - Returns a pass that prints the PIC16
111/// assembly code for a MachineFunction to the given output stream,
112/// using the given target machine description. This should work
113/// regardless of whether the function is in SSA form.
114///
David Greene71847812009-07-14 20:18:05 +0000115FunctionPass *llvm::createPIC16CodePrinterPass(formatted_raw_ostream &o,
Daniel Dunbar1e1f8ba2009-07-15 23:17:20 +0000116 TargetMachine &tm,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000117 bool verbose) {
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000118 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000119}
120
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000121
122// printOperand - print operand of insn.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000123void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000124 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000125
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000126 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000127 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000128 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000129 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000130 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000131 llvm_unreachable("not implemented");
Torok Edwinc25e7582009-07-11 20:10:48 +0000132 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000133
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000134 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000135 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000136 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000137
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000138 case MachineOperand::MO_GlobalAddress: {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000139 O << Mang->getMangledName(MO.getGlobal());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000140 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000141 }
142 case MachineOperand::MO_ExternalSymbol: {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000143 const char *Sname = MO.getSymbolName();
144
145 // If its a libcall name, record it to decls section.
146 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000147 LibcallDecls.push_back(Sname);
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000148 }
149
150 O << Sname;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000151 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000152 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000153 case MachineOperand::MO_MachineBasicBlock:
154 printBasicBlockLabel(MO.getMBB());
155 return;
156
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000157 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000158 llvm_unreachable(" Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000159 }
160}
161
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000162/// printCCOperand - Print the cond code operand.
163///
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000164void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
165 int CC = (int)MI->getOperand(opNum).getImm();
166 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
167}
168
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000169/// printLibcallDecls - print the extern declarations for compiler
170/// intrinsics.
171///
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000172void PIC16AsmPrinter::printLibcallDecls(void) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000173 // If no libcalls used, return.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000174 if (LibcallDecls.empty()) return;
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000175
Sanjiv Gupta42918572009-05-12 06:52:41 +0000176 O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000177 // Remove duplicate entries.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000178 LibcallDecls.sort();
179 LibcallDecls.unique();
180 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
181 I != LibcallDecls.end(); I++) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000182 O << TAI->getExternDirective() << *I << "\n";
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000183 O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
184 O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000185 }
Sanjiv Gupta42918572009-05-12 06:52:41 +0000186 O << TAI->getCommentString() << "External decls for libcalls - END." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000187}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000188
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000189/// doInitialization - Perfrom Module level initializations here.
190/// One task that we do here is to sectionize all global variables.
191/// The MemSelOptimizer pass depends on the sectionizing.
192///
Chris Lattnerdc471462009-07-21 16:44:47 +0000193bool PIC16AsmPrinter::doInitialization(Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000194 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000195
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000196 // FIXME:: This is temporary solution to generate the include file.
197 // The processor should be passed to llc as in input and the header file
198 // should be generated accordingly.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000199 O << "\n\t#include P16F1937.INC\n";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000200
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000201 // Set the section names for all globals.
202 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
203 I != E; ++I) {
204 I->setSection(TAI->SectionForGlobal(I)->getName());
205 }
206
Sanjiv Guptabde79422009-06-16 09:45:18 +0000207 DbgInfo.BeginModule(M);
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000208 EmitFunctionDecls(M);
209 EmitUndefinedVars(M);
210 EmitDefinedVars(M);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000211 EmitIData(M);
212 EmitUData(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000213 EmitRomData(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000214 return Result;
215}
216
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000217/// Emit extern decls for functions imported from other modules, and emit
218/// global declarations for function defined in this module and which are
219/// available to other modules.
220///
Chris Lattnerdc471462009-07-21 16:44:47 +0000221void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000222 // Emit declarations for external functions.
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000223 O <<"\n"<<TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000224 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000225 std::string Name = Mang->getMangledName(I);
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000226 if (Name.compare("@abort") == 0)
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000227 continue;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000228
229 // If it is llvm intrinsic call then don't emit
230 if (Name.find("llvm.") != std::string::npos)
231 continue;
232
Chris Lattnerdc471462009-07-21 16:44:47 +0000233 if (!I->isDeclaration() && !I->hasExternalLinkage())
Sanjiv Guptaaf3fdb52009-05-10 16:18:39 +0000234 continue;
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000235
236 const char *directive = I->isDeclaration() ? TAI->getExternDirective() :
237 TAI->getGlobalDirective();
238
239 O << directive << Name << "\n";
240 O << directive << PAN::getRetvalLabel(Name) << "\n";
241 O << directive << PAN::getArgsLabel(Name) << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000242 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000243
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000244 O << TAI->getCommentString() << "Function Declarations - END." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000245}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000246
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000247// Emit variables imported from other Modules.
Chris Lattnerdc471462009-07-21 16:44:47 +0000248void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000249 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDecls->Items;
Chris Lattnerdc471462009-07-21 16:44:47 +0000250 if (!Items.size()) return;
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000251
252 O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
253 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000254 O << TAI->getExternDirective() << Mang->getMangledName(Items[j]) << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000255 }
256 O << TAI->getCommentString() << "Imported Variables - END" << "\n";
257}
258
259// Emit variables defined in this module and are available to other modules.
Chris Lattnerdc471462009-07-21 16:44:47 +0000260void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000261 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDefs->Items;
Chris Lattnerdc471462009-07-21 16:44:47 +0000262 if (!Items.size()) return;
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000263
264 O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
265 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000266 O << TAI->getGlobalDirective() << Mang->getMangledName(Items[j]) << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000267 }
268 O << TAI->getCommentString() << "Exported Variables - END" << "\n";
269}
270
271// Emit initialized data placed in ROM.
Chris Lattnerdc471462009-07-21 16:44:47 +0000272void PIC16AsmPrinter::EmitRomData(Module &M) {
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000273 // Print ROM Data section.
Chris Lattnerdc471462009-07-21 16:44:47 +0000274 const std::vector<PIC16Section*> &ROSections = PTAI->ROSections;
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000275 for (unsigned i = 0; i < ROSections.size(); i++) {
Chris Lattnerdc471462009-07-21 16:44:47 +0000276 const std::vector<const GlobalVariable*> &Items = ROSections[i]->Items;
277 if (!Items.size()) continue;
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000278 O << "\n";
279 SwitchToSection(PTAI->ROSections[i]->S_);
280 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000281 O << Mang->getMangledName(Items[j]);
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000282 Constant *C = Items[j]->getInitializer();
283 int AddrSpace = Items[j]->getType()->getAddressSpace();
284 EmitGlobalConstant(C, AddrSpace);
285 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000286 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000287}
288
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000289bool PIC16AsmPrinter::doFinalization(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000290 printLibcallDecls();
Sanjiv Guptab157f252009-06-09 15:31:19 +0000291 EmitRemainingAutos();
Sanjiv Guptabde79422009-06-16 09:45:18 +0000292 DbgInfo.EndModule(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000293 O << "\n\t" << "END\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000294 bool Result = AsmPrinter::doFinalization(M);
295 return Result;
296}
297
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000298void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000299 const Function *F = MF.getFunction();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000300 std::string FuncName = Mang->getMangledName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000301 const TargetData *TD = TM.getTargetData();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000302 // Emit the data section name.
303 O << "\n";
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000304 const char *SectionName = PAN::getFrameSectionName(CurrentFnName).c_str();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000305
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000306 const Section *fPDataSection = TAI->getNamedSection(SectionName,
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000307 SectionFlags::Writeable);
308 SwitchToSection(fPDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000309
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000310 // Emit function frame label
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000311 O << PAN::getFrameLabel(CurrentFnName) << ":\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000312
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000313 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000314 unsigned RetSize = 0;
315 if (RetType->getTypeID() != Type::VoidTyID)
Duncan Sands777d2302009-05-09 07:06:46 +0000316 RetSize = TD->getTypeAllocSize(RetType);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000317
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000318 //Emit function return value space
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000319 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
320 // we will need to avoid printing a global directive for Retval label
321 // in emitExternandGloblas.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000322 if(RetSize > 0)
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000323 O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000324 else
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000325 O << PAN::getRetvalLabel(CurrentFnName) << ": \n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000326
327 // Emit variable to hold the space for function arguments
328 unsigned ArgSize = 0;
329 for (Function::const_arg_iterator argi = F->arg_begin(),
330 arge = F->arg_end(); argi != arge ; ++argi) {
331 const Type *Ty = argi->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000332 ArgSize += TD->getTypeAllocSize(Ty);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000333 }
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000334
335 O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000336
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000337 // Emit temporary space
338 int TempSize = PTLI->GetTmpSize();
Chris Lattnerdc471462009-07-21 16:44:47 +0000339 if (TempSize > 0)
340 O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize << '\n';
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000341}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000342
Chris Lattnerdc471462009-07-21 16:44:47 +0000343void PIC16AsmPrinter::EmitIData(Module &M) {
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000344
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000345 // Print all IDATA sections.
Chris Lattnerdc471462009-07-21 16:44:47 +0000346 const std::vector<PIC16Section*> &IDATASections = PTAI->IDATASections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000347 for (unsigned i = 0; i < IDATASections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000348 O << "\n";
Sanjiv Gupta024e94c2009-07-06 18:07:06 +0000349 if (IDATASections[i]->S_->getName().find("llvm.") != std::string::npos)
350 continue;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000351 SwitchToSection(IDATASections[i]->S_);
352 std::vector<const GlobalVariable*> Items = IDATASections[i]->Items;
353 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000354 std::string Name = Mang->getMangledName(Items[j]);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000355 Constant *C = Items[j]->getInitializer();
356 int AddrSpace = Items[j]->getType()->getAddressSpace();
357 O << Name;
358 EmitGlobalConstant(C, AddrSpace);
359 }
360 }
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000361}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000362
Chris Lattnerdc471462009-07-21 16:44:47 +0000363void PIC16AsmPrinter::EmitUData(Module &M) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000364 const TargetData *TD = TM.getTargetData();
365
366 // Print all BSS sections.
Chris Lattnerdc471462009-07-21 16:44:47 +0000367 const std::vector<PIC16Section*> &BSSSections = PTAI->BSSSections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000368 for (unsigned i = 0; i < BSSSections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000369 O << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000370 SwitchToSection(BSSSections[i]->S_);
371 std::vector<const GlobalVariable*> Items = BSSSections[i]->Items;
372 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000373 std::string Name = Mang->getMangledName(Items[j]);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000374 Constant *C = Items[j]->getInitializer();
375 const Type *Ty = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000376 unsigned Size = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000377
Chris Lattnerdc471462009-07-21 16:44:47 +0000378 O << Name << " RES " << Size << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000379 }
380 }
381}
382
Chris Lattnerdc471462009-07-21 16:44:47 +0000383void PIC16AsmPrinter::EmitAutos(std::string FunctName) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000384 // Section names for all globals are already set.
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000385 const TargetData *TD = TM.getTargetData();
386
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000387 // Now print Autos section for this function.
388 std::string SectionName = PAN::getAutosSectionName(FunctName);
Chris Lattnerdc471462009-07-21 16:44:47 +0000389 const std::vector<PIC16Section*> &AutosSections = PTAI->AutosSections;
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000390 for (unsigned i = 0; i < AutosSections.size(); i++) {
391 O << "\n";
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000392 if (AutosSections[i]->S_->getName() == SectionName) {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000393 // Set the printing status to true
394 AutosSections[i]->setPrintedStatus(true);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000395 SwitchToSection(AutosSections[i]->S_);
Chris Lattnerdc471462009-07-21 16:44:47 +0000396 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000397 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000398 std::string VarName = Mang->getMangledName(Items[j]);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000399 Constant *C = Items[j]->getInitializer();
400 const Type *Ty = C->getType();
401 unsigned Size = TD->getTypeAllocSize(Ty);
402 // Emit memory reserve directive.
403 O << VarName << " RES " << Size << "\n";
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000404 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000405 break;
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000406 }
407 }
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000408}
409
Sanjiv Guptab157f252009-06-09 15:31:19 +0000410// Print autos that were not printed during the code printing of functions.
411// As the functions might themselves would have got deleted by the optimizer.
Chris Lattnerdc471462009-07-21 16:44:47 +0000412void PIC16AsmPrinter::EmitRemainingAutos() {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000413 const TargetData *TD = TM.getTargetData();
414
415 // Now print Autos section for this function.
416 std::vector <PIC16Section *>AutosSections = PTAI->AutosSections;
417 for (unsigned i = 0; i < AutosSections.size(); i++) {
418
419 // if the section is already printed then don't print again
420 if (AutosSections[i]->isPrinted())
421 continue;
422
423 // Set status as printed
424 AutosSections[i]->setPrintedStatus(true);
425
426 O << "\n";
427 SwitchToSection(AutosSections[i]->S_);
Chris Lattnerdc471462009-07-21 16:44:47 +0000428 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
Sanjiv Guptab157f252009-06-09 15:31:19 +0000429 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000430 std::string VarName = Mang->getMangledName(Items[j]);
Sanjiv Guptab157f252009-06-09 15:31:19 +0000431 Constant *C = Items[j]->getInitializer();
432 const Type *Ty = C->getType();
433 unsigned Size = TD->getTypeAllocSize(Ty);
434 // Emit memory reserve directive.
435 O << VarName << " RES " << Size << "\n";
436 }
437 }
438}
439