blob: 510e105ebd22a0a7523ea498e7b6697a51cc37bf [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,
Bill Wendling98a366d2009-04-29 23:29:43 +0000164 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000165 bool verbose) {
166 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000167}
168
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000169void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000170 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000171
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000172 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000173 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000174 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000175 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000176 else
177 assert(0 && "not implemented");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000178 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000179
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000180 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000181 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000182 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000183
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000184 case MachineOperand::MO_GlobalAddress: {
185 std::string Name = Mang->getValueName(MO.getGlobal());
186 if (isLocalName(Name))
187 O << FunctionLabelBegin << Mang->getValueName(MO.getGlobal());
188 else
189 O << Mang->getValueName(MO.getGlobal());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000190 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000191 }
192 case MachineOperand::MO_ExternalSymbol: {
193 std::string Name = MO.getSymbolName();
194 if (Name.find("__intrinsics.") != std::string::npos)
195 O << MO.getSymbolName();
196 else
197 O << FunctionLabelBegin << MO.getSymbolName();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000198 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000199 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000200 case MachineOperand::MO_MachineBasicBlock:
201 printBasicBlockLabel(MO.getMBB());
202 return;
203
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000204 default:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000205 assert(0 && " Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000206 }
207}
208
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000209void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
210 int CC = (int)MI->getOperand(opNum).getImm();
211 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
212}
213
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 Guptab1b5ffd2008-11-19 11:00:54 +0000217 // FIXME:: This is temporary solution to generate the include file.
218 // The processor should be passed to llc as in input and the header file
219 // should be generated accordingly.
220 O << "\t#include P16F1937.INC\n";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000221 MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
222 assert(MMI);
223 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
224 assert(DW && "Dwarf Writer is not available");
225 DW->BeginModule(&M, MMI, O, this, TAI);
226
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000227 EmitExternsAndGlobals (M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000228 EmitInitData (M);
229 EmitUnInitData(M);
230 EmitRomData(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000231 return Result;
232}
233
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000234void PIC16AsmPrinter::EmitExternsAndGlobals (Module &M) {
235 // Emit declarations for external functions.
236 O << "section.0" <<"\n";
237 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
238 std::string Name = Mang->getValueName(I);
239 if (Name.compare("abort") == 0)
240 continue;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000241
242 // If it is llvm intrinsic call then don't emit
243 if (Name.find("llvm.") != std::string::npos)
244 continue;
245
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000246 if (I->isDeclaration()) {
247 O << "\textern " <<Name << "\n";
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000248 O << "\textern " << FunctionLabelBegin << Name << ".retval\n";
249 O << "\textern " << FunctionLabelBegin << Name << ".args\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000250 }
251 else if (I->hasExternalLinkage()) {
252 O << "\tglobal " << Name << "\n";
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000253 O << "\tglobal " << FunctionLabelBegin << Name << ".retval\n";
254 O << "\tglobal " << FunctionLabelBegin<< Name << ".args\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000255 }
256 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000257
258 // Emit header file to include declaration of library functions
259 O << "\t#include C16IntrinsicCalls.INC\n";
260
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000261 // Emit declarations for external globals.
262 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
263 I != E; I++) {
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000264 // Any variables reaching here with ".auto." in its name is a local scope
265 // variable and should not be printed in global data section.
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000266 std::string Name = Mang->getValueName(I);
Sanjiv Guptad0765702009-03-12 02:10:45 +0000267 if (isLocalName (Name))
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000268 continue;
269
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000270 if (I->isDeclaration())
271 O << "\textern "<< Name << "\n";
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000272 else if (I->hasCommonLinkage() || I->hasExternalLinkage())
Sanjiv Guptaa2d8b062009-02-06 18:24:59 +0000273 O << "\tglobal "<< Name << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000274 }
275}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000276
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000277void PIC16AsmPrinter::EmitInitData (Module &M) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000278 SwitchToSection(TAI->getDataSection());
Sanjiv Guptad0765702009-03-12 02:10:45 +0000279 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000280 I != E; ++I) {
281 if (!I->hasInitializer()) // External global require no code.
282 continue;
283
284 Constant *C = I->getInitializer();
285 const PointerType *PtrTy = I->getType();
286 int AddrSpace = PtrTy->getAddressSpace();
287
288 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::RAM_SPACE)) {
289
290 if (EmitSpecialLLVMGlobal(I))
291 continue;
292
293 // Any variables reaching here with "." in its name is a local scope
294 // variable and should not be printed in global data section.
Sanjiv Guptad0765702009-03-12 02:10:45 +0000295 std::string Name = Mang->getValueName(I);
296 if (isLocalName(Name))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000297 continue;
298
Sanjiv Guptad0765702009-03-12 02:10:45 +0000299 I->setSection(TAI->getDataSection()->getName());
300 O << Name;
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000301 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000302 }
303 }
304}
305
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000306void PIC16AsmPrinter::EmitRomData (Module &M)
307{
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000308 SwitchToSection(TAI->getReadOnlySection());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000309 IsRomData = true;
Sanjiv Guptad0765702009-03-12 02:10:45 +0000310 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000311 I != E; ++I) {
312 if (!I->hasInitializer()) // External global require no code.
313 continue;
314
315 Constant *C = I->getInitializer();
316 const PointerType *PtrTy = I->getType();
317 int AddrSpace = PtrTy->getAddressSpace();
318 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::ROM_SPACE)) {
319
320 if (EmitSpecialLLVMGlobal(I))
321 continue;
322
323 // Any variables reaching here with "." in its name is a local scope
324 // variable and should not be printed in global data section.
325 std::string name = Mang->getValueName(I);
326 if (name.find(".") != std::string::npos)
327 continue;
328
Sanjiv Guptad0765702009-03-12 02:10:45 +0000329 I->setSection(TAI->getReadOnlySection()->getName());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000330 O << name;
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000331 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000332 O << "\n";
333 }
334 }
335 IsRomData = false;
336}
337
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000338void PIC16AsmPrinter::EmitUnInitData (Module &M)
339{
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000340 SwitchToSection(TAI->getBSSSection_());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000341 const TargetData *TD = TM.getTargetData();
342
Sanjiv Guptad0765702009-03-12 02:10:45 +0000343 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000344 I != E; ++I) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000345 if (!I->hasInitializer()) // External global require no code.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000346 continue;
347
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000348 Constant *C = I->getInitializer();
349 if (C->isNullValue()) {
350
351 if (EmitSpecialLLVMGlobal(I))
352 continue;
353
354 // Any variables reaching here with "." in its name is a local scope
355 // variable and should not be printed in global data section.
356 std::string name = Mang->getValueName(I);
357 if (name.find(".") != std::string::npos)
358 continue;
359
Sanjiv Guptad0765702009-03-12 02:10:45 +0000360 I->setSection(TAI->getBSSSection_()->getName());
361
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000362 const Type *Ty = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000363 unsigned Size = TD->getTypePaddedSize(Ty);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000364
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000365 O << name << " " <<"RES"<< " " << Size ;
366 O << "\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000367 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000368 }
369}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000370
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000371bool PIC16AsmPrinter::doFinalization(Module &M) {
372 O << "\t" << "END\n";
373 bool Result = AsmPrinter::doFinalization(M);
374 return Result;
375}
376
377void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
378 const Function *F = MF.getFunction();
379 std::string FuncName = Mang->getValueName(F);
Sanjiv Guptad0765702009-03-12 02:10:45 +0000380 Module *M = const_cast<Module *>(F->getParent());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000381 const TargetData *TD = TM.getTargetData();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000382 unsigned FrameSize = 0;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000383 // Emit the data section name.
384 O << "\n";
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000385 std::string SectionName = "fpdata." + CurrentFnName + ".# " + "UDATA_OVR";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000386
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000387 const Section *fPDataSection = TAI->getNamedSection(SectionName.c_str(),
388 SectionFlags::Writeable);
389 SwitchToSection(fPDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000390
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000391
392 // Emit function frame label
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000393 O << FunctionLabelBegin << CurrentFnName << ".frame:\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000394
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000395 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000396 unsigned RetSize = 0;
397 if (RetType->getTypeID() != Type::VoidTyID)
398 RetSize = TD->getTypePaddedSize(RetType);
399
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000400 //Emit function return value space
401 if(RetSize > 0)
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000402 O << FunctionLabelBegin << CurrentFnName << ".retval RES " << RetSize
403 << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000404 else
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000405 O << FunctionLabelBegin << CurrentFnName << ".retval:\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000406
407 // Emit variable to hold the space for function arguments
408 unsigned ArgSize = 0;
409 for (Function::const_arg_iterator argi = F->arg_begin(),
410 arge = F->arg_end(); argi != arge ; ++argi) {
411 const Type *Ty = argi->getType();
412 ArgSize += TD->getTypePaddedSize(Ty);
413 }
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000414 O << FunctionLabelBegin << CurrentFnName << ".args RES " << ArgSize
415 << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000416
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000417 // Emit temporary space
418 int TempSize = PTLI->GetTmpSize();
419 if (TempSize > 0 )
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000420 O << FunctionLabelBegin << CurrentFnName << ".tmp RES " << TempSize
421 <<"\n";
422
423 // Emit the section name for local variables.
424 O << "\n";
425 std::string SecNameLocals = "fadata." + CurrentFnName + ".# " + "UDATA_OVR";
426
427 const Section *fADataSection = TAI->getNamedSection(SecNameLocals.c_str(),
428 SectionFlags::Writeable);
429 SwitchToSection(fADataSection);
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000430
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000431 // Emit the function variables.
432
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000433 // In PIC16 all the function arguments and local variables are global.
434 // Therefore to get the variable belonging to this function entire
435 // global list will be traversed and variables belonging to this function
436 // will be emitted in the current data section.
Sanjiv Guptad0765702009-03-12 02:10:45 +0000437 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000438 I != E; ++I) {
439 std::string VarName = Mang->getValueName(I);
440
441 // The variables of a function are of form FuncName.* . If this variable
442 // does not belong to this function then continue.
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000443 // Static local varilabes of a function does not have .auto. in their
444 // name. They are not printed as part of function data but module
445 // level global data.
Sanjiv Guptad0765702009-03-12 02:10:45 +0000446 if (! isLocalToFunc(FuncName, VarName))
447 continue;
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000448
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000449 I->setSection("fadata." + CurrentFnName + ".#");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000450 Constant *C = I->getInitializer();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000451 const Type *Ty = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000452 unsigned Size = TD->getTypePaddedSize(Ty);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000453 FrameSize += Size;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000454 // Emit memory reserve directive.
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000455 O << FunctionLabelBegin << VarName << " RES " << Size << "\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000456 }
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000457
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000458}