blob: f9a8801265001a873e7323b977f775d596a0a2f8 [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 Guptadcb6da32009-06-13 17:35:54 +000051 // Iterate over the first basic block instructions to find if it has a
52 // DebugLoc. If so emit .file directive. Instructions such as movlw do not
53 // have valid DebugLoc, so need to iterate over instructions.
54 MachineFunction::const_iterator I = MF.begin();
55 for (MachineBasicBlock::const_iterator MBBI = I->begin(), E = I->end();
56 MBBI != E; MBBI++) {
57 const DebugLoc DLoc = MBBI->getDebugLoc();
58 if (!DLoc.isUnknown()) {
59 GlobalVariable *CU = MF.getDebugLocTuple(DLoc).CompileUnit;
60 unsigned line = MF.getDebugLocTuple(DLoc).Line;
61 DbgInfo.EmitFileDirective(CU);
62 DbgInfo.SetFunctBeginLine(line);
63 break;
64 }
65 }
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000066
67 // Emit the function frame (args and temps).
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000068 EmitFunctionFrame(MF);
69
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000070 // Emit function begin debug directive.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000071 DbgInfo.EmitFunctBeginDI(F);
72
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000073 // Emit the autos section of function.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000074 EmitAutos(CurrentFnName);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000075
76 // Now emit the instructions of function in its code section.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000077 const char *codeSection = PAN::getCodeSectionName(CurrentFnName).c_str();
78
79 const Section *fCodeSection = TAI->getNamedSection(codeSection,
80 SectionFlags::Code);
Sanjiv Gupta211f3622009-05-10 05:23:47 +000081 // Start the Code Section.
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000082 O << "\n";
Sanjiv Gupta1b046942009-01-13 19:18:47 +000083 SwitchToSection (fCodeSection);
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000084
85 // Emit the frame address of the function at the beginning of code.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000086 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
87 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000088
Sanjiv Gupta211f3622009-05-10 05:23:47 +000089 // Emit function start label.
90 O << CurrentFnName << ":\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000091
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000092 // For emitting line directives, we need to keep track of the current
93 // source line. When it changes then only emit the line directive.
94 unsigned CurLine = 0;
95 O << "\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000096 // Print out code for the function.
97 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
98 I != E; ++I) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000099
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000100 // Print a label for the basic block.
101 if (I != MF.begin()) {
102 printBasicBlockLabel(I, true);
103 O << '\n';
104 }
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000105
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000106 // Print a basic block.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000107 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
108 II != E; ++II) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000109
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000110 // Emit the line directive if source line changed.
111 const DebugLoc DL = II->getDebugLoc();
112 if (!DL.isUnknown()) {
113 unsigned line = MF.getDebugLocTuple(DL).Line;
114 if (line != CurLine) {
115 O << "\t.line " << line << "\n";
116 CurLine = line;
117 }
118 }
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000119
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000120 // Print the assembly for the instruction.
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000121 printMachineInstruction(II);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000122 }
123 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000124
125 // Emit function end debug directives.
126 DbgInfo.EmitFunctEndDI(F, CurLine);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000127
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000128 return false; // we didn't modify anything.
129}
130
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000131/// createPIC16CodePrinterPass - Returns a pass that prints the PIC16
132/// assembly code for a MachineFunction to the given output stream,
133/// using the given target machine description. This should work
134/// regardless of whether the function is in SSA form.
135///
Owen Andersoncb371882008-08-21 00:14:44 +0000136FunctionPass *llvm::createPIC16CodePrinterPass(raw_ostream &o,
Bill Wendling57f0db82009-02-24 08:30:20 +0000137 PIC16TargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +0000138 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000139 bool verbose) {
140 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000141}
142
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000143
144// printOperand - print operand of insn.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000145void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000146 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000147
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000148 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000149 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000150 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000151 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000152 else
153 assert(0 && "not implemented");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000154 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000155
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000156 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000157 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000158 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000159
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000160 case MachineOperand::MO_GlobalAddress: {
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000161 O << Mang->getValueName(MO.getGlobal());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000162 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000163 }
164 case MachineOperand::MO_ExternalSymbol: {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000165 const char *Sname = MO.getSymbolName();
166
167 // If its a libcall name, record it to decls section.
168 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000169 LibcallDecls.push_back(Sname);
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000170 }
171
172 O << Sname;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000173 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000174 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000175 case MachineOperand::MO_MachineBasicBlock:
176 printBasicBlockLabel(MO.getMBB());
177 return;
178
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000179 default:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000180 assert(0 && " Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000181 }
182}
183
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000184/// printCCOperand - Print the cond code operand.
185///
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000186void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
187 int CC = (int)MI->getOperand(opNum).getImm();
188 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
189}
190
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000191/// printLibcallDecls - print the extern declarations for compiler
192/// intrinsics.
193///
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000194void PIC16AsmPrinter::printLibcallDecls(void) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000195 // If no libcalls used, return.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000196 if (LibcallDecls.empty()) return;
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000197
Sanjiv Gupta42918572009-05-12 06:52:41 +0000198 O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000199 // Remove duplicate entries.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000200 LibcallDecls.sort();
201 LibcallDecls.unique();
202 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
203 I != LibcallDecls.end(); I++) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000204 O << TAI->getExternDirective() << *I << "\n";
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000205 O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
206 O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000207 }
Sanjiv Gupta42918572009-05-12 06:52:41 +0000208 O << TAI->getCommentString() << "External decls for libcalls - END." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000209}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000210
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000211/// doInitialization - Perfrom Module level initializations here.
212/// One task that we do here is to sectionize all global variables.
213/// The MemSelOptimizer pass depends on the sectionizing.
214///
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000215bool PIC16AsmPrinter::doInitialization (Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000216 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000217
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000218 // FIXME:: This is temporary solution to generate the include file.
219 // The processor should be passed to llc as in input and the header file
220 // should be generated accordingly.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000221 O << "\n\t#include P16F1937.INC\n";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000222
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000223 // Set the section names for all globals.
224 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
225 I != E; ++I) {
226 I->setSection(TAI->SectionForGlobal(I)->getName());
227 }
228
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000229 DbgInfo.Init(M);
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000230 EmitFunctionDecls(M);
231 EmitUndefinedVars(M);
232 EmitDefinedVars(M);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000233 EmitIData(M);
234 EmitUData(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000235 EmitRomData(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000236 return Result;
237}
238
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000239/// Emit extern decls for functions imported from other modules, and emit
240/// global declarations for function defined in this module and which are
241/// available to other modules.
242///
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000243void PIC16AsmPrinter::EmitFunctionDecls (Module &M) {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000244 // Emit declarations for external functions.
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000245 O <<"\n"<<TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000246 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
247 std::string Name = Mang->getValueName(I);
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000248 if (Name.compare("@abort") == 0)
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000249 continue;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000250
251 // If it is llvm intrinsic call then don't emit
252 if (Name.find("llvm.") != std::string::npos)
253 continue;
254
Sanjiv Guptaaf3fdb52009-05-10 16:18:39 +0000255 if (! (I->isDeclaration() || I->hasExternalLinkage()))
256 continue;
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000257
258 const char *directive = I->isDeclaration() ? TAI->getExternDirective() :
259 TAI->getGlobalDirective();
260
261 O << directive << Name << "\n";
262 O << directive << PAN::getRetvalLabel(Name) << "\n";
263 O << directive << PAN::getArgsLabel(Name) << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000264 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000265
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000266 O << TAI->getCommentString() << "Function Declarations - END." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000267}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000268
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000269// Emit variables imported from other Modules.
270void PIC16AsmPrinter::EmitUndefinedVars (Module &M)
271{
272 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDecls->Items;
273 if (! Items.size()) return;
274
275 O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
276 for (unsigned j = 0; j < Items.size(); j++) {
277 O << TAI->getExternDirective() << Mang->getValueName(Items[j]) << "\n";
278 }
279 O << TAI->getCommentString() << "Imported Variables - END" << "\n";
280}
281
282// Emit variables defined in this module and are available to other modules.
283void PIC16AsmPrinter::EmitDefinedVars (Module &M)
284{
285 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDefs->Items;
286 if (! Items.size()) return;
287
288 O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
289 for (unsigned j = 0; j < Items.size(); j++) {
290 O << TAI->getGlobalDirective() << Mang->getValueName(Items[j]) << "\n";
291 }
292 O << TAI->getCommentString() << "Exported Variables - END" << "\n";
293}
294
295// Emit initialized data placed in ROM.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000296void PIC16AsmPrinter::EmitRomData (Module &M)
297{
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000298
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000299 std::vector<const GlobalVariable*> Items = PTAI->ROSection->Items;
300 if (! Items.size()) return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000301
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000302 // Print ROData ection.
303 O << "\n";
304 SwitchToSection(PTAI->ROSection->S_);
305 for (unsigned j = 0; j < Items.size(); j++) {
306 O << Mang->getValueName(Items[j]);
307 Constant *C = Items[j]->getInitializer();
308 int AddrSpace = Items[j]->getType()->getAddressSpace();
309 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000310 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000311}
312
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000313bool PIC16AsmPrinter::doFinalization(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000314 printLibcallDecls();
Sanjiv Guptab157f252009-06-09 15:31:19 +0000315 EmitRemainingAutos();
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000316 DbgInfo.EmitVarDebugInfo(M);
Sanjiv Gupta3fc7e532009-06-03 16:27:49 +0000317 DbgInfo.EmitEOF();
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000318 O << "\n\t" << "END\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000319 bool Result = AsmPrinter::doFinalization(M);
320 return Result;
321}
322
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000323void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000324 const Function *F = MF.getFunction();
325 std::string FuncName = Mang->getValueName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000326 const TargetData *TD = TM.getTargetData();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000327 // Emit the data section name.
328 O << "\n";
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000329 const char *SectionName = PAN::getFrameSectionName(CurrentFnName).c_str();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000330
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000331 const Section *fPDataSection = TAI->getNamedSection(SectionName,
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000332 SectionFlags::Writeable);
333 SwitchToSection(fPDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000334
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000335 // Emit function frame label
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000336 O << PAN::getFrameLabel(CurrentFnName) << ":\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000337
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000338 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000339 unsigned RetSize = 0;
340 if (RetType->getTypeID() != Type::VoidTyID)
Duncan Sands777d2302009-05-09 07:06:46 +0000341 RetSize = TD->getTypeAllocSize(RetType);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000342
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000343 //Emit function return value space
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000344 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
345 // we will need to avoid printing a global directive for Retval label
346 // in emitExternandGloblas.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000347 if(RetSize > 0)
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000348 O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000349 else
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000350 O << PAN::getRetvalLabel(CurrentFnName) << ": \n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000351
352 // Emit variable to hold the space for function arguments
353 unsigned ArgSize = 0;
354 for (Function::const_arg_iterator argi = F->arg_begin(),
355 arge = F->arg_end(); argi != arge ; ++argi) {
356 const Type *Ty = argi->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000357 ArgSize += TD->getTypeAllocSize(Ty);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000358 }
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000359
360 O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000361
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000362 // Emit temporary space
363 int TempSize = PTLI->GetTmpSize();
364 if (TempSize > 0 )
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000365 O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize <<"\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000366}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000367
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000368void PIC16AsmPrinter::EmitIData (Module &M) {
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000369
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000370 // Print all IDATA sections.
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000371 std::vector <PIC16Section *>IDATASections = PTAI->IDATASections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000372 for (unsigned i = 0; i < IDATASections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000373 O << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000374 SwitchToSection(IDATASections[i]->S_);
375 std::vector<const GlobalVariable*> Items = IDATASections[i]->Items;
376 for (unsigned j = 0; j < Items.size(); j++) {
377 std::string Name = Mang->getValueName(Items[j]);
378 Constant *C = Items[j]->getInitializer();
379 int AddrSpace = Items[j]->getType()->getAddressSpace();
380 O << Name;
381 EmitGlobalConstant(C, AddrSpace);
382 }
383 }
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000384}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000385
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000386void PIC16AsmPrinter::EmitUData (Module &M) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000387 const TargetData *TD = TM.getTargetData();
388
389 // Print all BSS sections.
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000390 std::vector <PIC16Section *>BSSSections = PTAI->BSSSections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000391 for (unsigned i = 0; i < BSSSections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000392 O << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000393 SwitchToSection(BSSSections[i]->S_);
394 std::vector<const GlobalVariable*> Items = BSSSections[i]->Items;
395 for (unsigned j = 0; j < Items.size(); j++) {
396 std::string Name = Mang->getValueName(Items[j]);
397 Constant *C = Items[j]->getInitializer();
398 const Type *Ty = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000399 unsigned Size = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000400
401 O << Name << " " <<"RES"<< " " << Size ;
402 O << "\n";
403 }
404 }
405}
406
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000407void PIC16AsmPrinter::EmitAutos (std::string FunctName)
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000408{
409 // Section names for all globals are already set.
410
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000411 const TargetData *TD = TM.getTargetData();
412
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000413 // Now print Autos section for this function.
414 std::string SectionName = PAN::getAutosSectionName(FunctName);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000415 std::vector <PIC16Section *>AutosSections = PTAI->AutosSections;
416 for (unsigned i = 0; i < AutosSections.size(); i++) {
417 O << "\n";
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000418 if (AutosSections[i]->S_->getName() == SectionName) {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000419 // Set the printing status to true
420 AutosSections[i]->setPrintedStatus(true);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000421 SwitchToSection(AutosSections[i]->S_);
422 std::vector<const GlobalVariable*> Items = AutosSections[i]->Items;
423 for (unsigned j = 0; j < Items.size(); j++) {
424 std::string VarName = Mang->getValueName(Items[j]);
425 Constant *C = Items[j]->getInitializer();
426 const Type *Ty = C->getType();
427 unsigned Size = TD->getTypeAllocSize(Ty);
428 // Emit memory reserve directive.
429 O << VarName << " RES " << Size << "\n";
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000430 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000431 break;
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000432 }
433 }
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000434}
435
Sanjiv Guptab157f252009-06-09 15:31:19 +0000436// Print autos that were not printed during the code printing of functions.
437// As the functions might themselves would have got deleted by the optimizer.
438void PIC16AsmPrinter::EmitRemainingAutos()
439{
440 const TargetData *TD = TM.getTargetData();
441
442 // Now print Autos section for this function.
443 std::vector <PIC16Section *>AutosSections = PTAI->AutosSections;
444 for (unsigned i = 0; i < AutosSections.size(); i++) {
445
446 // if the section is already printed then don't print again
447 if (AutosSections[i]->isPrinted())
448 continue;
449
450 // Set status as printed
451 AutosSections[i]->setPrintedStatus(true);
452
453 O << "\n";
454 SwitchToSection(AutosSections[i]->S_);
455 std::vector<const GlobalVariable*> Items = AutosSections[i]->Items;
456 for (unsigned j = 0; j < Items.size(); j++) {
457 std::string VarName = Mang->getValueName(Items[j]);
458 Constant *C = Items[j]->getInitializer();
459 const Type *Ty = C->getType();
460 unsigned Size = TD->getTypeAllocSize(Ty);
461 // Emit memory reserve directive.
462 O << VarName << " RES " << Size << "\n";
463 }
464 }
465}
466