blob: ef3bc4b52a96f51fa87dccd736a8997e894c50ae [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
36/// runOnMachineFunction - This uses the printInstruction()
37/// method to print assembly for each instruction.
38///
39bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +000040 this->MF = &MF;
41
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000042 // This calls the base class function required to be called at beginning
43 // of runOnMachineFunction.
44 SetupMachineFunction(MF);
45
46 // Get the mangled name.
47 const Function *F = MF.getFunction();
48 CurrentFnName = Mang->getValueName(F);
49
50 // Emit the function variables.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000051 EmitFunctionFrame(MF);
52
53 // Emit function begin debug directives
54 DbgInfo.EmitFunctBeginDI(F);
55
56 EmitAutos(CurrentFnName);
Sanjiv Gupta211f3622009-05-10 05:23:47 +000057 const char *codeSection = PAN::getCodeSectionName(CurrentFnName).c_str();
58
59 const Section *fCodeSection = TAI->getNamedSection(codeSection,
60 SectionFlags::Code);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000061 O << "\n";
Sanjiv Gupta211f3622009-05-10 05:23:47 +000062 // Start the Code Section.
Sanjiv Gupta1b046942009-01-13 19:18:47 +000063 SwitchToSection (fCodeSection);
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000064
65 // Emit the frame address of the function at the beginning of code.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000066 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
67 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000068
Sanjiv Gupta211f3622009-05-10 05:23:47 +000069 // Emit function start label.
70 O << CurrentFnName << ":\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000071
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000072 // For emitting line directives, we need to keep track of the current
73 // source line. When it changes then only emit the line directive.
74 unsigned CurLine = 0;
75 O << "\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000076 // Print out code for the function.
77 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
78 I != E; ++I) {
79 // Print a label for the basic block.
80 if (I != MF.begin()) {
81 printBasicBlockLabel(I, true);
82 O << '\n';
83 }
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000084
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000085 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
86 II != E; ++II) {
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000087 // Emit the line directive if source line changed.
88 const DebugLoc DL = II->getDebugLoc();
89 if (!DL.isUnknown()) {
90 unsigned line = MF.getDebugLocTuple(DL).Line;
91 if (line != CurLine) {
92 O << "\t.line " << line << "\n";
93 CurLine = line;
94 }
95 }
Sanjiv Gupta0608b492009-05-11 06:01:38 +000096
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000097 // Print the assembly for the instruction.
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000098 printMachineInstruction(II);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000099 }
100 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000101
102 // Emit function end debug directives.
103 DbgInfo.EmitFunctEndDI(F, CurLine);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000104 return false; // we didn't modify anything.
105}
106
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000107/// createPIC16CodePrinterPass - Returns a pass that prints the PIC16
108/// assembly code for a MachineFunction to the given output stream,
109/// using the given target machine description. This should work
110/// regardless of whether the function is in SSA form.
111///
Owen Andersoncb371882008-08-21 00:14:44 +0000112FunctionPass *llvm::createPIC16CodePrinterPass(raw_ostream &o,
Bill Wendling57f0db82009-02-24 08:30:20 +0000113 PIC16TargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +0000114 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000115 bool verbose) {
116 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000117}
118
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000119
120// printOperand - print operand of insn.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000121void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000122 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000123
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000124 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000125 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000126 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000127 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000128 else
129 assert(0 && "not implemented");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000130 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000131
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000132 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000133 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000134 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000135
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000136 case MachineOperand::MO_GlobalAddress: {
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000137 O << Mang->getValueName(MO.getGlobal());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000138 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000139 }
140 case MachineOperand::MO_ExternalSymbol: {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000141 const char *Sname = MO.getSymbolName();
142
143 // If its a libcall name, record it to decls section.
144 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000145 LibcallDecls.push_back(Sname);
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000146 }
147
148 O << Sname;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000149 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000150 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000151 case MachineOperand::MO_MachineBasicBlock:
152 printBasicBlockLabel(MO.getMBB());
153 return;
154
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000155 default:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000156 assert(0 && " Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000157 }
158}
159
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000160void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
161 int CC = (int)MI->getOperand(opNum).getImm();
162 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
163}
164
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000165void PIC16AsmPrinter::printLibcallDecls(void) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000166 // If no libcalls used, return.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000167 if (LibcallDecls.empty()) return;
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000168
Sanjiv Gupta42918572009-05-12 06:52:41 +0000169 O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000170 // Remove duplicate entries.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000171 LibcallDecls.sort();
172 LibcallDecls.unique();
173 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
174 I != LibcallDecls.end(); I++) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000175 O << TAI->getExternDirective() << *I << "\n";
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000176 O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
177 O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000178 }
Sanjiv Gupta42918572009-05-12 06:52:41 +0000179 O << TAI->getCommentString() << "External decls for libcalls - END." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000180}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000181
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000182bool PIC16AsmPrinter::doInitialization (Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000183 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000184 DbgInfo.EmitFileDirective(M);
185
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000186 // FIXME:: This is temporary solution to generate the include file.
187 // The processor should be passed to llc as in input and the header file
188 // should be generated accordingly.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000189 O << "\n\t#include P16F1937.INC\n";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000190 MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
191 assert(MMI);
192 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
193 assert(DW && "Dwarf Writer is not available");
194 DW->BeginModule(&M, MMI, O, this, TAI);
195
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000196 // Set the section names for all globals.
197 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
198 I != E; ++I) {
199 I->setSection(TAI->SectionForGlobal(I)->getName());
200 }
201
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000202 EmitFunctionDecls(M);
203 EmitUndefinedVars(M);
204 EmitDefinedVars(M);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000205 EmitIData(M);
206 EmitUData(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000207 EmitRomData(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000208 DbgInfo.PopulateFunctsDI(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000209 return Result;
210}
211
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000212// Emit extern decls for functions imported from other modules, and emit
213// global declarations for function defined in this module and which are
214// available to other modules.
215void PIC16AsmPrinter::EmitFunctionDecls (Module &M) {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000216 // Emit declarations for external functions.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000217 O << TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000218 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
219 std::string Name = Mang->getValueName(I);
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000220 if (Name.compare("@abort") == 0)
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000221 continue;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000222
223 // If it is llvm intrinsic call then don't emit
224 if (Name.find("llvm.") != std::string::npos)
225 continue;
226
Sanjiv Guptaaf3fdb52009-05-10 16:18:39 +0000227 if (! (I->isDeclaration() || I->hasExternalLinkage()))
228 continue;
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000229
230 const char *directive = I->isDeclaration() ? TAI->getExternDirective() :
231 TAI->getGlobalDirective();
232
233 O << directive << Name << "\n";
234 O << directive << PAN::getRetvalLabel(Name) << "\n";
235 O << directive << PAN::getArgsLabel(Name) << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000236 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000237
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000238 O << TAI->getCommentString() << "Function Declarations - END." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000239}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000240
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000241// Emit variables imported from other Modules.
242void PIC16AsmPrinter::EmitUndefinedVars (Module &M)
243{
244 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDecls->Items;
245 if (! Items.size()) return;
246
247 O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
248 for (unsigned j = 0; j < Items.size(); j++) {
249 O << TAI->getExternDirective() << Mang->getValueName(Items[j]) << "\n";
250 }
251 O << TAI->getCommentString() << "Imported Variables - END" << "\n";
252}
253
254// Emit variables defined in this module and are available to other modules.
255void PIC16AsmPrinter::EmitDefinedVars (Module &M)
256{
257 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDefs->Items;
258 if (! Items.size()) return;
259
260 O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
261 for (unsigned j = 0; j < Items.size(); j++) {
262 O << TAI->getGlobalDirective() << Mang->getValueName(Items[j]) << "\n";
263 }
264 O << TAI->getCommentString() << "Exported Variables - END" << "\n";
265}
266
267// Emit initialized data placed in ROM.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000268void PIC16AsmPrinter::EmitRomData (Module &M)
269{
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000270
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000271 std::vector<const GlobalVariable*> Items = PTAI->ROSection->Items;
272 if (! Items.size()) return;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000273
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000274 // Print ROData ection.
275 O << "\n";
276 SwitchToSection(PTAI->ROSection->S_);
277 for (unsigned j = 0; j < Items.size(); j++) {
278 O << Mang->getValueName(Items[j]);
279 Constant *C = Items[j]->getInitializer();
280 int AddrSpace = Items[j]->getType()->getAddressSpace();
281 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000282 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000283}
284
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000285bool PIC16AsmPrinter::doFinalization(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000286 printLibcallDecls();
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000287 DbgInfo.EmitVarDebugInfo(M);
288 O << "\n\t" << ".EOF";
289 O << "\n\t" << "END\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000290 bool Result = AsmPrinter::doFinalization(M);
291 return Result;
292}
293
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000294void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000295 const Function *F = MF.getFunction();
296 std::string FuncName = Mang->getValueName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000297 const TargetData *TD = TM.getTargetData();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000298 // Emit the data section name.
299 O << "\n";
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000300 const char *SectionName = PAN::getFrameSectionName(CurrentFnName).c_str();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000301
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000302 const Section *fPDataSection = TAI->getNamedSection(SectionName,
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000303 SectionFlags::Writeable);
304 SwitchToSection(fPDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000305
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000306 // Emit function frame label
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000307 O << PAN::getFrameLabel(CurrentFnName) << ":\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000308
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000309 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000310 unsigned RetSize = 0;
311 if (RetType->getTypeID() != Type::VoidTyID)
Duncan Sands777d2302009-05-09 07:06:46 +0000312 RetSize = TD->getTypeAllocSize(RetType);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000313
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000314 //Emit function return value space
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000315 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
316 // we will need to avoid printing a global directive for Retval label
317 // in emitExternandGloblas.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000318 if(RetSize > 0)
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000319 O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000320 else
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000321 O << PAN::getRetvalLabel(CurrentFnName) << ": \n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000322
323 // Emit variable to hold the space for function arguments
324 unsigned ArgSize = 0;
325 for (Function::const_arg_iterator argi = F->arg_begin(),
326 arge = F->arg_end(); argi != arge ; ++argi) {
327 const Type *Ty = argi->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000328 ArgSize += TD->getTypeAllocSize(Ty);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000329 }
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000330
331 O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000332
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000333 // Emit temporary space
334 int TempSize = PTLI->GetTmpSize();
335 if (TempSize > 0 )
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000336 O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize <<"\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000337}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000338
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000339void PIC16AsmPrinter::EmitIData (Module &M) {
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000340
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000341 // Print all IDATA sections.
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000342 std::vector <PIC16Section *>IDATASections = PTAI->IDATASections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000343 for (unsigned i = 0; i < IDATASections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000344 O << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000345 SwitchToSection(IDATASections[i]->S_);
346 std::vector<const GlobalVariable*> Items = IDATASections[i]->Items;
347 for (unsigned j = 0; j < Items.size(); j++) {
348 std::string Name = Mang->getValueName(Items[j]);
349 Constant *C = Items[j]->getInitializer();
350 int AddrSpace = Items[j]->getType()->getAddressSpace();
351 O << Name;
352 EmitGlobalConstant(C, AddrSpace);
353 }
354 }
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000355}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000356
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000357void PIC16AsmPrinter::EmitUData (Module &M) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000358 const TargetData *TD = TM.getTargetData();
359
360 // Print all BSS sections.
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000361 std::vector <PIC16Section *>BSSSections = PTAI->BSSSections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000362 for (unsigned i = 0; i < BSSSections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000363 O << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000364 SwitchToSection(BSSSections[i]->S_);
365 std::vector<const GlobalVariable*> Items = BSSSections[i]->Items;
366 for (unsigned j = 0; j < Items.size(); j++) {
367 std::string Name = Mang->getValueName(Items[j]);
368 Constant *C = Items[j]->getInitializer();
369 const Type *Ty = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000370 unsigned Size = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000371
372 O << Name << " " <<"RES"<< " " << Size ;
373 O << "\n";
374 }
375 }
376}
377
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000378void PIC16AsmPrinter::EmitAutos (std::string FunctName)
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000379{
380 // Section names for all globals are already set.
381
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000382 const TargetData *TD = TM.getTargetData();
383
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000384 // Now print Autos section for this function.
385 std::string SectionName = PAN::getAutosSectionName(FunctName);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000386 std::vector <PIC16Section *>AutosSections = PTAI->AutosSections;
387 for (unsigned i = 0; i < AutosSections.size(); i++) {
388 O << "\n";
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000389 if (AutosSections[i]->S_->getName() == SectionName) {
390 SwitchToSection(AutosSections[i]->S_);
391 std::vector<const GlobalVariable*> Items = AutosSections[i]->Items;
392 for (unsigned j = 0; j < Items.size(); j++) {
393 std::string VarName = Mang->getValueName(Items[j]);
394 Constant *C = Items[j]->getInitializer();
395 const Type *Ty = C->getType();
396 unsigned Size = TD->getTypeAllocSize(Ty);
397 // Emit memory reserve directive.
398 O << VarName << " RES " << Size << "\n";
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000399 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000400 break;
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000401 }
402 }
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000403}
404