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 // If we have a Global address or external symbol then we need to print
53 unsigned BankSelVar
= 0;
54 MachineOperand Op
= MI
->getOperand(BankSelVar
);
55 while (BankSelVar
< Operands
-1) {
56 Op
= MI
->getOperand(BankSelVar
);
57 if ((Op
.getType() == MachineOperand::MO_GlobalAddress
) ||
58 (Op
.getType() == MachineOperand::MO_ExternalSymbol
))
62 if (BankSelVar
< Operands
-1) {
63 unsigned OpType
= Op
.getType();
64 if (OpType
== MachineOperand::MO_GlobalAddress
)
65 NewBank
= Op
.getGlobal()->getSection();
67 // External Symbol is generated for temp data and arguments. They are
68 // in fpdata.<functionname>.# section.
69 std::string ESName
= Op
.getSymbolName();
70 int index
= ESName
.find_first_of(".");
71 std::string FnName
= ESName
.substr(0,index
);
72 NewBank
= "fpdata." + FnName
+".#";
74 // Operand after global address or external symbol should be banksel.
75 // Value 1 for this operand means we need to generate banksel else do not
77 const MachineOperand
&BS
= MI
->getOperand(BankSelVar
+1);
78 // If Section names are same then the variables are in same section.
79 // This is not true for external variables as section names for global
80 // variables in all files are same at this time. For eg. initialized
81 // data in put in idata.# section in all files.
82 if ((BS
.getType() == MachineOperand::MO_Immediate
83 && (int)BS
.getImm() == 1)
84 && ((Op
.isGlobal() && Op
.getGlobal()->hasExternalLinkage()) ||
85 (NewBank
.compare(CurBank
) != 0))) {
87 printOperand(MI
, BankSelVar
);
97 /// runOnMachineFunction - This uses the printInstruction()
98 /// method to print assembly for each instruction.
100 bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction
&MF
) {
103 // This calls the base class function required to be called at beginning
104 // of runOnMachineFunction.
105 SetupMachineFunction(MF
);
107 // Get the mangled name.
108 const Function
*F
= MF
.getFunction();
109 CurrentFnName
= Mang
->getValueName(F
);
111 // Emit the function variables.
112 emitFunctionData(MF
);
113 std::string codeSection
;
114 codeSection
= "code." + CurrentFnName
+ ".# " + "CODE";
115 const Section
*fCodeSection
= TAI
->getNamedSection(codeSection
.c_str(),
118 SwitchToSection (fCodeSection
);
120 // Emit the frame address of the function at the beginning of code.
121 O
<< " retlw low(" << FunctionLabelBegin
<< CurrentFnName
<< ".frame)\n";
122 O
<< " retlw high(" << FunctionLabelBegin
<< CurrentFnName
<< ".frame)\n";
123 O
<< CurrentFnName
<< ":\n";
126 // Print out code for the function.
127 for (MachineFunction::const_iterator I
= MF
.begin(), E
= MF
.end();
129 // Print a label for the basic block.
130 if (I
!= MF
.begin()) {
131 printBasicBlockLabel(I
, true);
136 // For emitting line directives, we need to keep track of the current
137 // source line. When it changes then only emit the line directive.
138 unsigned CurLine
= 0;
139 for (MachineBasicBlock::const_iterator II
= I
->begin(), E
= I
->end();
141 // Emit the line directive if source line changed.
142 const DebugLoc DL
= II
->getDebugLoc();
143 if (!DL
.isUnknown()) {
144 unsigned line
= MF
.getDebugLocTuple(DL
).Line
;
145 if (line
!= CurLine
) {
146 O
<< "\t.line " << line
<< "\n";
150 // Print the assembly for the instruction.
151 printMachineInstruction(II
);
154 return false; // we didn't modify anything.
157 /// createPIC16CodePrinterPass - Returns a pass that prints the PIC16
158 /// assembly code for a MachineFunction to the given output stream,
159 /// using the given target machine description. This should work
160 /// regardless of whether the function is in SSA form.
162 FunctionPass
*llvm::createPIC16CodePrinterPass(raw_ostream
&o
,
163 PIC16TargetMachine
&tm
,
164 bool fast
, bool verbose
) {
165 return new PIC16AsmPrinter(o
, tm
, tm
.getTargetAsmInfo(), fast
, verbose
);
168 void PIC16AsmPrinter::printOperand(const MachineInstr
*MI
, int opNum
) {
169 const MachineOperand
&MO
= MI
->getOperand(opNum
);
171 switch (MO
.getType()) {
172 case MachineOperand::MO_Register
:
173 if (TargetRegisterInfo::isPhysicalRegister(MO
.getReg()))
174 O
<< TM
.getRegisterInfo()->get(MO
.getReg()).AsmName
;
176 assert(0 && "not implemented");
179 case MachineOperand::MO_Immediate
:
180 O
<< (int)MO
.getImm();
183 case MachineOperand::MO_GlobalAddress
: {
184 std::string Name
= Mang
->getValueName(MO
.getGlobal());
185 if (isLocalName(Name
))
186 O
<< FunctionLabelBegin
<< Mang
->getValueName(MO
.getGlobal());
188 O
<< Mang
->getValueName(MO
.getGlobal());
191 case MachineOperand::MO_ExternalSymbol
: {
192 std::string Name
= MO
.getSymbolName();
193 if (Name
.find("__intrinsics.") != std::string::npos
)
194 O
<< MO
.getSymbolName();
196 O
<< FunctionLabelBegin
<< MO
.getSymbolName();
199 case MachineOperand::MO_MachineBasicBlock
:
200 printBasicBlockLabel(MO
.getMBB());
204 assert(0 && " Operand type not supported.");
208 void PIC16AsmPrinter::printCCOperand(const MachineInstr
*MI
, int opNum
) {
209 int CC
= (int)MI
->getOperand(opNum
).getImm();
210 O
<< PIC16CondCodeToString((PIC16CC::CondCodes
)CC
);
214 bool PIC16AsmPrinter::doInitialization (Module
&M
) {
215 bool Result
= AsmPrinter::doInitialization(M
);
216 // FIXME:: This is temporary solution to generate the include file.
217 // The processor should be passed to llc as in input and the header file
218 // should be generated accordingly.
219 O
<< "\t#include P16F1937.INC\n";
220 MachineModuleInfo
*MMI
= getAnalysisIfAvailable
<MachineModuleInfo
>();
222 DwarfWriter
*DW
= getAnalysisIfAvailable
<DwarfWriter
>();
223 assert(DW
&& "Dwarf Writer is not available");
224 DW
->BeginModule(&M
, MMI
, O
, this, TAI
);
226 EmitExternsAndGlobals (M
);
233 void PIC16AsmPrinter::EmitExternsAndGlobals (Module
&M
) {
234 // Emit declarations for external functions.
235 O
<< "section.0" <<"\n";
236 for (Module::iterator I
= M
.begin(), E
= M
.end(); I
!= E
; I
++) {
237 std::string Name
= Mang
->getValueName(I
);
238 if (Name
.compare("abort") == 0)
241 // If it is llvm intrinsic call then don't emit
242 if (Name
.find("llvm.") != std::string::npos
)
245 if (I
->isDeclaration()) {
246 O
<< "\textern " <<Name
<< "\n";
247 O
<< "\textern " << FunctionLabelBegin
<< Name
<< ".retval\n";
248 O
<< "\textern " << FunctionLabelBegin
<< Name
<< ".args\n";
250 else if (I
->hasExternalLinkage()) {
251 O
<< "\tglobal " << Name
<< "\n";
252 O
<< "\tglobal " << FunctionLabelBegin
<< Name
<< ".retval\n";
253 O
<< "\tglobal " << FunctionLabelBegin
<< Name
<< ".args\n";
257 // Emit header file to include declaration of library functions
258 O
<< "\t#include C16IntrinsicCalls.INC\n";
260 // Emit declarations for external globals.
261 for (Module::const_global_iterator I
= M
.global_begin(), E
= M
.global_end();
263 // Any variables reaching here with ".auto." in its name is a local scope
264 // variable and should not be printed in global data section.
265 std::string Name
= Mang
->getValueName(I
);
266 if (isLocalName (Name
))
269 if (I
->isDeclaration())
270 O
<< "\textern "<< Name
<< "\n";
271 else if (I
->hasCommonLinkage() || I
->hasExternalLinkage())
272 O
<< "\tglobal "<< Name
<< "\n";
276 void PIC16AsmPrinter::EmitInitData (Module
&M
) {
277 SwitchToSection(TAI
->getDataSection());
278 for (Module::global_iterator I
= M
.global_begin(), E
= M
.global_end();
280 if (!I
->hasInitializer()) // External global require no code.
283 Constant
*C
= I
->getInitializer();
284 const PointerType
*PtrTy
= I
->getType();
285 int AddrSpace
= PtrTy
->getAddressSpace();
287 if ((!C
->isNullValue()) && (AddrSpace
== PIC16ISD::RAM_SPACE
)) {
289 if (EmitSpecialLLVMGlobal(I
))
292 // Any variables reaching here with "." in its name is a local scope
293 // variable and should not be printed in global data section.
294 std::string Name
= Mang
->getValueName(I
);
295 if (isLocalName(Name
))
298 I
->setSection(TAI
->getDataSection()->getName());
300 EmitGlobalConstant(C
, AddrSpace
);
305 void PIC16AsmPrinter::EmitRomData (Module
&M
)
307 SwitchToSection(TAI
->getReadOnlySection());
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 const PointerType
*PtrTy
= I
->getType();
316 int AddrSpace
= PtrTy
->getAddressSpace();
317 if ((!C
->isNullValue()) && (AddrSpace
== PIC16ISD::ROM_SPACE
)) {
319 if (EmitSpecialLLVMGlobal(I
))
322 // Any variables reaching here with "." in its name is a local scope
323 // variable and should not be printed in global data section.
324 std::string name
= Mang
->getValueName(I
);
325 if (name
.find(".") != std::string::npos
)
328 I
->setSection(TAI
->getReadOnlySection()->getName());
330 EmitGlobalConstant(C
, AddrSpace
);
337 void PIC16AsmPrinter::EmitUnInitData (Module
&M
)
339 SwitchToSection(TAI
->getBSSSection_());
340 const TargetData
*TD
= TM
.getTargetData();
342 for (Module::global_iterator I
= M
.global_begin(), E
= M
.global_end();
344 if (!I
->hasInitializer()) // External global require no code.
347 Constant
*C
= I
->getInitializer();
348 if (C
->isNullValue()) {
350 if (EmitSpecialLLVMGlobal(I
))
353 // Any variables reaching here with "." in its name is a local scope
354 // variable and should not be printed in global data section.
355 std::string name
= Mang
->getValueName(I
);
356 if (name
.find(".") != std::string::npos
)
359 I
->setSection(TAI
->getBSSSection_()->getName());
361 const Type
*Ty
= C
->getType();
362 unsigned Size
= TD
->getTypePaddedSize(Ty
);
364 O
<< name
<< " " <<"RES"<< " " << Size
;
370 bool PIC16AsmPrinter::doFinalization(Module
&M
) {
371 O
<< "\t" << "END\n";
372 bool Result
= AsmPrinter::doFinalization(M
);
376 void PIC16AsmPrinter::emitFunctionData(MachineFunction
&MF
) {
377 const Function
*F
= MF
.getFunction();
378 std::string FuncName
= Mang
->getValueName(F
);
379 Module
*M
= const_cast<Module
*>(F
->getParent());
380 const TargetData
*TD
= TM
.getTargetData();
381 unsigned FrameSize
= 0;
382 // Emit the data section name.
384 std::string SectionName
= "fpdata." + CurrentFnName
+ ".# " + "UDATA_OVR";
386 const Section
*fPDataSection
= TAI
->getNamedSection(SectionName
.c_str(),
387 SectionFlags::Writeable
);
388 SwitchToSection(fPDataSection
);
391 // Emit function frame label
392 O
<< FunctionLabelBegin
<< CurrentFnName
<< ".frame:\n";
394 const Type
*RetType
= F
->getReturnType();
395 unsigned RetSize
= 0;
396 if (RetType
->getTypeID() != Type::VoidTyID
)
397 RetSize
= TD
->getTypePaddedSize(RetType
);
399 //Emit function return value space
401 O
<< FunctionLabelBegin
<< CurrentFnName
<< ".retval RES " << RetSize
404 O
<< FunctionLabelBegin
<< CurrentFnName
<< ".retval:\n";
406 // Emit variable to hold the space for function arguments
407 unsigned ArgSize
= 0;
408 for (Function::const_arg_iterator argi
= F
->arg_begin(),
409 arge
= F
->arg_end(); argi
!= arge
; ++argi
) {
410 const Type
*Ty
= argi
->getType();
411 ArgSize
+= TD
->getTypePaddedSize(Ty
);
413 O
<< FunctionLabelBegin
<< CurrentFnName
<< ".args RES " << ArgSize
416 // Emit temporary space
417 int TempSize
= PTLI
->GetTmpSize();
419 O
<< FunctionLabelBegin
<< CurrentFnName
<< ".tmp RES " << TempSize
422 // Emit the section name for local variables.
424 std::string SecNameLocals
= "fadata." + CurrentFnName
+ ".# " + "UDATA_OVR";
426 const Section
*fADataSection
= TAI
->getNamedSection(SecNameLocals
.c_str(),
427 SectionFlags::Writeable
);
428 SwitchToSection(fADataSection
);
430 // Emit the function variables.
432 // In PIC16 all the function arguments and local variables are global.
433 // Therefore to get the variable belonging to this function entire
434 // global list will be traversed and variables belonging to this function
435 // will be emitted in the current data section.
436 for (Module::global_iterator I
= M
->global_begin(), E
= M
->global_end();
438 std::string VarName
= Mang
->getValueName(I
);
440 // The variables of a function are of form FuncName.* . If this variable
441 // does not belong to this function then continue.
442 // Static local varilabes of a function does not have .auto. in their
443 // name. They are not printed as part of function data but module
444 // level global data.
445 if (! isLocalToFunc(FuncName
, VarName
))
448 I
->setSection("fadata." + CurrentFnName
+ ".#");
449 Constant
*C
= I
->getInitializer();
450 const Type
*Ty
= C
->getType();
451 unsigned Size
= TD
->getTypePaddedSize(Ty
);
453 // Emit memory reserve directive.
454 O
<< FunctionLabelBegin
<< VarName
<< " RES " << Size
<< "\n";