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/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/Module.h"
20 #include "llvm/CodeGen/DwarfWriter.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Support/Mangler.h"
24 #include "llvm/CodeGen/DwarfWriter.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "PIC16GenAsmWriter.inc"
31 inline 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)
39 inline static bool isLocalName (std::string &Name) {
40 if (Name.find(".auto.") != std::string::npos
41 || Name.find(".arg.") != std::string::npos)
47 bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
48 std::string NewBank = "";
49 unsigned Operands = MI->getNumOperands();
51 // Global address or external symbol should be second operand from last
52 // if we want to print banksel for it.
53 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))
61 const MachineOperand &Op = MI->getOperand(BankSelVar);
62 unsigned OpType = Op.getType();
63 if (OpType == MachineOperand::MO_GlobalAddress ||
64 OpType == MachineOperand::MO_ExternalSymbol) {
65 if (OpType == MachineOperand::MO_GlobalAddress )
66 NewBank = Op.getGlobal()->getSection();
68 // External Symbol is generated for temp data. Temp data in in
69 // fdata.<functionname>.# section.
70 NewBank = "fdata." + CurrentFnName +".#";
72 // 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
75 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.
80 if ((BS.getType() == MachineOperand::MO_Immediate
81 && (int)BS.getImm() == 1)
82 && ((Op.isGlobal() && Op.getGlobal()->hasExternalLinkage()) ||
83 (NewBank.compare(CurBank) != 0))) {
85 printOperand(MI, BankSelVar);
95 /// runOnMachineFunction - This uses the printInstruction()
96 /// method to print assembly for each instruction.
98 bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
101 // This calls the base class function required to be called at beginning
102 // of runOnMachineFunction.
103 SetupMachineFunction(MF);
105 // Get the mangled name.
106 const Function *F = MF.getFunction();
107 CurrentFnName = Mang->getValueName(F);
109 // Emit the function variables.
110 emitFunctionData(MF);
111 std::string codeSection;
112 codeSection = "code." + CurrentFnName + ".# " + "CODE";
113 const Section *fCodeSection = TAI->getNamedSection(codeSection.c_str(),
116 SwitchToSection (fCodeSection);
118 // Emit the frame address of the function at the beginning of code.
119 O << CurrentFnName << ":\n";
120 O << " retlw low(" << CurrentFnName << ".frame)\n";
121 O << " retlw high(" << CurrentFnName << ".frame)\n";
124 // Print out code for the function.
125 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
127 // Print a label for the basic block.
128 if (I != MF.begin()) {
129 printBasicBlockLabel(I, true);
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;
137 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
139 // 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";
148 // Print the assembly for the instruction.
149 printMachineInstruction(II);
152 return false; // we didn't modify anything.
155 /// 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.
160 FunctionPass *llvm::createPIC16CodePrinterPass(raw_ostream &o,
161 PIC16TargetMachine &tm,
162 bool fast, bool verbose) {
163 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo(), fast, verbose);
166 void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
167 const MachineOperand &MO = MI->getOperand(opNum);
169 switch (MO.getType()) {
170 case MachineOperand::MO_Register:
171 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
172 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
174 assert(0 && "not implemented");
177 case MachineOperand::MO_Immediate:
178 O << (int)MO.getImm();
181 case MachineOperand::MO_GlobalAddress:
182 O << Mang->getValueName(MO.getGlobal());
185 case MachineOperand::MO_ExternalSymbol:
186 O << MO.getSymbolName();
189 case MachineOperand::MO_MachineBasicBlock:
190 printBasicBlockLabel(MO.getMBB());
194 assert(0 && " Operand type not supported.");
198 void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
199 int CC = (int)MI->getOperand(opNum).getImm();
200 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
204 bool PIC16AsmPrinter::doInitialization (Module &M) {
205 bool Result = AsmPrinter::doInitialization(M);
206 // 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";
210 MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
212 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
213 assert(DW && "Dwarf Writer is not available");
214 DW->BeginModule(&M, MMI, O, this, TAI);
216 EmitExternsAndGlobals (M);
223 void 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)
231 // If it is llvm intrinsic call then don't emit
232 if (Name.find("llvm.") != std::string::npos)
235 if (I->isDeclaration()) {
236 O << "\textern " <<Name << "\n";
237 O << "\textern " << Name << ".retval\n";
238 O << "\textern " << Name << ".args\n";
240 else if (I->hasExternalLinkage()) {
241 O << "\tglobal " << Name << "\n";
242 O << "\tglobal " << Name << ".retval\n";
243 O << "\tglobal " << Name << ".args\n";
247 // Emit header file to include declaration of library functions
248 O << "\t#include C16IntrinsicCalls.INC\n";
250 // Emit declarations for external globals.
251 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
253 // Any variables reaching here with ".auto." in its name is a local scope
254 // variable and should not be printed in global data section.
255 std::string Name = Mang->getValueName(I);
256 if (isLocalName (Name))
259 if (I->isDeclaration())
260 O << "\textern "<< Name << "\n";
261 else if (I->hasCommonLinkage() || I->hasExternalLinkage())
262 O << "\tglobal "<< Name << "\n";
266 void PIC16AsmPrinter::EmitInitData (Module &M) {
267 SwitchToSection(TAI->getDataSection());
268 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
270 if (!I->hasInitializer()) // External global require no code.
273 Constant *C = I->getInitializer();
274 const PointerType *PtrTy = I->getType();
275 int AddrSpace = PtrTy->getAddressSpace();
277 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::RAM_SPACE)) {
279 if (EmitSpecialLLVMGlobal(I))
282 // Any variables reaching here with "." in its name is a local scope
283 // variable and should not be printed in global data section.
284 std::string Name = Mang->getValueName(I);
285 if (isLocalName(Name))
288 I->setSection(TAI->getDataSection()->getName());
290 EmitGlobalConstant(C, AddrSpace);
295 void PIC16AsmPrinter::EmitRomData (Module &M)
297 SwitchToSection(TAI->getReadOnlySection());
299 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
301 if (!I->hasInitializer()) // External global require no code.
304 Constant *C = I->getInitializer();
305 const PointerType *PtrTy = I->getType();
306 int AddrSpace = PtrTy->getAddressSpace();
307 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::ROM_SPACE)) {
309 if (EmitSpecialLLVMGlobal(I))
312 // Any variables reaching here with "." in its name is a local scope
313 // variable and should not be printed in global data section.
314 std::string name = Mang->getValueName(I);
315 if (name.find(".") != std::string::npos)
318 I->setSection(TAI->getReadOnlySection()->getName());
320 EmitGlobalConstant(C, AddrSpace);
327 void PIC16AsmPrinter::EmitUnInitData (Module &M)
329 SwitchToSection(TAI->getBSSSection_());
330 const TargetData *TD = TM.getTargetData();
332 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
334 if (!I->hasInitializer()) // External global require no code.
337 Constant *C = I->getInitializer();
338 if (C->isNullValue()) {
340 if (EmitSpecialLLVMGlobal(I))
343 // Any variables reaching here with "." in its name is a local scope
344 // variable and should not be printed in global data section.
345 std::string name = Mang->getValueName(I);
346 if (name.find(".") != std::string::npos)
349 I->setSection(TAI->getBSSSection_()->getName());
351 const Type *Ty = C->getType();
352 unsigned Size = TD->getTypePaddedSize(Ty);
354 O << name << " " <<"RES"<< " " << Size ;
360 bool PIC16AsmPrinter::doFinalization(Module &M) {
361 O << "\t" << "END\n";
362 bool Result = AsmPrinter::doFinalization(M);
366 void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
367 const Function *F = MF.getFunction();
368 std::string FuncName = Mang->getValueName(F);
369 Module *M = const_cast<Module *>(F->getParent());
370 const TargetData *TD = TM.getTargetData();
371 unsigned FrameSize = 0;
372 // Emit the data section name.
374 std::string SectionName = "fdata." + CurrentFnName + ".# " + "UDATA";
376 const Section *fDataSection = TAI->getNamedSection(SectionName.c_str(),
377 SectionFlags::Writeable);
378 SwitchToSection(fDataSection);
381 // Emit function frame label
382 O << CurrentFnName << ".frame:\n";
384 const Type *RetType = F->getReturnType();
385 unsigned RetSize = 0;
386 if (RetType->getTypeID() != Type::VoidTyID)
387 RetSize = TD->getTypePaddedSize(RetType);
389 //Emit function return value space
391 O << CurrentFnName << ".retval RES " << RetSize << "\n";
393 O << CurrentFnName << ".retval:\n";
395 // Emit variable to hold the space for function arguments
396 unsigned ArgSize = 0;
397 for (Function::const_arg_iterator argi = F->arg_begin(),
398 arge = F->arg_end(); argi != arge ; ++argi) {
399 const Type *Ty = argi->getType();
400 ArgSize += TD->getTypePaddedSize(Ty);
402 O << CurrentFnName << ".args RES " << ArgSize << "\n";
404 // Emit temporary space
405 int TempSize = PTLI->GetTmpSize();
407 O << CurrentFnName << ".tmp RES " << TempSize <<"\n";
409 // Emit the function variables.
411 // In PIC16 all the function arguments and local variables are global.
412 // Therefore to get the variable belonging to this function entire
413 // global list will be traversed and variables belonging to this function
414 // will be emitted in the current data section.
415 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
417 std::string VarName = Mang->getValueName(I);
419 // The variables of a function are of form FuncName.* . If this variable
420 // does not belong to this function then continue.
421 // Static local varilabes of a function does not have .auto. in their
422 // name. They are not printed as part of function data but module
423 // level global data.
424 if (! isLocalToFunc(FuncName, VarName))
427 I->setSection("fdata." + CurrentFnName + ".#");
428 Constant *C = I->getInitializer();
429 const Type *Ty = C->getType();
430 unsigned Size = TD->getTypePaddedSize(Ty);
432 // Emit memory reserve directive.
433 O << VarName << " RES " << Size << "\n";