blob: d6282270ec5a627487422e968612aea8ce77ee09 [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 .file directive.
Sanjiv Gupta3fc7e532009-06-03 16:27:49 +000052 DbgInfo.EmitFileDirective(F);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000053
54 // Emit the function frame (args and temps).
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000055 EmitFunctionFrame(MF);
56
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000057 // Emit function begin debug directive.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000058 DbgInfo.EmitFunctBeginDI(F);
59
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000060 // Emit the autos section of function.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000061 EmitAutos(CurrentFnName);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000062
63 // Now emit the instructions of function in its code section.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000064 const char *codeSection = PAN::getCodeSectionName(CurrentFnName).c_str();
65
66 const Section *fCodeSection = TAI->getNamedSection(codeSection,
67 SectionFlags::Code);
Sanjiv Gupta211f3622009-05-10 05:23:47 +000068 // Start the Code Section.
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000069 O << "\n";
Sanjiv Gupta1b046942009-01-13 19:18:47 +000070 SwitchToSection (fCodeSection);
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000071
72 // Emit the frame address of the function at the beginning of code.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000073 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
74 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000075
Sanjiv Gupta211f3622009-05-10 05:23:47 +000076 // Emit function start label.
77 O << CurrentFnName << ":\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000078
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000079 // For emitting line directives, we need to keep track of the current
80 // source line. When it changes then only emit the line directive.
81 unsigned CurLine = 0;
82 O << "\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000083 // Print out code for the function.
84 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
85 I != E; ++I) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000086
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000087 // Print a label for the basic block.
88 if (I != MF.begin()) {
89 printBasicBlockLabel(I, true);
90 O << '\n';
91 }
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000092
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000093 // Print a basic block.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000094 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
95 II != E; ++II) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000096
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000097 // Emit the line directive if source line changed.
98 const DebugLoc DL = II->getDebugLoc();
99 if (!DL.isUnknown()) {
100 unsigned line = MF.getDebugLocTuple(DL).Line;
101 if (line != CurLine) {
102 O << "\t.line " << line << "\n";
103 CurLine = line;
104 }
105 }
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000106
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000107 // Print the assembly for the instruction.
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000108 printMachineInstruction(II);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000109 }
110 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000111
112 // Emit function end debug directives.
113 DbgInfo.EmitFunctEndDI(F, CurLine);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000114
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000115 return false; // we didn't modify anything.
116}
117
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000118/// createPIC16CodePrinterPass - Returns a pass that prints the PIC16
119/// assembly code for a MachineFunction to the given output stream,
120/// using the given target machine description. This should work
121/// regardless of whether the function is in SSA form.
122///
Owen Andersoncb371882008-08-21 00:14:44 +0000123FunctionPass *llvm::createPIC16CodePrinterPass(raw_ostream &o,
Bill Wendling57f0db82009-02-24 08:30:20 +0000124 PIC16TargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +0000125 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000126 bool verbose) {
127 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000128}
129
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000130
131// printOperand - print operand of insn.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000132void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000133 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000134
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000135 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000136 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000137 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000138 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000139 else
140 assert(0 && "not implemented");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000141 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000142
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000143 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000144 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000145 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000146
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000147 case MachineOperand::MO_GlobalAddress: {
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000148 O << Mang->getValueName(MO.getGlobal());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000149 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000150 }
151 case MachineOperand::MO_ExternalSymbol: {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000152 const char *Sname = MO.getSymbolName();
153
154 // If its a libcall name, record it to decls section.
155 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000156 LibcallDecls.push_back(Sname);
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000157 }
158
159 O << Sname;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000160 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000161 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000162 case MachineOperand::MO_MachineBasicBlock:
163 printBasicBlockLabel(MO.getMBB());
164 return;
165
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000166 default:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000167 assert(0 && " Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000168 }
169}
170
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000171/// printCCOperand - Print the cond code operand.
172///
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000173void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
174 int CC = (int)MI->getOperand(opNum).getImm();
175 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
176}
177
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000178/// printLibcallDecls - print the extern declarations for compiler
179/// intrinsics.
180///
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000181void PIC16AsmPrinter::printLibcallDecls(void) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000182 // If no libcalls used, return.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000183 if (LibcallDecls.empty()) return;
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000184
Sanjiv Gupta42918572009-05-12 06:52:41 +0000185 O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000186 // Remove duplicate entries.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000187 LibcallDecls.sort();
188 LibcallDecls.unique();
189 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
190 I != LibcallDecls.end(); I++) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000191 O << TAI->getExternDirective() << *I << "\n";
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000192 O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
193 O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000194 }
Sanjiv Gupta42918572009-05-12 06:52:41 +0000195 O << TAI->getCommentString() << "External decls for libcalls - END." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000196}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000197
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000198/// doInitialization - Perfrom Module level initializations here.
199/// One task that we do here is to sectionize all global variables.
200/// The MemSelOptimizer pass depends on the sectionizing.
201///
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000202bool PIC16AsmPrinter::doInitialization (Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000203 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000204
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000205 // FIXME:: This is temporary solution to generate the include file.
206 // The processor should be passed to llc as in input and the header file
207 // should be generated accordingly.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000208 O << "\n\t#include P16F1937.INC\n";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000209
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000210 // Set the section names for all globals.
211 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
212 I != E; ++I) {
213 I->setSection(TAI->SectionForGlobal(I)->getName());
214 }
215
Sanjiv Gupta3fc7e532009-06-03 16:27:49 +0000216 DbgInfo.EmitFileDirective(M);
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000217 EmitFunctionDecls(M);
218 EmitUndefinedVars(M);
219 EmitDefinedVars(M);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000220 EmitIData(M);
221 EmitUData(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000222 EmitRomData(M);
Sanjiv Gupta3fc7e532009-06-03 16:27:49 +0000223 DbgInfo.PopulateFunctsDI(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000224 return Result;
225}
226
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000227/// Emit extern decls for functions imported from other modules, and emit
228/// global declarations for function defined in this module and which are
229/// available to other modules.
230///
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000231void PIC16AsmPrinter::EmitFunctionDecls (Module &M) {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000232 // Emit declarations for external functions.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000233 O << TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000234 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
235 std::string Name = Mang->getValueName(I);
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000236 if (Name.compare("@abort") == 0)
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000237 continue;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000238
239 // If it is llvm intrinsic call then don't emit
240 if (Name.find("llvm.") != std::string::npos)
241 continue;
242
Sanjiv Guptaaf3fdb52009-05-10 16:18:39 +0000243 if (! (I->isDeclaration() || I->hasExternalLinkage()))
244 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.
258void PIC16AsmPrinter::EmitUndefinedVars (Module &M)
259{
260 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDecls->Items;
261 if (! Items.size()) return;
262
263 O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
264 for (unsigned j = 0; j < Items.size(); j++) {
265 O << TAI->getExternDirective() << Mang->getValueName(Items[j]) << "\n";
266 }
267 O << TAI->getCommentString() << "Imported Variables - END" << "\n";
268}
269
270// Emit variables defined in this module and are available to other modules.
271void PIC16AsmPrinter::EmitDefinedVars (Module &M)
272{
273 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDefs->Items;
274 if (! Items.size()) return;
275
276 O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
277 for (unsigned j = 0; j < Items.size(); j++) {
278 O << TAI->getGlobalDirective() << Mang->getValueName(Items[j]) << "\n";
279 }
280 O << TAI->getCommentString() << "Exported Variables - END" << "\n";
281}
282
283// Emit initialized data placed in ROM.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000284void PIC16AsmPrinter::EmitRomData (Module &M)
285{
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000286
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000287 std::vector<const GlobalVariable*> Items = PTAI->ROSection->Items;
288 if (! Items.size()) return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000289
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000290 // Print ROData ection.
291 O << "\n";
292 SwitchToSection(PTAI->ROSection->S_);
293 for (unsigned j = 0; j < Items.size(); j++) {
294 O << Mang->getValueName(Items[j]);
295 Constant *C = Items[j]->getInitializer();
296 int AddrSpace = Items[j]->getType()->getAddressSpace();
297 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000298 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000299}
300
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000301bool PIC16AsmPrinter::doFinalization(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000302 printLibcallDecls();
Sanjiv Guptab157f252009-06-09 15:31:19 +0000303 EmitRemainingAutos();
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000304 DbgInfo.EmitVarDebugInfo(M);
Sanjiv Gupta3fc7e532009-06-03 16:27:49 +0000305 DbgInfo.EmitEOF();
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000306 O << "\n\t" << "END\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000307 bool Result = AsmPrinter::doFinalization(M);
308 return Result;
309}
310
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000311void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000312 const Function *F = MF.getFunction();
313 std::string FuncName = Mang->getValueName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000314 const TargetData *TD = TM.getTargetData();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000315 // Emit the data section name.
316 O << "\n";
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000317 const char *SectionName = PAN::getFrameSectionName(CurrentFnName).c_str();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000318
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000319 const Section *fPDataSection = TAI->getNamedSection(SectionName,
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000320 SectionFlags::Writeable);
321 SwitchToSection(fPDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000322
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000323 // Emit function frame label
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000324 O << PAN::getFrameLabel(CurrentFnName) << ":\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000325
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000326 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000327 unsigned RetSize = 0;
328 if (RetType->getTypeID() != Type::VoidTyID)
Duncan Sands777d2302009-05-09 07:06:46 +0000329 RetSize = TD->getTypeAllocSize(RetType);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000330
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000331 //Emit function return value space
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000332 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
333 // we will need to avoid printing a global directive for Retval label
334 // in emitExternandGloblas.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000335 if(RetSize > 0)
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000336 O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000337 else
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000338 O << PAN::getRetvalLabel(CurrentFnName) << ": \n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000339
340 // Emit variable to hold the space for function arguments
341 unsigned ArgSize = 0;
342 for (Function::const_arg_iterator argi = F->arg_begin(),
343 arge = F->arg_end(); argi != arge ; ++argi) {
344 const Type *Ty = argi->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000345 ArgSize += TD->getTypeAllocSize(Ty);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000346 }
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000347
348 O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000349
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000350 // Emit temporary space
351 int TempSize = PTLI->GetTmpSize();
352 if (TempSize > 0 )
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000353 O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize <<"\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000354}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000355
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000356void PIC16AsmPrinter::EmitIData (Module &M) {
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000357
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000358 // Print all IDATA sections.
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000359 std::vector <PIC16Section *>IDATASections = PTAI->IDATASections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000360 for (unsigned i = 0; i < IDATASections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000361 O << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000362 SwitchToSection(IDATASections[i]->S_);
363 std::vector<const GlobalVariable*> Items = IDATASections[i]->Items;
364 for (unsigned j = 0; j < Items.size(); j++) {
365 std::string Name = Mang->getValueName(Items[j]);
366 Constant *C = Items[j]->getInitializer();
367 int AddrSpace = Items[j]->getType()->getAddressSpace();
368 O << Name;
369 EmitGlobalConstant(C, AddrSpace);
370 }
371 }
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000372}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000373
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000374void PIC16AsmPrinter::EmitUData (Module &M) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000375 const TargetData *TD = TM.getTargetData();
376
377 // Print all BSS sections.
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000378 std::vector <PIC16Section *>BSSSections = PTAI->BSSSections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000379 for (unsigned i = 0; i < BSSSections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000380 O << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000381 SwitchToSection(BSSSections[i]->S_);
382 std::vector<const GlobalVariable*> Items = BSSSections[i]->Items;
383 for (unsigned j = 0; j < Items.size(); j++) {
384 std::string Name = Mang->getValueName(Items[j]);
385 Constant *C = Items[j]->getInitializer();
386 const Type *Ty = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000387 unsigned Size = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000388
389 O << Name << " " <<"RES"<< " " << Size ;
390 O << "\n";
391 }
392 }
393}
394
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000395void PIC16AsmPrinter::EmitAutos (std::string FunctName)
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000396{
397 // Section names for all globals are already set.
398
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000399 const TargetData *TD = TM.getTargetData();
400
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000401 // Now print Autos section for this function.
402 std::string SectionName = PAN::getAutosSectionName(FunctName);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000403 std::vector <PIC16Section *>AutosSections = PTAI->AutosSections;
404 for (unsigned i = 0; i < AutosSections.size(); i++) {
405 O << "\n";
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000406 if (AutosSections[i]->S_->getName() == SectionName) {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000407 // Set the printing status to true
408 AutosSections[i]->setPrintedStatus(true);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000409 SwitchToSection(AutosSections[i]->S_);
410 std::vector<const GlobalVariable*> Items = AutosSections[i]->Items;
411 for (unsigned j = 0; j < Items.size(); j++) {
412 std::string VarName = Mang->getValueName(Items[j]);
413 Constant *C = Items[j]->getInitializer();
414 const Type *Ty = C->getType();
415 unsigned Size = TD->getTypeAllocSize(Ty);
416 // Emit memory reserve directive.
417 O << VarName << " RES " << Size << "\n";
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000418 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000419 break;
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000420 }
421 }
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000422}
423
Sanjiv Guptab157f252009-06-09 15:31:19 +0000424// Print autos that were not printed during the code printing of functions.
425// As the functions might themselves would have got deleted by the optimizer.
426void PIC16AsmPrinter::EmitRemainingAutos()
427{
428 const TargetData *TD = TM.getTargetData();
429
430 // Now print Autos section for this function.
431 std::vector <PIC16Section *>AutosSections = PTAI->AutosSections;
432 for (unsigned i = 0; i < AutosSections.size(); i++) {
433
434 // if the section is already printed then don't print again
435 if (AutosSections[i]->isPrinted())
436 continue;
437
438 // Set status as printed
439 AutosSections[i]->setPrintedStatus(true);
440
441 O << "\n";
442 SwitchToSection(AutosSections[i]->S_);
443 std::vector<const GlobalVariable*> Items = AutosSections[i]->Items;
444 for (unsigned j = 0; j < Items.size(); j++) {
445 std::string VarName = Mang->getValueName(Items[j]);
446 Constant *C = Items[j]->getInitializer();
447 const Type *Ty = C->getType();
448 unsigned Size = TD->getTypeAllocSize(Ty);
449 // Emit memory reserve directive.
450 O << VarName << " RES " << Size << "\n";
451 }
452 }
453}
454