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 (((int)BS.getImm() == 1) &&
81 ((Op.isGlobal() && Op.getGlobal()->hasExternalLinkage()) ||
82 (NewBank.compare(CurBank) != 0))) {
84 printOperand(MI, BankSelVar);
94 /// runOnMachineFunction - This uses the printInstruction()
95 /// method to print assembly for each instruction.
97 bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
100 // This calls the base class function required to be called at beginning
101 // of runOnMachineFunction.
102 SetupMachineFunction(MF);
104 // Get the mangled name.
105 const Function *F = MF.getFunction();
106 CurrentFnName = Mang->getValueName(F);
108 // Emit the function variables.
109 emitFunctionData(MF);
110 std::string codeSection;
111 codeSection = "code." + CurrentFnName + ".# " + "CODE";
112 const Section *fCodeSection = TAI->getNamedSection(codeSection.c_str(),
115 SwitchToSection (fCodeSection);
117 // Print out code for the function.
118 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
120 // Print a label for the basic block.
121 if (I != MF.begin()) {
122 printBasicBlockLabel(I, true);
126 O << CurrentFnName << ":\n";
128 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
130 // Print the assembly for the instruction.
131 printMachineInstruction(II);
134 return false; // we didn't modify anything.
137 /// createPIC16CodePrinterPass - Returns a pass that prints the PIC16
138 /// assembly code for a MachineFunction to the given output stream,
139 /// using the given target machine description. This should work
140 /// regardless of whether the function is in SSA form.
142 FunctionPass *llvm::createPIC16CodePrinterPass(raw_ostream &o,
143 PIC16TargetMachine &tm,
144 bool fast, bool verbose) {
145 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo(), fast, verbose);
148 void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
149 const MachineOperand &MO = MI->getOperand(opNum);
151 switch (MO.getType()) {
152 case MachineOperand::MO_Register:
153 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
154 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
156 assert(0 && "not implemented");
159 case MachineOperand::MO_Immediate:
160 O << (int)MO.getImm();
163 case MachineOperand::MO_GlobalAddress:
164 O << Mang->getValueName(MO.getGlobal());
167 case MachineOperand::MO_ExternalSymbol:
168 O << MO.getSymbolName();
171 case MachineOperand::MO_MachineBasicBlock:
172 printBasicBlockLabel(MO.getMBB());
176 assert(0 && " Operand type not supported.");
180 void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
181 int CC = (int)MI->getOperand(opNum).getImm();
182 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
186 bool PIC16AsmPrinter::doInitialization (Module &M) {
187 bool Result = AsmPrinter::doInitialization(M);
188 // FIXME:: This is temporary solution to generate the include file.
189 // The processor should be passed to llc as in input and the header file
190 // should be generated accordingly.
191 O << "\t#include P16F1937.INC\n";
192 MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
194 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
195 assert(DW && "Dwarf Writer is not available");
196 DW->BeginModule(&M, MMI, O, this, TAI);
198 EmitExternsAndGlobals (M);
205 void PIC16AsmPrinter::EmitExternsAndGlobals (Module &M) {
206 // Emit declarations for external functions.
207 O << "section.0" <<"\n";
208 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
209 std::string Name = Mang->getValueName(I);
210 if (Name.compare("abort") == 0)
212 if (I->isDeclaration()) {
213 O << "\textern " <<Name << "\n";
214 O << "\textern " << Name << ".retval\n";
215 O << "\textern " << Name << ".args\n";
217 else if (I->hasExternalLinkage()) {
218 O << "\tglobal " << Name << "\n";
219 O << "\tglobal " << Name << ".retval\n";
220 O << "\tglobal " << Name << ".args\n";
224 // Emit header file to include declaration of library functions
225 O << "\t#include C16IntrinsicCalls.INC\n";
227 // Emit declarations for external globals.
228 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
230 // Any variables reaching here with ".auto." in its name is a local scope
231 // variable and should not be printed in global data section.
232 std::string Name = Mang->getValueName(I);
233 if (isLocalName (Name))
236 if (I->isDeclaration())
237 O << "\textern "<< Name << "\n";
238 else if (I->hasCommonLinkage() || I->hasExternalLinkage())
239 O << "\tglobal "<< Name << "\n";
243 void PIC16AsmPrinter::EmitInitData (Module &M) {
244 SwitchToSection(TAI->getDataSection());
245 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
247 if (!I->hasInitializer()) // External global require no code.
250 Constant *C = I->getInitializer();
251 const PointerType *PtrTy = I->getType();
252 int AddrSpace = PtrTy->getAddressSpace();
254 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::RAM_SPACE)) {
256 if (EmitSpecialLLVMGlobal(I))
259 // Any variables reaching here with "." in its name is a local scope
260 // variable and should not be printed in global data section.
261 std::string Name = Mang->getValueName(I);
262 if (isLocalName(Name))
265 I->setSection(TAI->getDataSection()->getName());
267 EmitGlobalConstant(C, AddrSpace);
272 void PIC16AsmPrinter::EmitRomData (Module &M)
274 SwitchToSection(TAI->getReadOnlySection());
276 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
278 if (!I->hasInitializer()) // External global require no code.
281 Constant *C = I->getInitializer();
282 const PointerType *PtrTy = I->getType();
283 int AddrSpace = PtrTy->getAddressSpace();
284 if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::ROM_SPACE)) {
286 if (EmitSpecialLLVMGlobal(I))
289 // Any variables reaching here with "." in its name is a local scope
290 // variable and should not be printed in global data section.
291 std::string name = Mang->getValueName(I);
292 if (name.find(".") != std::string::npos)
295 I->setSection(TAI->getReadOnlySection()->getName());
297 EmitGlobalConstant(C, AddrSpace);
304 void PIC16AsmPrinter::EmitUnInitData (Module &M)
306 SwitchToSection(TAI->getBSSSection_());
307 const TargetData *TD = TM.getTargetData();
309 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
311 if (!I->hasInitializer()) // External global require no code.
314 Constant *C = I->getInitializer();
315 if (C->isNullValue()) {
317 if (EmitSpecialLLVMGlobal(I))
320 // Any variables reaching here with "." in its name is a local scope
321 // variable and should not be printed in global data section.
322 std::string name = Mang->getValueName(I);
323 if (name.find(".") != std::string::npos)
326 I->setSection(TAI->getBSSSection_()->getName());
328 const Type *Ty = C->getType();
329 unsigned Size = TD->getTypePaddedSize(Ty);
331 O << name << " " <<"RES"<< " " << Size ;
337 bool PIC16AsmPrinter::doFinalization(Module &M) {
338 O << "\t" << "END\n";
339 bool Result = AsmPrinter::doFinalization(M);
343 void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
344 const Function *F = MF.getFunction();
345 std::string FuncName = Mang->getValueName(F);
346 Module *M = const_cast<Module *>(F->getParent());
347 const TargetData *TD = TM.getTargetData();
348 unsigned FrameSize = 0;
349 // Emit the data section name.
351 std::string SectionName = "fdata." + CurrentFnName + ".# " + "UDATA";
353 const Section *fDataSection = TAI->getNamedSection(SectionName.c_str(),
354 SectionFlags::Writeable);
355 SwitchToSection(fDataSection);
358 // Emit function frame label
359 O << CurrentFnName << ".frame:\n";
361 const Type *RetType = F->getReturnType();
362 unsigned RetSize = 0;
363 if (RetType->getTypeID() != Type::VoidTyID)
364 RetSize = TD->getTypePaddedSize(RetType);
366 //Emit function return value space
368 O << CurrentFnName << ".retval RES " << RetSize << "\n";
370 O << CurrentFnName << ".retval:\n";
372 // Emit variable to hold the space for function arguments
373 unsigned ArgSize = 0;
374 for (Function::const_arg_iterator argi = F->arg_begin(),
375 arge = F->arg_end(); argi != arge ; ++argi) {
376 const Type *Ty = argi->getType();
377 ArgSize += TD->getTypePaddedSize(Ty);
379 O << CurrentFnName << ".args RES " << ArgSize << "\n";
381 // Emit the function variables.
383 // In PIC16 all the function arguments and local variables are global.
384 // Therefore to get the variable belonging to this function entire
385 // global list will be traversed and variables belonging to this function
386 // will be emitted in the current data section.
387 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
389 std::string VarName = Mang->getValueName(I);
391 // The variables of a function are of form FuncName.* . If this variable
392 // does not belong to this function then continue.
393 // Static local varilabes of a function does not have .auto. in their
394 // name. They are not printed as part of function data but module
395 // level global data.
396 if (! isLocalToFunc(FuncName, VarName))
399 I->setSection("fdata." + CurrentFnName + ".#");
400 Constant *C = I->getInitializer();
401 const Type *Ty = C->getType();
402 unsigned Size = TD->getTypePaddedSize(Ty);
404 // Emit memory reserve directive.
405 O << VarName << " RES " << Size << "\n";
408 int TempSize = PTLI->GetTmpSize();
410 O << CurrentFnName << ".tmp RES " << TempSize <<"\n";