blob: a72b8e5e5001e53585e726ba3e01810ee27b9bd5 [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"
Bill Wendlingcb819f12009-02-18 23:12:06 +000017#include "llvm/DerivedTypes.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000018#include "llvm/Function.h"
19#include "llvm/Module.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000020#include "llvm/CodeGen/DwarfWriter.h"
David Greene71847812009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000023#include "llvm/Support/Mangler.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +000025#include "llvm/CodeGen/DwarfWriter.h"
26#include "llvm/CodeGen/MachineModuleInfo.h"
Daniel Dunbar0c795d62009-07-25 06:49:55 +000027#include "llvm/Target/TargetRegistry.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000028
29using namespace llvm;
30
Sanjiv Gupta0e687712008-05-13 09:02:57 +000031#include "PIC16GenAsmWriter.inc"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000032
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000033bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000034 printInstruction(MI);
35 return true;
36}
37
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000038/// runOnMachineFunction - This emits the frame section, autos section and
39/// assembly for each instruction. Also takes care of function begin debug
40/// directive and file begin debug directive (if required) for the function.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000041///
42bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +000043 this->MF = &MF;
44
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000045 // This calls the base class function required to be called at beginning
46 // of runOnMachineFunction.
47 SetupMachineFunction(MF);
48
49 // Get the mangled name.
50 const Function *F = MF.getFunction();
Chris Lattnerb8158ac2009-07-14 18:17:16 +000051 CurrentFnName = Mang->getMangledName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000052
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000053 // Emit the function frame (args and temps).
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000054 EmitFunctionFrame(MF);
55
Sanjiv Guptabde79422009-06-16 09:45:18 +000056 DbgInfo.BeginFunction(MF);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000057
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000058 // Emit the autos section of function.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000059 EmitAutos(CurrentFnName);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000060
61 // Now emit the instructions of function in its code section.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000062 const char *codeSection = PAN::getCodeSectionName(CurrentFnName).c_str();
63
Chris Lattner5fe575f2009-07-27 05:32:16 +000064 const Section *fCodeSection =
65 TAI->getNamedSection(codeSection, SectionKind::Text);
Sanjiv Gupta211f3622009-05-10 05:23:47 +000066 // Start the Code Section.
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000067 O << "\n";
Chris Lattnerdc471462009-07-21 16:44:47 +000068 SwitchToSection(fCodeSection);
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000069
70 // Emit the frame address of the function at the beginning of code.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000071 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
72 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000073
Sanjiv Gupta211f3622009-05-10 05:23:47 +000074 // Emit function start label.
75 O << CurrentFnName << ":\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000076
Sanjiv Guptabde79422009-06-16 09:45:18 +000077 DebugLoc CurDL;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000078 O << "\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000079 // Print out code for the function.
80 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
81 I != E; ++I) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000082
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000083 // Print a label for the basic block.
84 if (I != MF.begin()) {
85 printBasicBlockLabel(I, true);
86 O << '\n';
87 }
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000088
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000089 // Print a basic block.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000090 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
91 II != E; ++II) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000092
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000093 // Emit the line directive if source line changed.
94 const DebugLoc DL = II->getDebugLoc();
Sanjiv Guptabde79422009-06-16 09:45:18 +000095 if (!DL.isUnknown() && DL != CurDL) {
96 DbgInfo.ChangeDebugLoc(MF, DL);
97 CurDL = DL;
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000098 }
Sanjiv Gupta0608b492009-05-11 06:01:38 +000099
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000100 // Print the assembly for the instruction.
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000101 printMachineInstruction(II);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000102 }
103 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000104
105 // Emit function end debug directives.
Sanjiv Guptabde79422009-06-16 09:45:18 +0000106 DbgInfo.EndFunction(MF);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000107
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000108 return false; // we didn't modify anything.
109}
110
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000111
112// printOperand - print operand of insn.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000113void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000114 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000115
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000116 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000117 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000118 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000119 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000120 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000121 llvm_unreachable("not implemented");
Torok Edwinc25e7582009-07-11 20:10:48 +0000122 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000123
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000124 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000125 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000126 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000127
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000128 case MachineOperand::MO_GlobalAddress: {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000129 O << Mang->getMangledName(MO.getGlobal());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000130 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000131 }
132 case MachineOperand::MO_ExternalSymbol: {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000133 const char *Sname = MO.getSymbolName();
134
135 // If its a libcall name, record it to decls section.
136 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000137 LibcallDecls.push_back(Sname);
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000138 }
139
140 O << Sname;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000141 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000142 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000143 case MachineOperand::MO_MachineBasicBlock:
144 printBasicBlockLabel(MO.getMBB());
145 return;
146
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000147 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000148 llvm_unreachable(" Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000149 }
150}
151
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000152/// printCCOperand - Print the cond code operand.
153///
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000154void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
155 int CC = (int)MI->getOperand(opNum).getImm();
156 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
157}
158
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000159/// printLibcallDecls - print the extern declarations for compiler
160/// intrinsics.
161///
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000162void PIC16AsmPrinter::printLibcallDecls(void) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000163 // If no libcalls used, return.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000164 if (LibcallDecls.empty()) return;
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000165
Sanjiv Gupta42918572009-05-12 06:52:41 +0000166 O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000167 // Remove duplicate entries.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000168 LibcallDecls.sort();
169 LibcallDecls.unique();
170 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
171 I != LibcallDecls.end(); I++) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000172 O << TAI->getExternDirective() << *I << "\n";
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000173 O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
174 O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000175 }
Sanjiv Gupta42918572009-05-12 06:52:41 +0000176 O << TAI->getCommentString() << "External decls for libcalls - END." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000177}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000178
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000179/// doInitialization - Perfrom Module level initializations here.
180/// One task that we do here is to sectionize all global variables.
181/// The MemSelOptimizer pass depends on the sectionizing.
182///
Chris Lattnerdc471462009-07-21 16:44:47 +0000183bool PIC16AsmPrinter::doInitialization(Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000184 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000185
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000186 // FIXME:: This is temporary solution to generate the include file.
187 // The processor should be passed to llc as in input and the header file
188 // should be generated accordingly.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000189 O << "\n\t#include P16F1937.INC\n";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000190
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000191 // Set the section names for all globals.
192 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
193 I != E; ++I) {
194 I->setSection(TAI->SectionForGlobal(I)->getName());
195 }
196
Sanjiv Guptabde79422009-06-16 09:45:18 +0000197 DbgInfo.BeginModule(M);
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000198 EmitFunctionDecls(M);
199 EmitUndefinedVars(M);
200 EmitDefinedVars(M);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000201 EmitIData(M);
202 EmitUData(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000203 EmitRomData(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000204 return Result;
205}
206
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000207/// Emit extern decls for functions imported from other modules, and emit
208/// global declarations for function defined in this module and which are
209/// available to other modules.
210///
Chris Lattnerdc471462009-07-21 16:44:47 +0000211void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000212 // Emit declarations for external functions.
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000213 O <<"\n"<<TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000214 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
Sanjiv Gupta0f009d22009-07-23 02:11:04 +0000215 if (I->isIntrinsic())
216 continue;
217
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000218 std::string Name = Mang->getMangledName(I);
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000219 if (Name.compare("@abort") == 0)
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000220 continue;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000221
Chris Lattnerdc471462009-07-21 16:44:47 +0000222 if (!I->isDeclaration() && !I->hasExternalLinkage())
Sanjiv Guptaaf3fdb52009-05-10 16:18:39 +0000223 continue;
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000224
225 const char *directive = I->isDeclaration() ? TAI->getExternDirective() :
226 TAI->getGlobalDirective();
227
228 O << directive << Name << "\n";
229 O << directive << PAN::getRetvalLabel(Name) << "\n";
230 O << directive << PAN::getArgsLabel(Name) << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000231 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000232
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000233 O << TAI->getCommentString() << "Function Declarations - END." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000234}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000235
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000236// Emit variables imported from other Modules.
Chris Lattnerdc471462009-07-21 16:44:47 +0000237void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000238 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDecls->Items;
Chris Lattnerdc471462009-07-21 16:44:47 +0000239 if (!Items.size()) return;
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000240
241 O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
242 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000243 O << TAI->getExternDirective() << Mang->getMangledName(Items[j]) << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000244 }
245 O << TAI->getCommentString() << "Imported Variables - END" << "\n";
246}
247
248// Emit variables defined in this module and are available to other modules.
Chris Lattnerdc471462009-07-21 16:44:47 +0000249void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000250 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDefs->Items;
Chris Lattnerdc471462009-07-21 16:44:47 +0000251 if (!Items.size()) return;
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000252
Chris Lattnera46cb522009-07-21 17:20:18 +0000253 O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000254 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000255 O << TAI->getGlobalDirective() << Mang->getMangledName(Items[j]) << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000256 }
257 O << TAI->getCommentString() << "Exported Variables - END" << "\n";
258}
259
260// Emit initialized data placed in ROM.
Chris Lattnerdc471462009-07-21 16:44:47 +0000261void PIC16AsmPrinter::EmitRomData(Module &M) {
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000262 // Print ROM Data section.
Chris Lattnerdc471462009-07-21 16:44:47 +0000263 const std::vector<PIC16Section*> &ROSections = PTAI->ROSections;
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000264 for (unsigned i = 0; i < ROSections.size(); i++) {
Chris Lattnerdc471462009-07-21 16:44:47 +0000265 const std::vector<const GlobalVariable*> &Items = ROSections[i]->Items;
266 if (!Items.size()) continue;
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000267 O << "\n";
268 SwitchToSection(PTAI->ROSections[i]->S_);
269 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000270 O << Mang->getMangledName(Items[j]);
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000271 Constant *C = Items[j]->getInitializer();
272 int AddrSpace = Items[j]->getType()->getAddressSpace();
273 EmitGlobalConstant(C, AddrSpace);
274 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000275 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000276}
277
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000278bool PIC16AsmPrinter::doFinalization(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000279 printLibcallDecls();
Sanjiv Guptab157f252009-06-09 15:31:19 +0000280 EmitRemainingAutos();
Sanjiv Guptabde79422009-06-16 09:45:18 +0000281 DbgInfo.EndModule(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000282 O << "\n\t" << "END\n";
Chris Lattner40bbebd2009-07-21 18:38:57 +0000283 return AsmPrinter::doFinalization(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000284}
285
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000286void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000287 const Function *F = MF.getFunction();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000288 std::string FuncName = Mang->getMangledName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000289 const TargetData *TD = TM.getTargetData();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000290 // Emit the data section name.
291 O << "\n";
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000292 const char *SectionName = PAN::getFrameSectionName(CurrentFnName).c_str();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000293
Chris Lattner5fe575f2009-07-27 05:32:16 +0000294 const Section *fPDataSection =
295 TAI->getNamedSection(SectionName, SectionKind::DataRel);
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000296 SwitchToSection(fPDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000297
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000298 // Emit function frame label
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000299 O << PAN::getFrameLabel(CurrentFnName) << ":\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000300
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000301 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000302 unsigned RetSize = 0;
303 if (RetType->getTypeID() != Type::VoidTyID)
Duncan Sands777d2302009-05-09 07:06:46 +0000304 RetSize = TD->getTypeAllocSize(RetType);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000305
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000306 //Emit function return value space
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000307 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
308 // we will need to avoid printing a global directive for Retval label
309 // in emitExternandGloblas.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000310 if(RetSize > 0)
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000311 O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000312 else
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000313 O << PAN::getRetvalLabel(CurrentFnName) << ": \n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000314
315 // Emit variable to hold the space for function arguments
316 unsigned ArgSize = 0;
317 for (Function::const_arg_iterator argi = F->arg_begin(),
318 arge = F->arg_end(); argi != arge ; ++argi) {
319 const Type *Ty = argi->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000320 ArgSize += TD->getTypeAllocSize(Ty);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000321 }
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000322
323 O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000324
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000325 // Emit temporary space
326 int TempSize = PTLI->GetTmpSize();
Chris Lattnerdc471462009-07-21 16:44:47 +0000327 if (TempSize > 0)
328 O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize << '\n';
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000329}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000330
Chris Lattnerdc471462009-07-21 16:44:47 +0000331void PIC16AsmPrinter::EmitIData(Module &M) {
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000332
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000333 // Print all IDATA sections.
Chris Lattnerdc471462009-07-21 16:44:47 +0000334 const std::vector<PIC16Section*> &IDATASections = PTAI->IDATASections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000335 for (unsigned i = 0; i < IDATASections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000336 O << "\n";
Sanjiv Gupta024e94c2009-07-06 18:07:06 +0000337 if (IDATASections[i]->S_->getName().find("llvm.") != std::string::npos)
338 continue;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000339 SwitchToSection(IDATASections[i]->S_);
340 std::vector<const GlobalVariable*> Items = IDATASections[i]->Items;
341 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000342 std::string Name = Mang->getMangledName(Items[j]);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000343 Constant *C = Items[j]->getInitializer();
344 int AddrSpace = Items[j]->getType()->getAddressSpace();
345 O << Name;
346 EmitGlobalConstant(C, AddrSpace);
347 }
348 }
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000349}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000350
Chris Lattnerdc471462009-07-21 16:44:47 +0000351void PIC16AsmPrinter::EmitUData(Module &M) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000352 const TargetData *TD = TM.getTargetData();
353
354 // Print all BSS sections.
Chris Lattnerdc471462009-07-21 16:44:47 +0000355 const std::vector<PIC16Section*> &BSSSections = PTAI->BSSSections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000356 for (unsigned i = 0; i < BSSSections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000357 O << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000358 SwitchToSection(BSSSections[i]->S_);
359 std::vector<const GlobalVariable*> Items = BSSSections[i]->Items;
360 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000361 std::string Name = Mang->getMangledName(Items[j]);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000362 Constant *C = Items[j]->getInitializer();
363 const Type *Ty = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000364 unsigned Size = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000365
Chris Lattnerdc471462009-07-21 16:44:47 +0000366 O << Name << " RES " << Size << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000367 }
368 }
369}
370
Chris Lattnerdc471462009-07-21 16:44:47 +0000371void PIC16AsmPrinter::EmitAutos(std::string FunctName) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000372 // Section names for all globals are already set.
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000373 const TargetData *TD = TM.getTargetData();
374
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000375 // Now print Autos section for this function.
376 std::string SectionName = PAN::getAutosSectionName(FunctName);
Chris Lattnerdc471462009-07-21 16:44:47 +0000377 const std::vector<PIC16Section*> &AutosSections = PTAI->AutosSections;
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000378 for (unsigned i = 0; i < AutosSections.size(); i++) {
379 O << "\n";
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000380 if (AutosSections[i]->S_->getName() == SectionName) {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000381 // Set the printing status to true
382 AutosSections[i]->setPrintedStatus(true);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000383 SwitchToSection(AutosSections[i]->S_);
Chris Lattnerdc471462009-07-21 16:44:47 +0000384 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000385 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000386 std::string VarName = Mang->getMangledName(Items[j]);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000387 Constant *C = Items[j]->getInitializer();
388 const Type *Ty = C->getType();
389 unsigned Size = TD->getTypeAllocSize(Ty);
390 // Emit memory reserve directive.
391 O << VarName << " RES " << Size << "\n";
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000392 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000393 break;
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000394 }
395 }
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000396}
397
Sanjiv Guptab157f252009-06-09 15:31:19 +0000398// Print autos that were not printed during the code printing of functions.
399// As the functions might themselves would have got deleted by the optimizer.
Chris Lattnerdc471462009-07-21 16:44:47 +0000400void PIC16AsmPrinter::EmitRemainingAutos() {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000401 const TargetData *TD = TM.getTargetData();
402
403 // Now print Autos section for this function.
404 std::vector <PIC16Section *>AutosSections = PTAI->AutosSections;
405 for (unsigned i = 0; i < AutosSections.size(); i++) {
406
407 // if the section is already printed then don't print again
408 if (AutosSections[i]->isPrinted())
409 continue;
410
411 // Set status as printed
412 AutosSections[i]->setPrintedStatus(true);
413
414 O << "\n";
415 SwitchToSection(AutosSections[i]->S_);
Chris Lattnerdc471462009-07-21 16:44:47 +0000416 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
Sanjiv Guptab157f252009-06-09 15:31:19 +0000417 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000418 std::string VarName = Mang->getMangledName(Items[j]);
Sanjiv Guptab157f252009-06-09 15:31:19 +0000419 Constant *C = Items[j]->getInitializer();
420 const Type *Ty = C->getType();
421 unsigned Size = TD->getTypeAllocSize(Ty);
422 // Emit memory reserve directive.
423 O << VarName << " RES " << Size << "\n";
424 }
425 }
426}
427
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000428
429extern "C" void LLVMInitializePIC16Target() {
430 // Register the targets
431 RegisterTargetMachine<PIC16TargetMachine> A(ThePIC16Target);
432 RegisterTargetMachine<CooperTargetMachine> B(TheCooperTarget);
433 RegisterAsmPrinter<PIC16AsmPrinter> C(ThePIC16Target);
434 RegisterAsmPrinter<PIC16AsmPrinter> D(TheCooperTarget);
435}