blob: 549e2d9b471b1fb5683fbdeae04eaae13ef47b6e [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 Guptad0765702009-03-12 02:10:45 +000031inline static bool isLocalToFunc (std::string &FuncName, std::string &VarName) {
32 if (VarName.find(FuncName + ".auto.") != std::string::npos
33 || VarName.find(FuncName + ".arg.") != std::string::npos)
Sanjiv Gupta1b046942009-01-13 19:18:47 +000034 return true;
Sanjiv Gupta1b046942009-01-13 19:18:47 +000035
Sanjiv Guptad0765702009-03-12 02:10:45 +000036 return false;
37}
38
39inline static bool isLocalName (std::string &Name) {
40 if (Name.find(".auto.") != std::string::npos
41 || Name.find(".arg.") != std::string::npos)
42 return true;
43
Sanjiv Gupta1b046942009-01-13 19:18:47 +000044 return false;
45}
Sanjiv Gupta0e687712008-05-13 09:02:57 +000046
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000047bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Sanjiv Guptad0765702009-03-12 02:10:45 +000048 std::string NewBank = "";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000049 unsigned Operands = MI->getNumOperands();
50 if (Operands > 1) {
Sanjiv Guptab1f321b2009-04-23 10:34:58 +000051 // If we have a Global address or external symbol then we need to print
52 // banksel for it.
53 unsigned BankSelVar = 0;
54 MachineOperand Op = MI->getOperand(BankSelVar);
55 while (BankSelVar < Operands-1) {
56 Op = MI->getOperand(BankSelVar);
57 if ((Op.getType() == MachineOperand::MO_GlobalAddress) ||
58 (Op.getType() == MachineOperand::MO_ExternalSymbol))
59 break;
60 BankSelVar++;
61 }
62 if (BankSelVar < Operands-1) {
63 unsigned OpType = Op.getType();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000064 if (OpType == MachineOperand::MO_GlobalAddress )
Sanjiv Guptad0765702009-03-12 02:10:45 +000065 NewBank = Op.getGlobal()->getSection();
66 else {
Sanjiv Guptab1f321b2009-04-23 10:34:58 +000067 // External Symbol is generated for temp data and arguments. They are
68 // in fpdata.<functionname>.# section.
69 std::string ESName = Op.getSymbolName();
70 int index = ESName.find_first_of(".");
71 std::string FnName = ESName.substr(0,index);
72 NewBank = "fpdata." + FnName +".#";
Sanjiv Guptad0765702009-03-12 02:10:45 +000073 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000074 // Operand after global address or external symbol should be banksel.
75 // Value 1 for this operand means we need to generate banksel else do not
76 // generate banksel.
Sanjiv Guptad0765702009-03-12 02:10:45 +000077 const MachineOperand &BS = MI->getOperand(BankSelVar+1);
78 // If Section names are same then the variables are in same section.
79 // This is not true for external variables as section names for global
80 // variables in all files are same at this time. For eg. initialized
81 // data in put in idata.# section in all files.
Sanjiv Gupta37831d02009-04-09 17:06:24 +000082 if ((BS.getType() == MachineOperand::MO_Immediate
Sanjiv Guptab1f321b2009-04-23 10:34:58 +000083 && (int)BS.getImm() == 1)
Sanjiv Gupta37831d02009-04-09 17:06:24 +000084 && ((Op.isGlobal() && Op.getGlobal()->hasExternalLinkage()) ||
Sanjiv Guptad0765702009-03-12 02:10:45 +000085 (NewBank.compare(CurBank) != 0))) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000086 O << "\tbanksel ";
Sanjiv Guptad0765702009-03-12 02:10:45 +000087 printOperand(MI, BankSelVar);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000088 O << "\n";
Sanjiv Guptad0765702009-03-12 02:10:45 +000089 CurBank = NewBank;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000090 }
91 }
92 }
93 printInstruction(MI);
94 return true;
95}
96
97/// runOnMachineFunction - This uses the printInstruction()
98/// method to print assembly for each instruction.
99///
100bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000101 this->MF = &MF;
102
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000103 // This calls the base class function required to be called at beginning
104 // of runOnMachineFunction.
105 SetupMachineFunction(MF);
106
107 // Get the mangled name.
108 const Function *F = MF.getFunction();
109 CurrentFnName = Mang->getValueName(F);
110
111 // Emit the function variables.
112 emitFunctionData(MF);
113 std::string codeSection;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000114 codeSection = "code." + CurrentFnName + ".# " + "CODE";
115 const Section *fCodeSection = TAI->getNamedSection(codeSection.c_str(),
116 SectionFlags::Code);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000117 O << "\n";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000118 SwitchToSection (fCodeSection);
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000119
120 // Emit the frame address of the function at the beginning of code.
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000121 O << " retlw low(" << FunctionLabelBegin<< CurrentFnName << ".frame)\n";
122 O << " retlw high(" << FunctionLabelBegin<< CurrentFnName << ".frame)\n";
Sanjiv Guptadd92dba2009-04-22 12:02:36 +0000123 O << CurrentFnName << ":\n";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000124
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000125
126 // Print out code for the function.
127 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
128 I != E; ++I) {
129 // Print a label for the basic block.
130 if (I != MF.begin()) {
131 printBasicBlockLabel(I, true);
132 O << '\n';
133 }
Sanjiv Guptad0765702009-03-12 02:10:45 +0000134 CurBank = "";
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000135
136 // For emitting line directives, we need to keep track of the current
137 // source line. When it changes then only emit the line directive.
138 unsigned CurLine = 0;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000139 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
140 II != E; ++II) {
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000141 // Emit the line directive if source line changed.
142 const DebugLoc DL = II->getDebugLoc();
143 if (!DL.isUnknown()) {
144 unsigned line = MF.getDebugLocTuple(DL).Line;
145 if (line != CurLine) {
146 O << "\t.line " << line << "\n";
147 CurLine = line;
148 }
149 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000150 // Print the assembly for the instruction.
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000151 printMachineInstruction(II);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000152 }
153 }
154 return false; // we didn't modify anything.
155}
156
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000157/// createPIC16CodePrinterPass - Returns a pass that prints the PIC16
158/// assembly code for a MachineFunction to the given output stream,
159/// using the given target machine description. This should work
160/// regardless of whether the function is in SSA form.
161///
Owen Andersoncb371882008-08-21 00:14:44 +0000162FunctionPass *llvm::createPIC16CodePrinterPass(raw_ostream &o,
Bill Wendling57f0db82009-02-24 08:30:20 +0000163 PIC16TargetMachine &tm,
Evan Cheng42bf74b2009-03-25 01:47:28 +0000164 bool fast, bool verbose) {
165 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo(), fast, verbose);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000166}
167
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000168void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000169 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000170
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000171 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000172 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000173 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000174 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000175 else
176 assert(0 && "not implemented");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000177 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000178
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000179 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000180 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000181 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000182
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000183 case MachineOperand::MO_GlobalAddress: {
184 std::string Name = Mang->getValueName(MO.getGlobal());
185 if (isLocalName(Name))
186 O << FunctionLabelBegin << Mang->getValueName(MO.getGlobal());
187 else
188 O << Mang->getValueName(MO.getGlobal());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000189 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000190 }
191 case MachineOperand::MO_ExternalSymbol: {
192 std::string Name = MO.getSymbolName();
193 if (Name.find("__intrinsics.") != std::string::npos)
194 O << MO.getSymbolName();
195 else
196 O << FunctionLabelBegin << MO.getSymbolName();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000197 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000198 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000199 case MachineOperand::MO_MachineBasicBlock:
200 printBasicBlockLabel(MO.getMBB());
201 return;
202
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000203 default:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000204 assert(0 && " Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000205 }
206}
207
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000208void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
209 int CC = (int)MI->getOperand(opNum).getImm();
210 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
211}
212
213
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000214bool PIC16AsmPrinter::doInitialization (Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000215 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000216 // FIXME:: This is temporary solution to generate the include file.
217 // The processor should be passed to llc as in input and the header file
218 // should be generated accordingly.
219 O << "\t#include P16F1937.INC\n";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000220 MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
221 assert(MMI);
222 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
223 assert(DW && "Dwarf Writer is not available");
224 DW->BeginModule(&M, MMI, O, this, TAI);
225
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000226 EmitExternsAndGlobals (M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000227 EmitInitData (M);
228 EmitUnInitData(M);
229 EmitRomData(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000230 return Result;
231}
232
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000233void PIC16AsmPrinter::EmitExternsAndGlobals (Module &M) {
234 // Emit declarations for external functions.
235 O << "section.0" <<"\n";
236 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
237 std::string Name = Mang->getValueName(I);
238 if (Name.compare("abort") == 0)
239 continue;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000240
241 // If it is llvm intrinsic call then don't emit
242 if (Name.find("llvm.") != std::string::npos)
243 continue;
244
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000245 if (I->isDeclaration()) {
246 O << "\textern " <<Name << "\n";
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000247 O << "\textern " << FunctionLabelBegin << Name << ".retval\n";
248 O << "\textern " << FunctionLabelBegin << Name << ".args\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000249 }
250 else if (I->hasExternalLinkage()) {
251 O << "\tglobal " << Name << "\n";
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000252 O << "\tglobal " << FunctionLabelBegin << Name << ".retval\n";
253 O << "\tglobal " << FunctionLabelBegin<< Name << ".args\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000254 }
255 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000256
257 // Emit header file to include declaration of library functions
258 O << "\t#include C16IntrinsicCalls.INC\n";
259
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000260 // Emit declarations for external globals.
261 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
262 I != E; I++) {
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000263 // Any variables reaching here with ".auto." in its name is a local scope
264 // variable and should not be printed in global data section.
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000265 std::string Name = Mang->getValueName(I);
Sanjiv Guptad0765702009-03-12 02:10:45 +0000266 if (isLocalName (Name))
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000267 continue;
268
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000269 if (I->isDeclaration())
270 O << "\textern "<< Name << "\n";
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000271 else if (I->hasCommonLinkage() || I->hasExternalLinkage())
Sanjiv Guptaa2d8b062009-02-06 18:24:59 +0000272 O << "\tglobal "<< Name << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000273 }
274}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000275
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000276void PIC16AsmPrinter::EmitInitData (Module &M) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000277 SwitchToSection(TAI->getDataSection());
Sanjiv Guptad0765702009-03-12 02:10:45 +0000278 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000279 I != E; ++I) {
280 if (!I->hasInitializer()) // External global require no code.
281 continue;
282
283 Constant *C = I->getInitializer();
284 const PointerType *PtrTy = I->getType();
285 int AddrSpace = PtrTy->getAddressSpace();
286
287 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::RAM_SPACE)) {
288
289 if (EmitSpecialLLVMGlobal(I))
290 continue;
291
292 // Any variables reaching here with "." in its name is a local scope
293 // variable and should not be printed in global data section.
Sanjiv Guptad0765702009-03-12 02:10:45 +0000294 std::string Name = Mang->getValueName(I);
295 if (isLocalName(Name))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000296 continue;
297
Sanjiv Guptad0765702009-03-12 02:10:45 +0000298 I->setSection(TAI->getDataSection()->getName());
299 O << Name;
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000300 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000301 }
302 }
303}
304
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000305void PIC16AsmPrinter::EmitRomData (Module &M)
306{
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000307 SwitchToSection(TAI->getReadOnlySection());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000308 IsRomData = true;
Sanjiv Guptad0765702009-03-12 02:10:45 +0000309 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000310 I != E; ++I) {
311 if (!I->hasInitializer()) // External global require no code.
312 continue;
313
314 Constant *C = I->getInitializer();
315 const PointerType *PtrTy = I->getType();
316 int AddrSpace = PtrTy->getAddressSpace();
317 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::ROM_SPACE)) {
318
319 if (EmitSpecialLLVMGlobal(I))
320 continue;
321
322 // Any variables reaching here with "." in its name is a local scope
323 // variable and should not be printed in global data section.
324 std::string name = Mang->getValueName(I);
325 if (name.find(".") != std::string::npos)
326 continue;
327
Sanjiv Guptad0765702009-03-12 02:10:45 +0000328 I->setSection(TAI->getReadOnlySection()->getName());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000329 O << name;
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000330 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000331 O << "\n";
332 }
333 }
334 IsRomData = false;
335}
336
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000337void PIC16AsmPrinter::EmitUnInitData (Module &M)
338{
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000339 SwitchToSection(TAI->getBSSSection_());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000340 const TargetData *TD = TM.getTargetData();
341
Sanjiv Guptad0765702009-03-12 02:10:45 +0000342 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000343 I != E; ++I) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000344 if (!I->hasInitializer()) // External global require no code.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000345 continue;
346
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000347 Constant *C = I->getInitializer();
348 if (C->isNullValue()) {
349
350 if (EmitSpecialLLVMGlobal(I))
351 continue;
352
353 // Any variables reaching here with "." in its name is a local scope
354 // variable and should not be printed in global data section.
355 std::string name = Mang->getValueName(I);
356 if (name.find(".") != std::string::npos)
357 continue;
358
Sanjiv Guptad0765702009-03-12 02:10:45 +0000359 I->setSection(TAI->getBSSSection_()->getName());
360
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000361 const Type *Ty = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000362 unsigned Size = TD->getTypePaddedSize(Ty);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000363
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000364 O << name << " " <<"RES"<< " " << Size ;
365 O << "\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000366 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000367 }
368}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000369
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000370bool PIC16AsmPrinter::doFinalization(Module &M) {
371 O << "\t" << "END\n";
372 bool Result = AsmPrinter::doFinalization(M);
373 return Result;
374}
375
376void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
377 const Function *F = MF.getFunction();
378 std::string FuncName = Mang->getValueName(F);
Sanjiv Guptad0765702009-03-12 02:10:45 +0000379 Module *M = const_cast<Module *>(F->getParent());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000380 const TargetData *TD = TM.getTargetData();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000381 unsigned FrameSize = 0;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000382 // Emit the data section name.
383 O << "\n";
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000384 std::string SectionName = "fpdata." + CurrentFnName + ".# " + "UDATA_OVR";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000385
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000386 const Section *fPDataSection = TAI->getNamedSection(SectionName.c_str(),
387 SectionFlags::Writeable);
388 SwitchToSection(fPDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000389
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000390
391 // Emit function frame label
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000392 O << FunctionLabelBegin << CurrentFnName << ".frame:\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000393
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000394 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000395 unsigned RetSize = 0;
396 if (RetType->getTypeID() != Type::VoidTyID)
397 RetSize = TD->getTypePaddedSize(RetType);
398
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000399 //Emit function return value space
400 if(RetSize > 0)
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000401 O << FunctionLabelBegin << CurrentFnName << ".retval RES " << RetSize
402 << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000403 else
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000404 O << FunctionLabelBegin << CurrentFnName << ".retval:\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000405
406 // Emit variable to hold the space for function arguments
407 unsigned ArgSize = 0;
408 for (Function::const_arg_iterator argi = F->arg_begin(),
409 arge = F->arg_end(); argi != arge ; ++argi) {
410 const Type *Ty = argi->getType();
411 ArgSize += TD->getTypePaddedSize(Ty);
412 }
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000413 O << FunctionLabelBegin << CurrentFnName << ".args RES " << ArgSize
414 << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000415
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000416 // Emit temporary space
417 int TempSize = PTLI->GetTmpSize();
418 if (TempSize > 0 )
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000419 O << FunctionLabelBegin << CurrentFnName << ".tmp RES " << TempSize
420 <<"\n";
421
422 // Emit the section name for local variables.
423 O << "\n";
424 std::string SecNameLocals = "fadata." + CurrentFnName + ".# " + "UDATA_OVR";
425
426 const Section *fADataSection = TAI->getNamedSection(SecNameLocals.c_str(),
427 SectionFlags::Writeable);
428 SwitchToSection(fADataSection);
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000429
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000430 // Emit the function variables.
431
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000432 // In PIC16 all the function arguments and local variables are global.
433 // Therefore to get the variable belonging to this function entire
434 // global list will be traversed and variables belonging to this function
435 // will be emitted in the current data section.
Sanjiv Guptad0765702009-03-12 02:10:45 +0000436 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000437 I != E; ++I) {
438 std::string VarName = Mang->getValueName(I);
439
440 // The variables of a function are of form FuncName.* . If this variable
441 // does not belong to this function then continue.
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000442 // Static local varilabes of a function does not have .auto. in their
443 // name. They are not printed as part of function data but module
444 // level global data.
Sanjiv Guptad0765702009-03-12 02:10:45 +0000445 if (! isLocalToFunc(FuncName, VarName))
446 continue;
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000447
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000448 I->setSection("fadata." + CurrentFnName + ".#");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000449 Constant *C = I->getInitializer();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000450 const Type *Ty = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000451 unsigned Size = TD->getTypePaddedSize(Ty);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000452 FrameSize += Size;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000453 // Emit memory reserve directive.
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000454 O << FunctionLabelBegin << VarName << " RES " << Size << "\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000455 }
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000456
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000457}