blob: 8e80eeabe2d706a043387bd70b5dc0cc9470050c [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 Gupta0e687712008-05-13 09:02:57 +000024
25using namespace llvm;
26
Sanjiv Gupta0e687712008-05-13 09:02:57 +000027#include "PIC16GenAsmWriter.inc"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000028bool PIC16AsmPrinter::inSameBank (char *s1, char *s2){
29
30 assert (s1 && s2 && "Null pointer assignment");
31
32 if ((*s1 == '.') && (*s2 == '.')) { //skip if they both start with '.'
33 s1++;
34 s2++;
35 }
36 while (*s1 && *s2) {
37 if (*s1 != *s2)
38 goto _NotInSameBank;
39
40 if ((*s1 == '.') && (*s2 == '.')) //both symbols in same function
41 goto _InSameBank; //in the same bank
42
43 s1++;
44 s2++;
45 }
46
47 if (*s1 && *s1) {
48 _InSameBank:
49 return true;
50 }
51
52 _NotInSameBank:
53 return false;
54}
Sanjiv Gupta0e687712008-05-13 09:02:57 +000055
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000056bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
57 std::string NewBankselLabel;
58 unsigned Operands = MI->getNumOperands();
59 if (Operands > 1) {
60 // Global address or external symbol should be second operand from last
61 // if we want to print banksel for it.
62 const MachineOperand &Op = MI->getOperand(Operands-2);
63 unsigned OpType = Op.getType();
64 if (OpType == MachineOperand::MO_GlobalAddress ||
65 OpType == MachineOperand::MO_ExternalSymbol) {
66 if (OpType == MachineOperand::MO_GlobalAddress )
67 NewBankselLabel = Mang->getValueName(Op.getGlobal());
68 else
69 NewBankselLabel = Op.getSymbolName();
70
71 // Operand after global address or external symbol should be banksel.
72 // Value 1 for this operand means we need to generate banksel else do not
73 // generate banksel.
74 const MachineOperand &BS = MI->getOperand(Operands-1);
75 if (((int)BS.getImm() == 1) &&
Sanjiv Gupta1b046942009-01-13 19:18:47 +000076 (!inSameBank ((char *)CurrentBankselLabelInBasicBlock.c_str(),
77 (char *)NewBankselLabel.c_str()))) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000078 CurrentBankselLabelInBasicBlock = NewBankselLabel;
79 O << "\tbanksel ";
80 printOperand(MI, Operands-2);
81 O << "\n";
82 }
83 }
84 }
85 printInstruction(MI);
86 return true;
87}
88
89/// runOnMachineFunction - This uses the printInstruction()
90/// method to print assembly for each instruction.
91///
92bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
93 // This calls the base class function required to be called at beginning
94 // of runOnMachineFunction.
95 SetupMachineFunction(MF);
96
97 // Get the mangled name.
98 const Function *F = MF.getFunction();
99 CurrentFnName = Mang->getValueName(F);
100
101 // Emit the function variables.
102 emitFunctionData(MF);
103 std::string codeSection;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000104 codeSection = "code." + CurrentFnName + ".# " + "CODE";
105 const Section *fCodeSection = TAI->getNamedSection(codeSection.c_str(),
106 SectionFlags::Code);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000107 O << "\n";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000108 SwitchToSection (fCodeSection);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000109
110 // Print out code for the function.
111 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
112 I != E; ++I) {
113 // Print a label for the basic block.
114 if (I != MF.begin()) {
115 printBasicBlockLabel(I, true);
116 O << '\n';
117 }
118 else
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000119 O << CurrentFnName << ":\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000120 CurrentBankselLabelInBasicBlock = "";
121 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
122 II != E; ++II) {
123 // Print the assembly for the instruction.
124 printMachineInstruction(II);
125 }
126 }
127 return false; // we didn't modify anything.
128}
129
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000130/// createPIC16CodePrinterPass - Returns a pass that prints the PIC16
131/// assembly code for a MachineFunction to the given output stream,
132/// using the given target machine description. This should work
133/// regardless of whether the function is in SSA form.
134///
Owen Andersoncb371882008-08-21 00:14:44 +0000135FunctionPass *llvm::createPIC16CodePrinterPass(raw_ostream &o,
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000136 PIC16TargetMachine &tm) {
137 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo());
138}
139
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000140void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000141 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000142
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000143 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000144 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000145 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000146 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000147 else
148 assert(0 && "not implemented");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000149 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000150
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000151 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000152 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000153 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000154
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000155 case MachineOperand::MO_GlobalAddress:
156 O << Mang->getValueName(MO.getGlobal());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000157 break;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000158
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000159 case MachineOperand::MO_ExternalSymbol:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000160 O << MO.getSymbolName();
161 break;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000162
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000163 case MachineOperand::MO_MachineBasicBlock:
164 printBasicBlockLabel(MO.getMBB());
165 return;
166
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000167 default:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000168 assert(0 && " Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000169 }
170}
171
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000172void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
173 int CC = (int)MI->getOperand(opNum).getImm();
174 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
175}
176
177
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000178bool PIC16AsmPrinter::doInitialization (Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000179 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000180 // FIXME:: This is temporary solution to generate the include file.
181 // The processor should be passed to llc as in input and the header file
182 // should be generated accordingly.
183 O << "\t#include P16F1937.INC\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000184 EmitExternsAndGlobals (M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000185 EmitInitData (M);
186 EmitUnInitData(M);
187 EmitRomData(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000188 return Result;
189}
190
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000191void PIC16AsmPrinter::EmitExternsAndGlobals (Module &M) {
192 // Emit declarations for external functions.
193 O << "section.0" <<"\n";
194 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
195 std::string Name = Mang->getValueName(I);
196 if (Name.compare("abort") == 0)
197 continue;
198 if (I->isDeclaration()) {
199 O << "\textern " <<Name << "\n";
200 O << "\textern " << Name << ".retval\n";
201 O << "\textern " << Name << ".args\n";
202 }
203 else if (I->hasExternalLinkage()) {
204 O << "\tglobal " << Name << "\n";
205 O << "\tglobal " << Name << ".retval\n";
206 O << "\tglobal " << Name << ".args\n";
207 }
208 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000209
210 // Emit header file to include declaration of library functions
211 O << "\t#include C16IntrinsicCalls.INC\n";
212
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000213 // Emit declarations for external globals.
214 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
215 I != E; I++) {
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000216 // Any variables reaching here with ".auto." in its name is a local scope
217 // variable and should not be printed in global data section.
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000218 std::string Name = Mang->getValueName(I);
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000219 if (Name.find(".auto.") != std::string::npos)
220 continue;
221
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000222 if (I->isDeclaration())
223 O << "\textern "<< Name << "\n";
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000224 else if (I->hasCommonLinkage() || I->hasExternalLinkage())
Sanjiv Guptaa2d8b062009-02-06 18:24:59 +0000225 O << "\tglobal "<< Name << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000226 }
227}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000228
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000229void PIC16AsmPrinter::EmitInitData (Module &M) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000230 SwitchToSection(TAI->getDataSection());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000231 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
232 I != E; ++I) {
233 if (!I->hasInitializer()) // External global require no code.
234 continue;
235
236 Constant *C = I->getInitializer();
237 const PointerType *PtrTy = I->getType();
238 int AddrSpace = PtrTy->getAddressSpace();
239
240 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::RAM_SPACE)) {
241
242 if (EmitSpecialLLVMGlobal(I))
243 continue;
244
245 // Any variables reaching here with "." in its name is a local scope
246 // variable and should not be printed in global data section.
247 std::string name = Mang->getValueName(I);
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000248 if (name.find(".auto.") != std::string::npos)
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000249 continue;
250
251 O << name;
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000252 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000253 }
254 }
255}
256
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000257void PIC16AsmPrinter::EmitRomData (Module &M)
258{
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000259 SwitchToSection(TAI->getReadOnlySection());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000260 IsRomData = true;
261 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
262 I != E; ++I) {
263 if (!I->hasInitializer()) // External global require no code.
264 continue;
265
266 Constant *C = I->getInitializer();
267 const PointerType *PtrTy = I->getType();
268 int AddrSpace = PtrTy->getAddressSpace();
269 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::ROM_SPACE)) {
270
271 if (EmitSpecialLLVMGlobal(I))
272 continue;
273
274 // Any variables reaching here with "." in its name is a local scope
275 // variable and should not be printed in global data section.
276 std::string name = Mang->getValueName(I);
277 if (name.find(".") != std::string::npos)
278 continue;
279
280 O << name;
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000281 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000282 O << "\n";
283 }
284 }
285 IsRomData = false;
286}
287
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000288void PIC16AsmPrinter::EmitUnInitData (Module &M)
289{
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000290 SwitchToSection(TAI->getBSSSection_());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000291 const TargetData *TD = TM.getTargetData();
292
293 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
294 I != E; ++I) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000295 if (!I->hasInitializer()) // External global require no code.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000296 continue;
297
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000298 Constant *C = I->getInitializer();
299 if (C->isNullValue()) {
300
301 if (EmitSpecialLLVMGlobal(I))
302 continue;
303
304 // Any variables reaching here with "." in its name is a local scope
305 // variable and should not be printed in global data section.
306 std::string name = Mang->getValueName(I);
307 if (name.find(".") != std::string::npos)
308 continue;
309
310 const Type *Ty = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000311 unsigned Size = TD->getTypePaddedSize(Ty);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000312
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000313 O << name << " " <<"RES"<< " " << Size ;
314 O << "\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000315 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000316 }
317}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000318
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000319bool PIC16AsmPrinter::doFinalization(Module &M) {
320 O << "\t" << "END\n";
321 bool Result = AsmPrinter::doFinalization(M);
322 return Result;
323}
324
325void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
326 const Function *F = MF.getFunction();
327 std::string FuncName = Mang->getValueName(F);
328 const Module *M = F->getParent();
329 const TargetData *TD = TM.getTargetData();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000330 unsigned FrameSize = 0;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000331 // Emit the data section name.
332 O << "\n";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000333 std::string SectionName = "fdata." + CurrentFnName + ".# " + "UDATA";
334
335 const Section *fDataSection = TAI->getNamedSection(SectionName.c_str(),
336 SectionFlags::Writeable);
337 SwitchToSection(fDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000338
339 //Emit function return value.
340 O << CurrentFnName << ".retval:\n";
341 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000342 unsigned RetSize = 0;
343 if (RetType->getTypeID() != Type::VoidTyID)
344 RetSize = TD->getTypePaddedSize(RetType);
345
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000346 // Emit function arguments.
347 O << CurrentFnName << ".args:\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000348 // Emit the function variables.
349
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000350 // In PIC16 all the function arguments and local variables are global.
351 // Therefore to get the variable belonging to this function entire
352 // global list will be traversed and variables belonging to this function
353 // will be emitted in the current data section.
354 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
355 I != E; ++I) {
356 std::string VarName = Mang->getValueName(I);
357
358 // The variables of a function are of form FuncName.* . If this variable
359 // does not belong to this function then continue.
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000360 // Static local varilabes of a function does not have .auto. in their
361 // name. They are not printed as part of function data but module
362 // level global data.
363 if (!(VarName.find(FuncName + ".auto.") == 0 ? true : false))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000364 continue;
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000365
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000366 Constant *C = I->getInitializer();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000367 const Type *Ty = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000368 unsigned Size = TD->getTypePaddedSize(Ty);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000369 FrameSize += Size;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000370 // Emit memory reserve directive.
371 O << VarName << " RES " << Size << "\n";
372 }
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000373
374 // Return value can not overlap with temp data, becasue a temp slot
375 // may be read/written after a return value is calculated and saved
376 // within the function.
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000377 if (RetSize > FrameSize)
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000378 O << CurrentFnName << ".dummy" << " RES " << (RetSize - FrameSize) << "\n";
379
380 emitFunctionTempData(MF, FrameSize);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000381}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000382
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000383void PIC16AsmPrinter::emitFunctionTempData(MachineFunction &MF,
384 unsigned &FrameSize) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000385 // Emit temporary variables.
386 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
387 if (FrameInfo->hasStackObjects()) {
388 int indexBegin = FrameInfo->getObjectIndexBegin();
389 int indexEnd = FrameInfo->getObjectIndexEnd();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000390
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000391 if (indexBegin < indexEnd) {
392 FrameSize += indexEnd - indexBegin;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000393 O << CurrentFnName << ".tmp RES"<< " "
394 <<indexEnd - indexBegin <<"\n";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000395 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000396 /*
397 while (indexBegin < indexEnd) {
398 O << CurrentFnName << "_tmp_" << indexBegin << " " << "RES"<< " "
399 << 1 << "\n" ;
400 indexBegin++;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000401 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000402 */
403 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000404}