blob: 6466ad6a22ec4561e9459cf8ea0a2cdb4a4e4df3 [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"
Bill Wendlingcb819f12009-02-18 23:12:06 +000022#include "llvm/Support/raw_ostream.h"
23#include "llvm/Support/Mangler.h"
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +000024#include "llvm/CodeGen/DwarfWriter.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000026
27using namespace llvm;
28
Sanjiv Gupta0e687712008-05-13 09:02:57 +000029#include "PIC16GenAsmWriter.inc"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000030
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000031bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000032 printInstruction(MI);
33 return true;
34}
35
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000036/// runOnMachineFunction - This emits the frame section, autos section and
37/// assembly for each instruction. Also takes care of function begin debug
38/// directive and file begin debug directive (if required) for the function.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000039///
40bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +000041 this->MF = &MF;
42
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000043 // This calls the base class function required to be called at beginning
44 // of runOnMachineFunction.
45 SetupMachineFunction(MF);
46
47 // Get the mangled name.
48 const Function *F = MF.getFunction();
49 CurrentFnName = Mang->getValueName(F);
50
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000051 // Emit the function frame (args and temps).
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000052 EmitFunctionFrame(MF);
53
Sanjiv Guptabde79422009-06-16 09:45:18 +000054 DbgInfo.BeginFunction(MF);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000055
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000056 // Emit the autos section of function.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000057 EmitAutos(CurrentFnName);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000058
59 // Now emit the instructions of function in its code section.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000060 const char *codeSection = PAN::getCodeSectionName(CurrentFnName).c_str();
61
62 const Section *fCodeSection = TAI->getNamedSection(codeSection,
63 SectionFlags::Code);
Sanjiv Gupta211f3622009-05-10 05:23:47 +000064 // Start the Code Section.
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000065 O << "\n";
Sanjiv Gupta1b046942009-01-13 19:18:47 +000066 SwitchToSection (fCodeSection);
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000067
68 // Emit the frame address of the function at the beginning of code.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000069 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
70 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000071
Sanjiv Gupta211f3622009-05-10 05:23:47 +000072 // Emit function start label.
73 O << CurrentFnName << ":\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000074
Sanjiv Guptabde79422009-06-16 09:45:18 +000075 DebugLoc CurDL;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000076 O << "\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000077 // Print out code for the function.
78 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
79 I != E; ++I) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000080
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000081 // Print a label for the basic block.
82 if (I != MF.begin()) {
83 printBasicBlockLabel(I, true);
84 O << '\n';
85 }
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000086
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000087 // Print a basic block.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000088 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
89 II != E; ++II) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000090
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000091 // Emit the line directive if source line changed.
92 const DebugLoc DL = II->getDebugLoc();
Sanjiv Guptabde79422009-06-16 09:45:18 +000093 if (!DL.isUnknown() && DL != CurDL) {
94 DbgInfo.ChangeDebugLoc(MF, DL);
95 CurDL = DL;
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000096 }
Sanjiv Gupta0608b492009-05-11 06:01:38 +000097
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000098 // Print the assembly for the instruction.
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000099 printMachineInstruction(II);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000100 }
101 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000102
103 // Emit function end debug directives.
Sanjiv Guptabde79422009-06-16 09:45:18 +0000104 DbgInfo.EndFunction(MF);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000105
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000106 return false; // we didn't modify anything.
107}
108
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000109/// createPIC16CodePrinterPass - Returns a pass that prints the PIC16
110/// assembly code for a MachineFunction to the given output stream,
111/// using the given target machine description. This should work
112/// regardless of whether the function is in SSA form.
113///
Owen Andersoncb371882008-08-21 00:14:44 +0000114FunctionPass *llvm::createPIC16CodePrinterPass(raw_ostream &o,
Bill Wendling57f0db82009-02-24 08:30:20 +0000115 PIC16TargetMachine &tm,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000116 bool verbose) {
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +0000117 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000118}
119
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000120
121// printOperand - print operand of insn.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000122void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000123 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000124
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000125 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000126 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000127 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000128 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000129 else
130 assert(0 && "not implemented");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000131 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000132
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000133 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000134 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000135 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000136
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000137 case MachineOperand::MO_GlobalAddress: {
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000138 O << Mang->getValueName(MO.getGlobal());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000139 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000140 }
141 case MachineOperand::MO_ExternalSymbol: {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000142 const char *Sname = MO.getSymbolName();
143
144 // If its a libcall name, record it to decls section.
145 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000146 LibcallDecls.push_back(Sname);
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000147 }
148
149 O << Sname;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000150 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000151 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000152 case MachineOperand::MO_MachineBasicBlock:
153 printBasicBlockLabel(MO.getMBB());
154 return;
155
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000156 default:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000157 assert(0 && " Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000158 }
159}
160
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000161/// printCCOperand - Print the cond code operand.
162///
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000163void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
164 int CC = (int)MI->getOperand(opNum).getImm();
165 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
166}
167
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000168/// printLibcallDecls - print the extern declarations for compiler
169/// intrinsics.
170///
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000171void PIC16AsmPrinter::printLibcallDecls(void) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000172 // If no libcalls used, return.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000173 if (LibcallDecls.empty()) return;
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000174
Sanjiv Gupta42918572009-05-12 06:52:41 +0000175 O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000176 // Remove duplicate entries.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000177 LibcallDecls.sort();
178 LibcallDecls.unique();
179 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
180 I != LibcallDecls.end(); I++) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000181 O << TAI->getExternDirective() << *I << "\n";
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000182 O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
183 O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000184 }
Sanjiv Gupta42918572009-05-12 06:52:41 +0000185 O << TAI->getCommentString() << "External decls for libcalls - END." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000186}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000187
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000188/// doInitialization - Perfrom Module level initializations here.
189/// One task that we do here is to sectionize all global variables.
190/// The MemSelOptimizer pass depends on the sectionizing.
191///
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000192bool PIC16AsmPrinter::doInitialization (Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000193 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000194
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000195 // FIXME:: This is temporary solution to generate the include file.
196 // The processor should be passed to llc as in input and the header file
197 // should be generated accordingly.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000198 O << "\n\t#include P16F1937.INC\n";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000199
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000200 // Set the section names for all globals.
201 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
202 I != E; ++I) {
203 I->setSection(TAI->SectionForGlobal(I)->getName());
204 }
205
Sanjiv Guptabde79422009-06-16 09:45:18 +0000206 DbgInfo.BeginModule(M);
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000207 EmitFunctionDecls(M);
208 EmitUndefinedVars(M);
209 EmitDefinedVars(M);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000210 EmitIData(M);
211 EmitUData(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000212 EmitRomData(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000213 return Result;
214}
215
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000216/// Emit extern decls for functions imported from other modules, and emit
217/// global declarations for function defined in this module and which are
218/// available to other modules.
219///
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000220void PIC16AsmPrinter::EmitFunctionDecls (Module &M) {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000221 // Emit declarations for external functions.
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000222 O <<"\n"<<TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000223 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
224 std::string Name = Mang->getValueName(I);
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000225 if (Name.compare("@abort") == 0)
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000226 continue;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000227
228 // If it is llvm intrinsic call then don't emit
229 if (Name.find("llvm.") != std::string::npos)
230 continue;
231
Sanjiv Guptaaf3fdb52009-05-10 16:18:39 +0000232 if (! (I->isDeclaration() || I->hasExternalLinkage()))
233 continue;
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000234
235 const char *directive = I->isDeclaration() ? TAI->getExternDirective() :
236 TAI->getGlobalDirective();
237
238 O << directive << Name << "\n";
239 O << directive << PAN::getRetvalLabel(Name) << "\n";
240 O << directive << PAN::getArgsLabel(Name) << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000241 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000242
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000243 O << TAI->getCommentString() << "Function Declarations - END." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000244}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000245
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000246// Emit variables imported from other Modules.
247void PIC16AsmPrinter::EmitUndefinedVars (Module &M)
248{
249 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDecls->Items;
250 if (! Items.size()) return;
251
252 O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
253 for (unsigned j = 0; j < Items.size(); j++) {
254 O << TAI->getExternDirective() << Mang->getValueName(Items[j]) << "\n";
255 }
256 O << TAI->getCommentString() << "Imported Variables - END" << "\n";
257}
258
259// Emit variables defined in this module and are available to other modules.
260void PIC16AsmPrinter::EmitDefinedVars (Module &M)
261{
262 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDefs->Items;
263 if (! Items.size()) return;
264
265 O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
266 for (unsigned j = 0; j < Items.size(); j++) {
267 O << TAI->getGlobalDirective() << Mang->getValueName(Items[j]) << "\n";
268 }
269 O << TAI->getCommentString() << "Exported Variables - END" << "\n";
270}
271
272// Emit initialized data placed in ROM.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000273void PIC16AsmPrinter::EmitRomData (Module &M)
274{
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000275 // Print ROM Data section.
276 std::vector <PIC16Section *>ROSections = PTAI->ROSections;
277 for (unsigned i = 0; i < ROSections.size(); i++) {
278 std::vector<const GlobalVariable*> Items = ROSections[i]->Items;
279 if (! Items.size()) continue;
280 O << "\n";
281 SwitchToSection(PTAI->ROSections[i]->S_);
282 for (unsigned j = 0; j < Items.size(); j++) {
283 O << Mang->getValueName(Items[j]);
284 Constant *C = Items[j]->getInitializer();
285 int AddrSpace = Items[j]->getType()->getAddressSpace();
286 EmitGlobalConstant(C, AddrSpace);
287 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000288 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000289}
290
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000291bool PIC16AsmPrinter::doFinalization(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000292 printLibcallDecls();
Sanjiv Guptab157f252009-06-09 15:31:19 +0000293 EmitRemainingAutos();
Sanjiv Guptabde79422009-06-16 09:45:18 +0000294 DbgInfo.EndModule(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000295 O << "\n\t" << "END\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000296 bool Result = AsmPrinter::doFinalization(M);
297 return Result;
298}
299
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000300void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000301 const Function *F = MF.getFunction();
302 std::string FuncName = Mang->getValueName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000303 const TargetData *TD = TM.getTargetData();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000304 // Emit the data section name.
305 O << "\n";
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000306 const char *SectionName = PAN::getFrameSectionName(CurrentFnName).c_str();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000307
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000308 const Section *fPDataSection = TAI->getNamedSection(SectionName,
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000309 SectionFlags::Writeable);
310 SwitchToSection(fPDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000311
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000312 // Emit function frame label
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000313 O << PAN::getFrameLabel(CurrentFnName) << ":\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000314
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000315 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000316 unsigned RetSize = 0;
317 if (RetType->getTypeID() != Type::VoidTyID)
Duncan Sands777d2302009-05-09 07:06:46 +0000318 RetSize = TD->getTypeAllocSize(RetType);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000319
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000320 //Emit function return value space
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000321 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
322 // we will need to avoid printing a global directive for Retval label
323 // in emitExternandGloblas.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000324 if(RetSize > 0)
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000325 O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000326 else
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000327 O << PAN::getRetvalLabel(CurrentFnName) << ": \n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000328
329 // Emit variable to hold the space for function arguments
330 unsigned ArgSize = 0;
331 for (Function::const_arg_iterator argi = F->arg_begin(),
332 arge = F->arg_end(); argi != arge ; ++argi) {
333 const Type *Ty = argi->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000334 ArgSize += TD->getTypeAllocSize(Ty);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000335 }
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000336
337 O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000338
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000339 // Emit temporary space
340 int TempSize = PTLI->GetTmpSize();
341 if (TempSize > 0 )
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000342 O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize <<"\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000343}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000344
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000345void PIC16AsmPrinter::EmitIData (Module &M) {
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000346
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000347 // Print all IDATA sections.
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000348 std::vector <PIC16Section *>IDATASections = PTAI->IDATASections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000349 for (unsigned i = 0; i < IDATASections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000350 O << "\n";
Sanjiv Gupta024e94c2009-07-06 18:07:06 +0000351 if (IDATASections[i]->S_->getName().find("llvm.") != std::string::npos)
352 continue;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000353 SwitchToSection(IDATASections[i]->S_);
354 std::vector<const GlobalVariable*> Items = IDATASections[i]->Items;
355 for (unsigned j = 0; j < Items.size(); j++) {
356 std::string Name = Mang->getValueName(Items[j]);
357 Constant *C = Items[j]->getInitializer();
358 int AddrSpace = Items[j]->getType()->getAddressSpace();
359 O << Name;
360 EmitGlobalConstant(C, AddrSpace);
361 }
362 }
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000363}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000364
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000365void PIC16AsmPrinter::EmitUData (Module &M) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000366 const TargetData *TD = TM.getTargetData();
367
368 // Print all BSS sections.
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000369 std::vector <PIC16Section *>BSSSections = PTAI->BSSSections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000370 for (unsigned i = 0; i < BSSSections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000371 O << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000372 SwitchToSection(BSSSections[i]->S_);
373 std::vector<const GlobalVariable*> Items = BSSSections[i]->Items;
374 for (unsigned j = 0; j < Items.size(); j++) {
375 std::string Name = Mang->getValueName(Items[j]);
376 Constant *C = Items[j]->getInitializer();
377 const Type *Ty = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000378 unsigned Size = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000379
380 O << Name << " " <<"RES"<< " " << Size ;
381 O << "\n";
382 }
383 }
384}
385
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000386void PIC16AsmPrinter::EmitAutos (std::string FunctName)
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000387{
388 // Section names for all globals are already set.
389
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000390 const TargetData *TD = TM.getTargetData();
391
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000392 // Now print Autos section for this function.
393 std::string SectionName = PAN::getAutosSectionName(FunctName);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000394 std::vector <PIC16Section *>AutosSections = PTAI->AutosSections;
395 for (unsigned i = 0; i < AutosSections.size(); i++) {
396 O << "\n";
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000397 if (AutosSections[i]->S_->getName() == SectionName) {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000398 // Set the printing status to true
399 AutosSections[i]->setPrintedStatus(true);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000400 SwitchToSection(AutosSections[i]->S_);
401 std::vector<const GlobalVariable*> Items = AutosSections[i]->Items;
402 for (unsigned j = 0; j < Items.size(); j++) {
403 std::string VarName = Mang->getValueName(Items[j]);
404 Constant *C = Items[j]->getInitializer();
405 const Type *Ty = C->getType();
406 unsigned Size = TD->getTypeAllocSize(Ty);
407 // Emit memory reserve directive.
408 O << VarName << " RES " << Size << "\n";
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000409 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000410 break;
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000411 }
412 }
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000413}
414
Sanjiv Guptab157f252009-06-09 15:31:19 +0000415// Print autos that were not printed during the code printing of functions.
416// As the functions might themselves would have got deleted by the optimizer.
417void PIC16AsmPrinter::EmitRemainingAutos()
418{
419 const TargetData *TD = TM.getTargetData();
420
421 // Now print Autos section for this function.
422 std::vector <PIC16Section *>AutosSections = PTAI->AutosSections;
423 for (unsigned i = 0; i < AutosSections.size(); i++) {
424
425 // if the section is already printed then don't print again
426 if (AutosSections[i]->isPrinted())
427 continue;
428
429 // Set status as printed
430 AutosSections[i]->setPrintedStatus(true);
431
432 O << "\n";
433 SwitchToSection(AutosSections[i]->S_);
434 std::vector<const GlobalVariable*> Items = AutosSections[i]->Items;
435 for (unsigned j = 0; j < Items.size(); j++) {
436 std::string VarName = Mang->getValueName(Items[j]);
437 Constant *C = Items[j]->getInitializer();
438 const Type *Ty = C->getType();
439 unsigned Size = TD->getTypeAllocSize(Ty);
440 // Emit memory reserve directive.
441 O << VarName << " RES " << Size << "\n";
442 }
443 }
444}
445