blob: 5c4b353294cc4ab26ee9a419efeddbd384a2dd44 [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 Gupta37831d02009-04-09 17:06:24 +000080 if ((BS.getType() == MachineOperand::MO_Immediate
81 && (int)BS.getImm() == 1)
82 && ((Op.isGlobal() && Op.getGlobal()->hasExternalLinkage()) ||
Sanjiv Guptad0765702009-03-12 02:10:45 +000083 (NewBank.compare(CurBank) != 0))) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000084 O << "\tbanksel ";
Sanjiv Guptad0765702009-03-12 02:10:45 +000085 printOperand(MI, BankSelVar);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000086 O << "\n";
Sanjiv Guptad0765702009-03-12 02:10:45 +000087 CurBank = NewBank;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000088 }
89 }
90 }
91 printInstruction(MI);
92 return true;
93}
94
95/// runOnMachineFunction - This uses the printInstruction()
96/// method to print assembly for each instruction.
97///
98bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +000099 this->MF = &MF;
100
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000101 // This calls the base class function required to be called at beginning
102 // of runOnMachineFunction.
103 SetupMachineFunction(MF);
104
105 // Get the mangled name.
106 const Function *F = MF.getFunction();
107 CurrentFnName = Mang->getValueName(F);
108
109 // Emit the function variables.
110 emitFunctionData(MF);
111 std::string codeSection;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000112 codeSection = "code." + CurrentFnName + ".# " + "CODE";
113 const Section *fCodeSection = TAI->getNamedSection(codeSection.c_str(),
114 SectionFlags::Code);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000115 O << "\n";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000116 SwitchToSection (fCodeSection);
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000117
118 // Emit the frame address of the function at the beginning of code.
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000119 O << CurrentFnName << ":\n";
120 O << " retlw low(" << CurrentFnName << ".frame)\n";
121 O << " retlw high(" << CurrentFnName << ".frame)\n";
122
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000123
124 // Print out code for the function.
125 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
126 I != E; ++I) {
127 // Print a label for the basic block.
128 if (I != MF.begin()) {
129 printBasicBlockLabel(I, true);
130 O << '\n';
131 }
Sanjiv Guptad0765702009-03-12 02:10:45 +0000132 CurBank = "";
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000133
134 // For emitting line directives, we need to keep track of the current
135 // source line. When it changes then only emit the line directive.
136 unsigned CurLine = 0;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000137 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
138 II != E; ++II) {
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000139 // Emit the line directive if source line changed.
140 const DebugLoc DL = II->getDebugLoc();
141 if (!DL.isUnknown()) {
142 unsigned line = MF.getDebugLocTuple(DL).Line;
143 if (line != CurLine) {
144 O << "\t.line " << line << "\n";
145 CurLine = line;
146 }
147 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000148 // Print the assembly for the instruction.
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000149 printMachineInstruction(II);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000150 }
151 }
152 return false; // we didn't modify anything.
153}
154
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000155/// createPIC16CodePrinterPass - Returns a pass that prints the PIC16
156/// assembly code for a MachineFunction to the given output stream,
157/// using the given target machine description. This should work
158/// regardless of whether the function is in SSA form.
159///
Owen Andersoncb371882008-08-21 00:14:44 +0000160FunctionPass *llvm::createPIC16CodePrinterPass(raw_ostream &o,
Bill Wendling57f0db82009-02-24 08:30:20 +0000161 PIC16TargetMachine &tm,
Evan Cheng42bf74b2009-03-25 01:47:28 +0000162 bool fast, bool verbose) {
163 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo(), fast, verbose);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000164}
165
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000166void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000167 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000168
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000169 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000170 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000171 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000172 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000173 else
174 assert(0 && "not implemented");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000175 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000176
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000177 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000178 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000179 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000180
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000181 case MachineOperand::MO_GlobalAddress:
182 O << Mang->getValueName(MO.getGlobal());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000183 break;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000184
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000185 case MachineOperand::MO_ExternalSymbol:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000186 O << MO.getSymbolName();
187 break;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000188
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000189 case MachineOperand::MO_MachineBasicBlock:
190 printBasicBlockLabel(MO.getMBB());
191 return;
192
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000193 default:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000194 assert(0 && " Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000195 }
196}
197
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000198void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
199 int CC = (int)MI->getOperand(opNum).getImm();
200 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
201}
202
203
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000204bool PIC16AsmPrinter::doInitialization (Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000205 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000206 // FIXME:: This is temporary solution to generate the include file.
207 // The processor should be passed to llc as in input and the header file
208 // should be generated accordingly.
209 O << "\t#include P16F1937.INC\n";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000210 MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
211 assert(MMI);
212 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
213 assert(DW && "Dwarf Writer is not available");
214 DW->BeginModule(&M, MMI, O, this, TAI);
215
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000216 EmitExternsAndGlobals (M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000217 EmitInitData (M);
218 EmitUnInitData(M);
219 EmitRomData(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000220 return Result;
221}
222
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000223void PIC16AsmPrinter::EmitExternsAndGlobals (Module &M) {
224 // Emit declarations for external functions.
225 O << "section.0" <<"\n";
226 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
227 std::string Name = Mang->getValueName(I);
228 if (Name.compare("abort") == 0)
229 continue;
230 if (I->isDeclaration()) {
231 O << "\textern " <<Name << "\n";
232 O << "\textern " << Name << ".retval\n";
233 O << "\textern " << Name << ".args\n";
234 }
235 else if (I->hasExternalLinkage()) {
236 O << "\tglobal " << Name << "\n";
237 O << "\tglobal " << Name << ".retval\n";
238 O << "\tglobal " << Name << ".args\n";
239 }
240 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000241
242 // Emit header file to include declaration of library functions
243 O << "\t#include C16IntrinsicCalls.INC\n";
244
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000245 // Emit declarations for external globals.
246 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
247 I != E; I++) {
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000248 // Any variables reaching here with ".auto." in its name is a local scope
249 // variable and should not be printed in global data section.
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000250 std::string Name = Mang->getValueName(I);
Sanjiv Guptad0765702009-03-12 02:10:45 +0000251 if (isLocalName (Name))
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000252 continue;
253
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000254 if (I->isDeclaration())
255 O << "\textern "<< Name << "\n";
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000256 else if (I->hasCommonLinkage() || I->hasExternalLinkage())
Sanjiv Guptaa2d8b062009-02-06 18:24:59 +0000257 O << "\tglobal "<< Name << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000258 }
259}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000260
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000261void PIC16AsmPrinter::EmitInitData (Module &M) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000262 SwitchToSection(TAI->getDataSection());
Sanjiv Guptad0765702009-03-12 02:10:45 +0000263 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000264 I != E; ++I) {
265 if (!I->hasInitializer()) // External global require no code.
266 continue;
267
268 Constant *C = I->getInitializer();
269 const PointerType *PtrTy = I->getType();
270 int AddrSpace = PtrTy->getAddressSpace();
271
272 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::RAM_SPACE)) {
273
274 if (EmitSpecialLLVMGlobal(I))
275 continue;
276
277 // Any variables reaching here with "." in its name is a local scope
278 // variable and should not be printed in global data section.
Sanjiv Guptad0765702009-03-12 02:10:45 +0000279 std::string Name = Mang->getValueName(I);
280 if (isLocalName(Name))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000281 continue;
282
Sanjiv Guptad0765702009-03-12 02:10:45 +0000283 I->setSection(TAI->getDataSection()->getName());
284 O << Name;
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000285 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000286 }
287 }
288}
289
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000290void PIC16AsmPrinter::EmitRomData (Module &M)
291{
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000292 SwitchToSection(TAI->getReadOnlySection());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000293 IsRomData = true;
Sanjiv Guptad0765702009-03-12 02:10:45 +0000294 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000295 I != E; ++I) {
296 if (!I->hasInitializer()) // External global require no code.
297 continue;
298
299 Constant *C = I->getInitializer();
300 const PointerType *PtrTy = I->getType();
301 int AddrSpace = PtrTy->getAddressSpace();
302 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::ROM_SPACE)) {
303
304 if (EmitSpecialLLVMGlobal(I))
305 continue;
306
307 // Any variables reaching here with "." in its name is a local scope
308 // variable and should not be printed in global data section.
309 std::string name = Mang->getValueName(I);
310 if (name.find(".") != std::string::npos)
311 continue;
312
Sanjiv Guptad0765702009-03-12 02:10:45 +0000313 I->setSection(TAI->getReadOnlySection()->getName());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000314 O << name;
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000315 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000316 O << "\n";
317 }
318 }
319 IsRomData = false;
320}
321
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000322void PIC16AsmPrinter::EmitUnInitData (Module &M)
323{
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000324 SwitchToSection(TAI->getBSSSection_());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000325 const TargetData *TD = TM.getTargetData();
326
Sanjiv Guptad0765702009-03-12 02:10:45 +0000327 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000328 I != E; ++I) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000329 if (!I->hasInitializer()) // External global require no code.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000330 continue;
331
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000332 Constant *C = I->getInitializer();
333 if (C->isNullValue()) {
334
335 if (EmitSpecialLLVMGlobal(I))
336 continue;
337
338 // Any variables reaching here with "." in its name is a local scope
339 // variable and should not be printed in global data section.
340 std::string name = Mang->getValueName(I);
341 if (name.find(".") != std::string::npos)
342 continue;
343
Sanjiv Guptad0765702009-03-12 02:10:45 +0000344 I->setSection(TAI->getBSSSection_()->getName());
345
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000346 const Type *Ty = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000347 unsigned Size = TD->getTypePaddedSize(Ty);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000348
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000349 O << name << " " <<"RES"<< " " << Size ;
350 O << "\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000351 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000352 }
353}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000354
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000355bool PIC16AsmPrinter::doFinalization(Module &M) {
356 O << "\t" << "END\n";
357 bool Result = AsmPrinter::doFinalization(M);
358 return Result;
359}
360
361void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
362 const Function *F = MF.getFunction();
363 std::string FuncName = Mang->getValueName(F);
Sanjiv Guptad0765702009-03-12 02:10:45 +0000364 Module *M = const_cast<Module *>(F->getParent());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000365 const TargetData *TD = TM.getTargetData();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000366 unsigned FrameSize = 0;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000367 // Emit the data section name.
368 O << "\n";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000369 std::string SectionName = "fdata." + CurrentFnName + ".# " + "UDATA";
370
371 const Section *fDataSection = TAI->getNamedSection(SectionName.c_str(),
372 SectionFlags::Writeable);
373 SwitchToSection(fDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000374
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000375
376 // Emit function frame label
377 O << CurrentFnName << ".frame:\n";
378
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000379 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000380 unsigned RetSize = 0;
381 if (RetType->getTypeID() != Type::VoidTyID)
382 RetSize = TD->getTypePaddedSize(RetType);
383
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000384 //Emit function return value space
385 if(RetSize > 0)
386 O << CurrentFnName << ".retval RES " << RetSize << "\n";
387 else
388 O << CurrentFnName << ".retval:\n";
389
390 // Emit variable to hold the space for function arguments
391 unsigned ArgSize = 0;
392 for (Function::const_arg_iterator argi = F->arg_begin(),
393 arge = F->arg_end(); argi != arge ; ++argi) {
394 const Type *Ty = argi->getType();
395 ArgSize += TD->getTypePaddedSize(Ty);
396 }
397 O << CurrentFnName << ".args RES " << ArgSize << "\n";
398
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000399 // Emit the function variables.
400
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000401 // In PIC16 all the function arguments and local variables are global.
402 // Therefore to get the variable belonging to this function entire
403 // global list will be traversed and variables belonging to this function
404 // will be emitted in the current data section.
Sanjiv Guptad0765702009-03-12 02:10:45 +0000405 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000406 I != E; ++I) {
407 std::string VarName = Mang->getValueName(I);
408
409 // The variables of a function are of form FuncName.* . If this variable
410 // does not belong to this function then continue.
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000411 // Static local varilabes of a function does not have .auto. in their
412 // name. They are not printed as part of function data but module
413 // level global data.
Sanjiv Guptad0765702009-03-12 02:10:45 +0000414 if (! isLocalToFunc(FuncName, VarName))
415 continue;
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000416
Sanjiv Guptad0765702009-03-12 02:10:45 +0000417 I->setSection("fdata." + CurrentFnName + ".#");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000418 Constant *C = I->getInitializer();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000419 const Type *Ty = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000420 unsigned Size = TD->getTypePaddedSize(Ty);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000421 FrameSize += Size;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000422 // Emit memory reserve directive.
423 O << VarName << " RES " << Size << "\n";
424 }
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000425
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000426 int TempSize = PTLI->GetTmpSize();
427 if (TempSize > 0 )
428 O << CurrentFnName << ".tmp RES " << TempSize <<"\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000429}