1 //===-- PIC16AsmPrinter.cpp - PIC16 LLVM assembly writer ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to PIC16 assembly language.
13 //===----------------------------------------------------------------------===//
15 #include "PIC16AsmPrinter.h"
16 #include "PIC16TargetAsmInfo.h"
17 #include "llvm/Support/raw_ostream.h"
18 #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"
26 #include "PIC16GenAsmWriter.inc"
27 bool PIC16AsmPrinter::inSameBank (char *s1, char *s2){
29 assert (s1 && s2 && "Null pointer assignment");
31 if ((*s1 == '.') && (*s2 == '.')) { //skip if they both start with '.'
39 if ((*s1 == '.') && (*s2 == '.')) //both symbols in same function
40 goto _InSameBank; //in the same bank
55 bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
56 std::string NewBankselLabel;
57 unsigned Operands = MI->getNumOperands();
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());
68 NewBankselLabel = Op.getSymbolName();
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
73 const MachineOperand &BS = MI->getOperand(Operands-1);
74 if (((int)BS.getImm() == 1) &&
75 (!inSameBank ((char *)CurrentBankselLabelInBasicBlock.c_str(),
76 (char *)NewBankselLabel.c_str()))) {
77 CurrentBankselLabelInBasicBlock = NewBankselLabel;
79 printOperand(MI, Operands-2);
88 /// runOnMachineFunction - This uses the printInstruction()
89 /// method to print assembly for each instruction.
91 bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
92 // This calls the base class function required to be called at beginning
93 // of runOnMachineFunction.
94 SetupMachineFunction(MF);
96 // Get the mangled name.
97 const Function *F = MF.getFunction();
98 CurrentFnName = Mang->getValueName(F);
100 // Emit the function variables.
101 emitFunctionData(MF);
102 std::string codeSection;
103 codeSection = "code." + CurrentFnName + ".# " + "CODE";
104 const Section *fCodeSection = TAI->getNamedSection(codeSection.c_str(),
107 SwitchToSection (fCodeSection);
109 // Print out code for the function.
110 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
112 // Print a label for the basic block.
113 if (I != MF.begin()) {
114 printBasicBlockLabel(I, true);
118 O << CurrentFnName << ":\n";
119 CurrentBankselLabelInBasicBlock = "";
120 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
122 // Print the assembly for the instruction.
123 printMachineInstruction(II);
126 return false; // we didn't modify anything.
129 /// 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.
134 FunctionPass *llvm::createPIC16CodePrinterPass(raw_ostream &o,
135 PIC16TargetMachine &tm) {
136 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo());
139 void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
140 const MachineOperand &MO = MI->getOperand(opNum);
142 switch (MO.getType()) {
143 case MachineOperand::MO_Register:
144 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
145 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
147 assert(0 && "not implemented");
150 case MachineOperand::MO_Immediate:
151 O << (int)MO.getImm();
154 case MachineOperand::MO_GlobalAddress:
155 O << Mang->getValueName(MO.getGlobal());
158 case MachineOperand::MO_ExternalSymbol:
159 O << MO.getSymbolName();
162 case MachineOperand::MO_MachineBasicBlock:
163 printBasicBlockLabel(MO.getMBB());
167 assert(0 && " Operand type not supported.");
171 void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
172 int CC = (int)MI->getOperand(opNum).getImm();
173 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
177 bool PIC16AsmPrinter::doInitialization (Module &M) {
178 bool Result = AsmPrinter::doInitialization(M);
179 // 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";
183 EmitExternsAndGlobals (M);
190 void 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)
197 if (I->isDeclaration()) {
198 O << "\textern " <<Name << "\n";
199 O << "\textern " << Name << ".retval\n";
200 O << "\textern " << Name << ".args\n";
202 else if (I->hasExternalLinkage()) {
203 O << "\tglobal " << Name << "\n";
204 O << "\tglobal " << Name << ".retval\n";
205 O << "\tglobal " << Name << ".args\n";
209 // Emit header file to include declaration of library functions
210 O << "\t#include C16IntrinsicCalls.INC\n";
212 // Emit declarations for external globals.
213 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
215 std::string Name = Mang->getValueName(I);
216 if (I->isDeclaration())
217 O << "\textern "<< Name << "\n";
218 else if (I->getLinkage() == GlobalValue::CommonLinkage)
219 O << "\tglobal "<< Name << "\n";
220 else if (I->getLinkage() == GlobalValue::ExternalLinkage)
221 O << "\tglobal "<< Name << "\n";
224 void PIC16AsmPrinter::EmitInitData (Module &M) {
225 SwitchToSection(TAI->getDataSection());
226 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
228 if (!I->hasInitializer()) // External global require no code.
231 Constant *C = I->getInitializer();
232 const PointerType *PtrTy = I->getType();
233 int AddrSpace = PtrTy->getAddressSpace();
235 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::RAM_SPACE)) {
237 if (EmitSpecialLLVMGlobal(I))
240 // Any variables reaching here with "." in its name is a local scope
241 // variable and should not be printed in global data section.
242 std::string name = Mang->getValueName(I);
243 if (name.find(".") != std::string::npos)
247 EmitGlobalConstant(C, AddrSpace);
252 void PIC16AsmPrinter::EmitRomData (Module &M)
254 SwitchToSection(TAI->getReadOnlySection());
256 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
258 if (!I->hasInitializer()) // External global require no code.
261 Constant *C = I->getInitializer();
262 const PointerType *PtrTy = I->getType();
263 int AddrSpace = PtrTy->getAddressSpace();
264 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::ROM_SPACE)) {
266 if (EmitSpecialLLVMGlobal(I))
269 // Any variables reaching here with "." in its name is a local scope
270 // variable and should not be printed in global data section.
271 std::string name = Mang->getValueName(I);
272 if (name.find(".") != std::string::npos)
276 EmitGlobalConstant(C, AddrSpace);
283 void PIC16AsmPrinter::EmitUnInitData (Module &M)
285 SwitchToSection(TAI->getBSSSection_());
286 const TargetData *TD = TM.getTargetData();
288 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
290 if (!I->hasInitializer()) // External global require no code.
293 Constant *C = I->getInitializer();
294 if (C->isNullValue()) {
296 if (EmitSpecialLLVMGlobal(I))
299 // Any variables reaching here with "." in its name is a local scope
300 // variable and should not be printed in global data section.
301 std::string name = Mang->getValueName(I);
302 if (name.find(".") != std::string::npos)
305 const Type *Ty = C->getType();
306 unsigned Size = TD->getTypePaddedSize(Ty);
308 O << name << " " <<"RES"<< " " << Size ;
314 bool PIC16AsmPrinter::doFinalization(Module &M) {
315 O << "\t" << "END\n";
316 bool Result = AsmPrinter::doFinalization(M);
320 void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
321 const Function *F = MF.getFunction();
322 std::string FuncName = Mang->getValueName(F);
323 const Module *M = F->getParent();
324 const TargetData *TD = TM.getTargetData();
325 unsigned FrameSize = 0;
326 // Emit the data section name.
328 std::string SectionName = "fdata." + CurrentFnName + ".# " + "UDATA";
330 const Section *fDataSection = TAI->getNamedSection(SectionName.c_str(),
331 SectionFlags::Writeable);
332 SwitchToSection(fDataSection);
334 //Emit function return value.
335 O << CurrentFnName << ".retval:\n";
336 const Type *RetType = F->getReturnType();
337 unsigned RetSize = 0;
338 if (RetType->getTypeID() != Type::VoidTyID)
339 RetSize = TD->getTypePaddedSize(RetType);
341 // Emit function arguments.
342 O << CurrentFnName << ".args:\n";
343 // Emit the function variables.
345 // In PIC16 all the function arguments and local variables are global.
346 // Therefore to get the variable belonging to this function entire
347 // global list will be traversed and variables belonging to this function
348 // will be emitted in the current data section.
349 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
351 std::string VarName = Mang->getValueName(I);
353 // The variables of a function are of form FuncName.* . If this variable
354 // does not belong to this function then continue.
355 if (!(VarName.find(FuncName + ".") == 0 ? true : false))
358 Constant *C = I->getInitializer();
359 const Type *Ty = C->getType();
360 unsigned Size = TD->getTypePaddedSize(Ty);
362 // Emit memory reserve directive.
363 O << VarName << " RES " << Size << "\n";
365 emitFunctionTempData(MF, FrameSize);
366 if (RetSize > FrameSize)
367 O << CurrentFnName << ".dummy" << "RES" << (RetSize - FrameSize);
370 void PIC16AsmPrinter::emitFunctionTempData(MachineFunction &MF,
371 unsigned &FrameSize) {
372 // Emit temporary variables.
373 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
374 if (FrameInfo->hasStackObjects()) {
375 int indexBegin = FrameInfo->getObjectIndexBegin();
376 int indexEnd = FrameInfo->getObjectIndexEnd();
378 if (indexBegin < indexEnd) {
379 FrameSize += indexEnd - indexBegin;
380 O << CurrentFnName << ".tmp RES"<< " "
381 <<indexEnd - indexBegin <<"\n";
384 while (indexBegin < indexEnd) {
385 O << CurrentFnName << "_tmp_" << indexBegin << " " << "RES"<< " "