blob: 2af42cd16d81689e6e3040e021fffe94a004246b [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) {
51 // Global address or external symbol should be second operand from last
52 // if we want to print banksel for it.
Sanjiv Guptad0765702009-03-12 02:10:45 +000053 unsigned BankSelVar = Operands - 2;
54 // In cases where an instruction has a def or use defined in td file,
55 // that def or use becomes a machine instruction operand.
56 // eg. addfw_1 instruction defines STATUS register. So the machine
57 // instruction for it has MO_Register Operand as its last operand.
58 while ((MI->getOperand(BankSelVar + 1).getType() ==
59 MachineOperand::MO_Register) && (BankSelVar > 0))
60 BankSelVar--;
61 const MachineOperand &Op = MI->getOperand(BankSelVar);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000062 unsigned OpType = Op.getType();
63 if (OpType == MachineOperand::MO_GlobalAddress ||
Sanjiv Guptad0765702009-03-12 02:10:45 +000064 OpType == MachineOperand::MO_ExternalSymbol) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000065 if (OpType == MachineOperand::MO_GlobalAddress )
Sanjiv Guptad0765702009-03-12 02:10:45 +000066 NewBank = Op.getGlobal()->getSection();
67 else {
68 // External Symbol is generated for temp data. Temp data in in
69 // fdata.<functionname>.# section.
70 NewBank = "fdata." + CurrentFnName +".#";
71 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000072 // Operand after global address or external symbol should be banksel.
73 // Value 1 for this operand means we need to generate banksel else do not
74 // generate banksel.
Sanjiv Guptad0765702009-03-12 02:10:45 +000075 const MachineOperand &BS = MI->getOperand(BankSelVar+1);
76 // If Section names are same then the variables are in same section.
77 // This is not true for external variables as section names for global
78 // variables in all files are same at this time. For eg. initialized
79 // data in put in idata.# section in all files.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000080 if (((int)BS.getImm() == 1) &&
Sanjiv Guptad0765702009-03-12 02:10:45 +000081 ((Op.isGlobal() && Op.getGlobal()->hasExternalLinkage()) ||
82 (NewBank.compare(CurBank) != 0))) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000083 O << "\tbanksel ";
Sanjiv Guptad0765702009-03-12 02:10:45 +000084 printOperand(MI, BankSelVar);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000085 O << "\n";
Sanjiv Guptad0765702009-03-12 02:10:45 +000086 CurBank = NewBank;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000087 }
88 }
89 }
90 printInstruction(MI);
91 return true;
92}
93
94/// runOnMachineFunction - This uses the printInstruction()
95/// method to print assembly for each instruction.
96///
97bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +000098 this->MF = &MF;
99
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000100 // This calls the base class function required to be called at beginning
101 // of runOnMachineFunction.
102 SetupMachineFunction(MF);
103
104 // Get the mangled name.
105 const Function *F = MF.getFunction();
106 CurrentFnName = Mang->getValueName(F);
107
108 // Emit the function variables.
109 emitFunctionData(MF);
110 std::string codeSection;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000111 codeSection = "code." + CurrentFnName + ".# " + "CODE";
112 const Section *fCodeSection = TAI->getNamedSection(codeSection.c_str(),
113 SectionFlags::Code);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000114 O << "\n";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000115 SwitchToSection (fCodeSection);
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000116
117 // Emit the frame address of the function at the beginning of code.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000118 O << CurrentFnName << ":\n";
119 O << " retlw low(" << CurrentFnName << ".frame)\n";
120 O << " retlw high(" << CurrentFnName << ".frame)\n";
121
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000122
123 // Print out code for the function.
124 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
125 I != E; ++I) {
126 // Print a label for the basic block.
127 if (I != MF.begin()) {
128 printBasicBlockLabel(I, true);
129 O << '\n';
130 }
Sanjiv Guptad0765702009-03-12 02:10:45 +0000131 CurBank = "";
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000132
133 // For emitting line directives, we need to keep track of the current
134 // source line. When it changes then only emit the line directive.
135 unsigned CurLine = 0;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000136 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
137 II != E; ++II) {
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000138 // Emit the line directive if source line changed.
139 const DebugLoc DL = II->getDebugLoc();
140 if (!DL.isUnknown()) {
141 unsigned line = MF.getDebugLocTuple(DL).Line;
142 if (line != CurLine) {
143 O << "\t.line " << line << "\n";
144 CurLine = line;
145 }
146 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000147 // Print the assembly for the instruction.
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000148 printMachineInstruction(II);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000149 }
150 }
151 return false; // we didn't modify anything.
152}
153
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000154/// createPIC16CodePrinterPass - Returns a pass that prints the PIC16
155/// assembly code for a MachineFunction to the given output stream,
156/// using the given target machine description. This should work
157/// regardless of whether the function is in SSA form.
158///
Owen Andersoncb371882008-08-21 00:14:44 +0000159FunctionPass *llvm::createPIC16CodePrinterPass(raw_ostream &o,
Bill Wendling57f0db82009-02-24 08:30:20 +0000160 PIC16TargetMachine &tm,
Evan Cheng42bf74b2009-03-25 01:47:28 +0000161 bool fast, bool verbose) {
162 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo(), fast, verbose);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000163}
164
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000165void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000166 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000167
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000168 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000169 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000170 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000171 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000172 else
173 assert(0 && "not implemented");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000174 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000175
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000176 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000177 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000178 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000179
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000180 case MachineOperand::MO_GlobalAddress:
181 O << Mang->getValueName(MO.getGlobal());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000182 break;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000183
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000184 case MachineOperand::MO_ExternalSymbol:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000185 O << MO.getSymbolName();
186 break;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000187
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000188 case MachineOperand::MO_MachineBasicBlock:
189 printBasicBlockLabel(MO.getMBB());
190 return;
191
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000192 default:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000193 assert(0 && " Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000194 }
195}
196
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000197void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
198 int CC = (int)MI->getOperand(opNum).getImm();
199 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
200}
201
202
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000203bool PIC16AsmPrinter::doInitialization (Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000204 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000205 // FIXME:: This is temporary solution to generate the include file.
206 // The processor should be passed to llc as in input and the header file
207 // should be generated accordingly.
208 O << "\t#include P16F1937.INC\n";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000209 MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
210 assert(MMI);
211 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
212 assert(DW && "Dwarf Writer is not available");
213 DW->BeginModule(&M, MMI, O, this, TAI);
214
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000215 EmitExternsAndGlobals (M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000216 EmitInitData (M);
217 EmitUnInitData(M);
218 EmitRomData(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000219 return Result;
220}
221
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000222void PIC16AsmPrinter::EmitExternsAndGlobals (Module &M) {
223 // Emit declarations for external functions.
224 O << "section.0" <<"\n";
225 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
226 std::string Name = Mang->getValueName(I);
227 if (Name.compare("abort") == 0)
228 continue;
229 if (I->isDeclaration()) {
230 O << "\textern " <<Name << "\n";
231 O << "\textern " << Name << ".retval\n";
232 O << "\textern " << Name << ".args\n";
233 }
234 else if (I->hasExternalLinkage()) {
235 O << "\tglobal " << Name << "\n";
236 O << "\tglobal " << Name << ".retval\n";
237 O << "\tglobal " << Name << ".args\n";
238 }
239 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000240
241 // Emit header file to include declaration of library functions
242 O << "\t#include C16IntrinsicCalls.INC\n";
243
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000244 // Emit declarations for external globals.
245 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
246 I != E; I++) {
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000247 // Any variables reaching here with ".auto." in its name is a local scope
248 // variable and should not be printed in global data section.
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000249 std::string Name = Mang->getValueName(I);
Sanjiv Guptad0765702009-03-12 02:10:45 +0000250 if (isLocalName (Name))
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000251 continue;
252
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000253 if (I->isDeclaration())
254 O << "\textern "<< Name << "\n";
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000255 else if (I->hasCommonLinkage() || I->hasExternalLinkage())
Sanjiv Guptaa2d8b062009-02-06 18:24:59 +0000256 O << "\tglobal "<< Name << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000257 }
258}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000259
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000260void PIC16AsmPrinter::EmitInitData (Module &M) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000261 SwitchToSection(TAI->getDataSection());
Sanjiv Guptad0765702009-03-12 02:10:45 +0000262 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000263 I != E; ++I) {
264 if (!I->hasInitializer()) // External global require no code.
265 continue;
266
267 Constant *C = I->getInitializer();
268 const PointerType *PtrTy = I->getType();
269 int AddrSpace = PtrTy->getAddressSpace();
270
271 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::RAM_SPACE)) {
272
273 if (EmitSpecialLLVMGlobal(I))
274 continue;
275
276 // Any variables reaching here with "." in its name is a local scope
277 // variable and should not be printed in global data section.
Sanjiv Guptad0765702009-03-12 02:10:45 +0000278 std::string Name = Mang->getValueName(I);
279 if (isLocalName(Name))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000280 continue;
281
Sanjiv Guptad0765702009-03-12 02:10:45 +0000282 I->setSection(TAI->getDataSection()->getName());
283 O << Name;
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000284 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000285 }
286 }
287}
288
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000289void PIC16AsmPrinter::EmitRomData (Module &M)
290{
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000291 SwitchToSection(TAI->getReadOnlySection());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000292 IsRomData = true;
Sanjiv Guptad0765702009-03-12 02:10:45 +0000293 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000294 I != E; ++I) {
295 if (!I->hasInitializer()) // External global require no code.
296 continue;
297
298 Constant *C = I->getInitializer();
299 const PointerType *PtrTy = I->getType();
300 int AddrSpace = PtrTy->getAddressSpace();
301 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::ROM_SPACE)) {
302
303 if (EmitSpecialLLVMGlobal(I))
304 continue;
305
306 // Any variables reaching here with "." in its name is a local scope
307 // variable and should not be printed in global data section.
308 std::string name = Mang->getValueName(I);
309 if (name.find(".") != std::string::npos)
310 continue;
311
Sanjiv Guptad0765702009-03-12 02:10:45 +0000312 I->setSection(TAI->getReadOnlySection()->getName());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000313 O << name;
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000314 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000315 O << "\n";
316 }
317 }
318 IsRomData = false;
319}
320
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000321void PIC16AsmPrinter::EmitUnInitData (Module &M)
322{
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000323 SwitchToSection(TAI->getBSSSection_());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000324 const TargetData *TD = TM.getTargetData();
325
Sanjiv Guptad0765702009-03-12 02:10:45 +0000326 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000327 I != E; ++I) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000328 if (!I->hasInitializer()) // External global require no code.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000329 continue;
330
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000331 Constant *C = I->getInitializer();
332 if (C->isNullValue()) {
333
334 if (EmitSpecialLLVMGlobal(I))
335 continue;
336
337 // Any variables reaching here with "." in its name is a local scope
338 // variable and should not be printed in global data section.
339 std::string name = Mang->getValueName(I);
340 if (name.find(".") != std::string::npos)
341 continue;
342
Sanjiv Guptad0765702009-03-12 02:10:45 +0000343 I->setSection(TAI->getBSSSection_()->getName());
344
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000345 const Type *Ty = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000346 unsigned Size = TD->getTypePaddedSize(Ty);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000347
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000348 O << name << " " <<"RES"<< " " << Size ;
349 O << "\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000350 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000351 }
352}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000353
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000354bool PIC16AsmPrinter::doFinalization(Module &M) {
355 O << "\t" << "END\n";
356 bool Result = AsmPrinter::doFinalization(M);
357 return Result;
358}
359
360void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
361 const Function *F = MF.getFunction();
362 std::string FuncName = Mang->getValueName(F);
Sanjiv Guptad0765702009-03-12 02:10:45 +0000363 Module *M = const_cast<Module *>(F->getParent());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000364 const TargetData *TD = TM.getTargetData();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000365 unsigned FrameSize = 0;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000366 // Emit the data section name.
367 O << "\n";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000368 std::string SectionName = "fdata." + CurrentFnName + ".# " + "UDATA";
369
370 const Section *fDataSection = TAI->getNamedSection(SectionName.c_str(),
371 SectionFlags::Writeable);
372 SwitchToSection(fDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000373
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000374
375 // Emit function frame label
376 O << CurrentFnName << ".frame:\n";
377
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000378 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000379 unsigned RetSize = 0;
380 if (RetType->getTypeID() != Type::VoidTyID)
381 RetSize = TD->getTypePaddedSize(RetType);
382
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000383 //Emit function return value space
384 if(RetSize > 0)
385 O << CurrentFnName << ".retval RES " << RetSize << "\n";
386 else
387 O << CurrentFnName << ".retval:\n";
388
389 // Emit variable to hold the space for function arguments
390 unsigned ArgSize = 0;
391 for (Function::const_arg_iterator argi = F->arg_begin(),
392 arge = F->arg_end(); argi != arge ; ++argi) {
393 const Type *Ty = argi->getType();
394 ArgSize += TD->getTypePaddedSize(Ty);
395 }
396 O << CurrentFnName << ".args RES " << ArgSize << "\n";
397
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000398 // Emit the function variables.
399
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000400 // In PIC16 all the function arguments and local variables are global.
401 // Therefore to get the variable belonging to this function entire
402 // global list will be traversed and variables belonging to this function
403 // will be emitted in the current data section.
Sanjiv Guptad0765702009-03-12 02:10:45 +0000404 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000405 I != E; ++I) {
406 std::string VarName = Mang->getValueName(I);
407
408 // The variables of a function are of form FuncName.* . If this variable
409 // does not belong to this function then continue.
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000410 // Static local varilabes of a function does not have .auto. in their
411 // name. They are not printed as part of function data but module
412 // level global data.
Sanjiv Guptad0765702009-03-12 02:10:45 +0000413 if (! isLocalToFunc(FuncName, VarName))
414 continue;
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000415
Sanjiv Guptad0765702009-03-12 02:10:45 +0000416 I->setSection("fdata." + CurrentFnName + ".#");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000417 Constant *C = I->getInitializer();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000418 const Type *Ty = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000419 unsigned Size = TD->getTypePaddedSize(Ty);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000420 FrameSize += Size;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000421 // Emit memory reserve directive.
422 O << VarName << " RES " << Size << "\n";
423 }
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000424
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000425 int TempSize = PTLI->GetTmpSize();
426 if (TempSize > 0 )
427 O << CurrentFnName << ".tmp RES " << TempSize <<"\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000428}