blob: 1f3c4345646a4558949a7586d651d49f9275a547 [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"
Owen Andersoncb371882008-08-21 00:14:44 +000017#include "llvm/Support/raw_ostream.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000018#include "llvm/Support/Mangler.h"
19#include "llvm/Function.h"
20#include "llvm/Module.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/DerivedTypes.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000023
24using namespace llvm;
25
Sanjiv Gupta0e687712008-05-13 09:02:57 +000026#include "PIC16GenAsmWriter.inc"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000027bool PIC16AsmPrinter::inSameBank (char *s1, char *s2){
28
29 assert (s1 && s2 && "Null pointer assignment");
30
31 if ((*s1 == '.') && (*s2 == '.')) { //skip if they both start with '.'
32 s1++;
33 s2++;
34 }
35 while (*s1 && *s2) {
36 if (*s1 != *s2)
37 goto _NotInSameBank;
38
39 if ((*s1 == '.') && (*s2 == '.')) //both symbols in same function
40 goto _InSameBank; //in the same bank
41
42 s1++;
43 s2++;
44 }
45
46 if (*s1 && *s1) {
47 _InSameBank:
48 return true;
49 }
50
51 _NotInSameBank:
52 return false;
53}
Sanjiv Gupta0e687712008-05-13 09:02:57 +000054
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000055bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
56 std::string NewBankselLabel;
57 unsigned Operands = MI->getNumOperands();
58 if (Operands > 1) {
59 // Global address or external symbol should be second operand from last
60 // if we want to print banksel for it.
61 const MachineOperand &Op = MI->getOperand(Operands-2);
62 unsigned OpType = Op.getType();
63 if (OpType == MachineOperand::MO_GlobalAddress ||
64 OpType == MachineOperand::MO_ExternalSymbol) {
65 if (OpType == MachineOperand::MO_GlobalAddress )
66 NewBankselLabel = Mang->getValueName(Op.getGlobal());
67 else
68 NewBankselLabel = Op.getSymbolName();
69
70 // Operand after global address or external symbol should be banksel.
71 // Value 1 for this operand means we need to generate banksel else do not
72 // generate banksel.
73 const MachineOperand &BS = MI->getOperand(Operands-1);
74 if (((int)BS.getImm() == 1) &&
Sanjiv Gupta1b046942009-01-13 19:18:47 +000075 (!inSameBank ((char *)CurrentBankselLabelInBasicBlock.c_str(),
76 (char *)NewBankselLabel.c_str()))) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000077 CurrentBankselLabelInBasicBlock = NewBankselLabel;
78 O << "\tbanksel ";
79 printOperand(MI, Operands-2);
80 O << "\n";
81 }
82 }
83 }
84 printInstruction(MI);
85 return true;
86}
87
88/// runOnMachineFunction - This uses the printInstruction()
89/// method to print assembly for each instruction.
90///
91bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
92 // This calls the base class function required to be called at beginning
93 // of runOnMachineFunction.
94 SetupMachineFunction(MF);
95
96 // Get the mangled name.
97 const Function *F = MF.getFunction();
98 CurrentFnName = Mang->getValueName(F);
99
100 // Emit the function variables.
101 emitFunctionData(MF);
102 std::string codeSection;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000103 codeSection = "code." + CurrentFnName + ".# " + "CODE";
104 const Section *fCodeSection = TAI->getNamedSection(codeSection.c_str(),
105 SectionFlags::Code);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000106 O << "\n";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000107 SwitchToSection (fCodeSection);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000108
109 // Print out code for the function.
110 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
111 I != E; ++I) {
112 // Print a label for the basic block.
113 if (I != MF.begin()) {
114 printBasicBlockLabel(I, true);
115 O << '\n';
116 }
117 else
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000118 O << CurrentFnName << ":\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000119 CurrentBankselLabelInBasicBlock = "";
120 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
121 II != E; ++II) {
122 // Print the assembly for the instruction.
123 printMachineInstruction(II);
124 }
125 }
126 return false; // we didn't modify anything.
127}
128
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000129/// createPIC16CodePrinterPass - Returns a pass that prints the PIC16
130/// assembly code for a MachineFunction to the given output stream,
131/// using the given target machine description. This should work
132/// regardless of whether the function is in SSA form.
133///
Owen Andersoncb371882008-08-21 00:14:44 +0000134FunctionPass *llvm::createPIC16CodePrinterPass(raw_ostream &o,
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000135 PIC16TargetMachine &tm) {
136 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo());
137}
138
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000139void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000140 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000141
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000142 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000143 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000144 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000145 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000146 else
147 assert(0 && "not implemented");
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000148 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000149
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000150 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000151 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000152 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000153
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000154 case MachineOperand::MO_GlobalAddress:
155 O << Mang->getValueName(MO.getGlobal());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000156 break;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000157
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000158 case MachineOperand::MO_ExternalSymbol:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000159 O << MO.getSymbolName();
160 break;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000161
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000162 case MachineOperand::MO_MachineBasicBlock:
163 printBasicBlockLabel(MO.getMBB());
164 return;
165
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000166 default:
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000167 assert(0 && " Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000168 }
169}
170
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000171void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
172 int CC = (int)MI->getOperand(opNum).getImm();
173 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
174}
175
176
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000177bool PIC16AsmPrinter::doInitialization (Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000178 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000179 // FIXME:: This is temporary solution to generate the include file.
180 // The processor should be passed to llc as in input and the header file
181 // should be generated accordingly.
182 O << "\t#include P16F1937.INC\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000183 EmitExternsAndGlobals (M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000184 EmitInitData (M);
185 EmitUnInitData(M);
186 EmitRomData(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000187 return Result;
188}
189
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000190void PIC16AsmPrinter::EmitExternsAndGlobals (Module &M) {
191 // Emit declarations for external functions.
192 O << "section.0" <<"\n";
193 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
194 std::string Name = Mang->getValueName(I);
195 if (Name.compare("abort") == 0)
196 continue;
197 if (I->isDeclaration()) {
198 O << "\textern " <<Name << "\n";
199 O << "\textern " << Name << ".retval\n";
200 O << "\textern " << Name << ".args\n";
201 }
202 else if (I->hasExternalLinkage()) {
203 O << "\tglobal " << Name << "\n";
204 O << "\tglobal " << Name << ".retval\n";
205 O << "\tglobal " << Name << ".args\n";
206 }
207 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000208
209 // Emit header file to include declaration of library functions
210 O << "\t#include C16IntrinsicCalls.INC\n";
211
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000212 // Emit declarations for external globals.
213 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
214 I != E; I++) {
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000215 // Any variables reaching here with ".auto." in its name is a local scope
216 // variable and should not be printed in global data section.
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000217 std::string Name = Mang->getValueName(I);
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000218 if (Name.find(".auto.") != std::string::npos)
219 continue;
220
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000221 if (I->isDeclaration())
222 O << "\textern "<< Name << "\n";
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000223 else if (I->hasCommonLinkage() || I->hasExternalLinkage())
Sanjiv Guptaa2d8b062009-02-06 18:24:59 +0000224 O << "\tglobal "<< Name << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000225 }
226}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000227
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000228void PIC16AsmPrinter::EmitInitData (Module &M) {
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000229 SwitchToSection(TAI->getDataSection());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000230 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
231 I != E; ++I) {
232 if (!I->hasInitializer()) // External global require no code.
233 continue;
234
235 Constant *C = I->getInitializer();
236 const PointerType *PtrTy = I->getType();
237 int AddrSpace = PtrTy->getAddressSpace();
238
239 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::RAM_SPACE)) {
240
241 if (EmitSpecialLLVMGlobal(I))
242 continue;
243
244 // Any variables reaching here with "." in its name is a local scope
245 // variable and should not be printed in global data section.
246 std::string name = Mang->getValueName(I);
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000247 if (name.find(".auto.") != std::string::npos)
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000248 continue;
249
250 O << name;
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000251 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000252 }
253 }
254}
255
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000256void PIC16AsmPrinter::EmitRomData (Module &M)
257{
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000258 SwitchToSection(TAI->getReadOnlySection());
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000259 IsRomData = true;
260 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
261 I != E; ++I) {
262 if (!I->hasInitializer()) // External global require no code.
263 continue;
264
265 Constant *C = I->getInitializer();
266 const PointerType *PtrTy = I->getType();
267 int AddrSpace = PtrTy->getAddressSpace();
268 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::ROM_SPACE)) {
269
270 if (EmitSpecialLLVMGlobal(I))
271 continue;
272
273 // Any variables reaching here with "." in its name is a local scope
274 // variable and should not be printed in global data section.
275 std::string name = Mang->getValueName(I);
276 if (name.find(".") != std::string::npos)
277 continue;
278
279 O << name;
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000280 EmitGlobalConstant(C, AddrSpace);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000281 O << "\n";
282 }
283 }
284 IsRomData = false;
285}
286
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000287void PIC16AsmPrinter::EmitUnInitData (Module &M)
288{
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000289 SwitchToSection(TAI->getBSSSection_());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000290 const TargetData *TD = TM.getTargetData();
291
292 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
293 I != E; ++I) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000294 if (!I->hasInitializer()) // External global require no code.
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000295 continue;
296
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000297 Constant *C = I->getInitializer();
298 if (C->isNullValue()) {
299
300 if (EmitSpecialLLVMGlobal(I))
301 continue;
302
303 // Any variables reaching here with "." in its name is a local scope
304 // variable and should not be printed in global data section.
305 std::string name = Mang->getValueName(I);
306 if (name.find(".") != std::string::npos)
307 continue;
308
309 const Type *Ty = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000310 unsigned Size = TD->getTypePaddedSize(Ty);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000311
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000312 O << name << " " <<"RES"<< " " << Size ;
313 O << "\n";
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000314 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000315 }
316}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000317
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000318bool PIC16AsmPrinter::doFinalization(Module &M) {
319 O << "\t" << "END\n";
320 bool Result = AsmPrinter::doFinalization(M);
321 return Result;
322}
323
324void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
325 const Function *F = MF.getFunction();
326 std::string FuncName = Mang->getValueName(F);
327 const Module *M = F->getParent();
328 const TargetData *TD = TM.getTargetData();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000329 unsigned FrameSize = 0;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000330 // Emit the data section name.
331 O << "\n";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000332 std::string SectionName = "fdata." + CurrentFnName + ".# " + "UDATA";
333
334 const Section *fDataSection = TAI->getNamedSection(SectionName.c_str(),
335 SectionFlags::Writeable);
336 SwitchToSection(fDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000337
338 //Emit function return value.
339 O << CurrentFnName << ".retval:\n";
340 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000341 unsigned RetSize = 0;
342 if (RetType->getTypeID() != Type::VoidTyID)
343 RetSize = TD->getTypePaddedSize(RetType);
344
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000345 // Emit function arguments.
346 O << CurrentFnName << ".args:\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000347 // Emit the function variables.
348
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000349 // In PIC16 all the function arguments and local variables are global.
350 // Therefore to get the variable belonging to this function entire
351 // global list will be traversed and variables belonging to this function
352 // will be emitted in the current data section.
353 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
354 I != E; ++I) {
355 std::string VarName = Mang->getValueName(I);
356
357 // The variables of a function are of form FuncName.* . If this variable
358 // does not belong to this function then continue.
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000359 // Static local varilabes of a function does not have .auto. in their
360 // name. They are not printed as part of function data but module
361 // level global data.
362 if (!(VarName.find(FuncName + ".auto.") == 0 ? true : false))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000363 continue;
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000364
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000365 Constant *C = I->getInitializer();
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000366 const Type *Ty = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000367 unsigned Size = TD->getTypePaddedSize(Ty);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000368 FrameSize += Size;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000369 // Emit memory reserve directive.
370 O << VarName << " RES " << Size << "\n";
371 }
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000372
373 // Return value can not overlap with temp data, becasue a temp slot
374 // may be read/written after a return value is calculated and saved
375 // within the function.
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000376 if (RetSize > FrameSize)
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000377 O << CurrentFnName << ".dummy" << " RES " << (RetSize - FrameSize) << "\n";
378
379 emitFunctionTempData(MF, FrameSize);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000380}
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000381
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000382void PIC16AsmPrinter::emitFunctionTempData(MachineFunction &MF,
383 unsigned &FrameSize) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000384 // Emit temporary variables.
385 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
386 if (FrameInfo->hasStackObjects()) {
387 int indexBegin = FrameInfo->getObjectIndexBegin();
388 int indexEnd = FrameInfo->getObjectIndexEnd();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000389
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000390 if (indexBegin < indexEnd) {
391 FrameSize += indexEnd - indexBegin;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000392 O << CurrentFnName << ".tmp RES"<< " "
393 <<indexEnd - indexBegin <<"\n";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000394 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000395 /*
396 while (indexBegin < indexEnd) {
397 O << CurrentFnName << "_tmp_" << indexBegin << " " << "RES"<< " "
398 << 1 << "\n" ;
399 indexBegin++;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000400 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000401 */
402 }
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000403}