blob: 7fad6f315683a35337a4e638d484a7c90abf4286 [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"
Chris Lattnerf0144122009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Sanjiv Gupta0e687712008-05-13 09:02:57 +000029
30using namespace llvm;
31
Sanjiv Gupta0e687712008-05-13 09:02:57 +000032#include "PIC16GenAsmWriter.inc"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000033
Chris Lattnerf0144122009-07-28 03:13:23 +000034PIC16AsmPrinter::PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
35 const TargetAsmInfo *T, bool V)
36: AsmPrinter(O, TM, T, V), DbgInfo(O, T) {
37 PTLI = static_cast<const PIC16TargetLowering*>(TM.getTargetLowering());
38 PTAI = static_cast<const PIC16TargetAsmInfo*>(T);
39 PTOF = static_cast<const PIC16TargetObjectFile*>(&PTLI->getObjFileLowering());
40}
41
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000042bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000043 printInstruction(MI);
44 return true;
45}
46
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000047/// runOnMachineFunction - This emits the frame section, autos section and
48/// assembly for each instruction. Also takes care of function begin debug
49/// directive and file begin debug directive (if required) for the function.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000050///
51bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +000052 this->MF = &MF;
53
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000054 // This calls the base class function required to be called at beginning
55 // of runOnMachineFunction.
56 SetupMachineFunction(MF);
57
58 // Get the mangled name.
59 const Function *F = MF.getFunction();
Chris Lattnerb8158ac2009-07-14 18:17:16 +000060 CurrentFnName = Mang->getMangledName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000061
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000062 // Emit the function frame (args and temps).
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000063 EmitFunctionFrame(MF);
64
Sanjiv Guptabde79422009-06-16 09:45:18 +000065 DbgInfo.BeginFunction(MF);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000066
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000067 // Emit the autos section of function.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000068 EmitAutos(CurrentFnName);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000069
70 // Now emit the instructions of function in its code section.
Chris Lattnerf0144122009-07-28 03:13:23 +000071 std::string T = PAN::getCodeSectionName(CurrentFnName);
72 const char *codeSection = T.c_str();
Sanjiv Gupta211f3622009-05-10 05:23:47 +000073
Chris Lattner5fe575f2009-07-27 05:32:16 +000074 const Section *fCodeSection =
Chris Lattnerf0144122009-07-28 03:13:23 +000075 getObjFileLowering().getOrCreateSection(codeSection, false,
76 SectionKind::Text);
Sanjiv Gupta211f3622009-05-10 05:23:47 +000077 // Start the Code Section.
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000078 O << "\n";
Chris Lattnerdc471462009-07-21 16:44:47 +000079 SwitchToSection(fCodeSection);
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000080
81 // Emit the frame address of the function at the beginning of code.
Sanjiv Gupta211f3622009-05-10 05:23:47 +000082 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
83 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000084
Sanjiv Gupta211f3622009-05-10 05:23:47 +000085 // Emit function start label.
86 O << CurrentFnName << ":\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000087
Sanjiv Guptabde79422009-06-16 09:45:18 +000088 DebugLoc CurDL;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +000089 O << "\n";
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000090 // Print out code for the function.
91 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
92 I != E; ++I) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +000093
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000094 // Print a label for the basic block.
95 if (I != MF.begin()) {
96 printBasicBlockLabel(I, true);
97 O << '\n';
98 }
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +000099
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000100 // Print a basic block.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000101 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
102 II != E; ++II) {
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000103
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000104 // Emit the line directive if source line changed.
105 const DebugLoc DL = II->getDebugLoc();
Sanjiv Guptabde79422009-06-16 09:45:18 +0000106 if (!DL.isUnknown() && DL != CurDL) {
107 DbgInfo.ChangeDebugLoc(MF, DL);
108 CurDL = DL;
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000109 }
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000110
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000111 // Print the assembly for the instruction.
Sanjiv Guptac1fa70c2009-04-08 06:24:04 +0000112 printMachineInstruction(II);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000113 }
114 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000115
116 // Emit function end debug directives.
Sanjiv Guptabde79422009-06-16 09:45:18 +0000117 DbgInfo.EndFunction(MF);
Sanjiv Guptab65d1f22009-06-11 06:49:55 +0000118
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000119 return false; // we didn't modify anything.
120}
121
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000122
123// printOperand - print operand of insn.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000124void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000125 const MachineOperand &MO = MI->getOperand(opNum);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000126
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000127 switch (MO.getType()) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000128 case MachineOperand::MO_Register:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000129 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000130 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000131 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000132 llvm_unreachable("not implemented");
Torok Edwinc25e7582009-07-11 20:10:48 +0000133 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000134
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000135 case MachineOperand::MO_Immediate:
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000136 O << (int)MO.getImm();
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000137 return;
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +0000138
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000139 case MachineOperand::MO_GlobalAddress: {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000140 O << Mang->getMangledName(MO.getGlobal());
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000141 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000142 }
143 case MachineOperand::MO_ExternalSymbol: {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000144 const char *Sname = MO.getSymbolName();
145
146 // If its a libcall name, record it to decls section.
147 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000148 LibcallDecls.push_back(Sname);
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000149 }
150
151 O << Sname;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000152 break;
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000153 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000154 case MachineOperand::MO_MachineBasicBlock:
155 printBasicBlockLabel(MO.getMBB());
156 return;
157
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000158 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000159 llvm_unreachable(" Operand type not supported.");
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000160 }
161}
162
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000163/// printCCOperand - Print the cond code operand.
164///
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000165void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
166 int CC = (int)MI->getOperand(opNum).getImm();
167 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
168}
169
Sanjiv Guptabe7b97b2009-07-27 18:04:34 +0000170// This function is used to sort the decls list.
171// should return true if s1 should come before s2.
172static bool is_before(const char *s1, const char *s2) {
173 std::string str1 = s1;
174 std::string str2 = s2;
175 int i = str1.compare(str2);
176 // Return true if s1 is smaller or equal.
177 if (i <= 0) return true;
178 // false if s1 should come after s2.
179 return false;
180}
181
182// This is used by list::unique below.
183// unique will filter out duplicates if it knows them.
184static bool is_duplicate(const char *s1, const char *s2) {
185 std::string str1 = s1;
186 std::string str2 = s2;
187 return str1 == str2;
188}
189
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000190/// printLibcallDecls - print the extern declarations for compiler
191/// intrinsics.
192///
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000193void PIC16AsmPrinter::printLibcallDecls(void) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000194 // If no libcalls used, return.
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000195 if (LibcallDecls.empty()) return;
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000196
Sanjiv Gupta42918572009-05-12 06:52:41 +0000197 O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000198 // Remove duplicate entries.
Sanjiv Guptabe7b97b2009-07-27 18:04:34 +0000199 LibcallDecls.sort(is_before);
200 LibcallDecls.unique(is_duplicate);
201
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000202 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
203 I != LibcallDecls.end(); I++) {
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000204 O << TAI->getExternDirective() << *I << "\n";
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +0000205 O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
206 O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000207 }
Sanjiv Gupta42918572009-05-12 06:52:41 +0000208 O << TAI->getCommentString() << "External decls for libcalls - END." <<"\n";
Sanjiv Gupta0608b492009-05-11 06:01:38 +0000209}
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000210
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000211/// doInitialization - Perfrom Module level initializations here.
212/// One task that we do here is to sectionize all global variables.
213/// The MemSelOptimizer pass depends on the sectionizing.
214///
Chris Lattnerdc471462009-07-21 16:44:47 +0000215bool PIC16AsmPrinter::doInitialization(Module &M) {
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000216 bool Result = AsmPrinter::doInitialization(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000217
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000218 // FIXME:: This is temporary solution to generate the include file.
219 // The processor should be passed to llc as in input and the header file
220 // should be generated accordingly.
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000221 O << "\n\t#include P16F1937.INC\n";
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000222
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000223 // Set the section names for all globals.
224 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerf0144122009-07-28 03:13:23 +0000225 I != E; ++I)
226 I->setSection(getObjFileLowering().SectionForGlobal(I, TM)->getName());
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000227
Sanjiv Guptabde79422009-06-16 09:45:18 +0000228 DbgInfo.BeginModule(M);
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000229 EmitFunctionDecls(M);
230 EmitUndefinedVars(M);
231 EmitDefinedVars(M);
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000232 EmitIData(M);
233 EmitUData(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000234 EmitRomData(M);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000235 return Result;
236}
237
Sanjiv Guptafa3f80a2009-06-11 06:55:48 +0000238/// Emit extern decls for functions imported from other modules, and emit
239/// global declarations for function defined in this module and which are
240/// available to other modules.
241///
Chris Lattnerdc471462009-07-21 16:44:47 +0000242void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000243 // Emit declarations for external functions.
Sanjiv Guptadcb6da32009-06-13 17:35:54 +0000244 O <<"\n"<<TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000245 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
Sanjiv Gupta0f009d22009-07-23 02:11:04 +0000246 if (I->isIntrinsic())
247 continue;
248
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000249 std::string Name = Mang->getMangledName(I);
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000250 if (Name.compare("@abort") == 0)
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000251 continue;
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000252
Chris Lattnerdc471462009-07-21 16:44:47 +0000253 if (!I->isDeclaration() && !I->hasExternalLinkage())
Sanjiv Guptaaf3fdb52009-05-10 16:18:39 +0000254 continue;
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000255
256 const char *directive = I->isDeclaration() ? TAI->getExternDirective() :
257 TAI->getGlobalDirective();
258
259 O << directive << Name << "\n";
260 O << directive << PAN::getRetvalLabel(Name) << "\n";
261 O << directive << PAN::getArgsLabel(Name) << "\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000262 }
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000263
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000264 O << TAI->getCommentString() << "Function Declarations - END." <<"\n";
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000265}
Sanjiv Gupta2cc75312009-02-10 04:20:26 +0000266
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000267// Emit variables imported from other Modules.
Chris Lattnerdc471462009-07-21 16:44:47 +0000268void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000269 std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDecls->Items;
Chris Lattnerdc471462009-07-21 16:44:47 +0000270 if (!Items.size()) return;
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000271
272 O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
273 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000274 O << TAI->getExternDirective() << Mang->getMangledName(Items[j]) << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000275 }
276 O << TAI->getCommentString() << "Imported Variables - END" << "\n";
277}
278
279// Emit variables defined in this module and are available to other modules.
Chris Lattnerdc471462009-07-21 16:44:47 +0000280void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000281 std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDefs->Items;
Chris Lattnerdc471462009-07-21 16:44:47 +0000282 if (!Items.size()) return;
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000283
Chris Lattnera46cb522009-07-21 17:20:18 +0000284 O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000285 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000286 O << TAI->getGlobalDirective() << Mang->getMangledName(Items[j]) << "\n";
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000287 }
288 O << TAI->getCommentString() << "Exported Variables - END" << "\n";
289}
290
291// Emit initialized data placed in ROM.
Chris Lattnerdc471462009-07-21 16:44:47 +0000292void PIC16AsmPrinter::EmitRomData(Module &M) {
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000293 // Print ROM Data section.
Chris Lattnerf0144122009-07-28 03:13:23 +0000294 const std::vector<PIC16Section*> &ROSections = PTOF->ROSections;
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000295 for (unsigned i = 0; i < ROSections.size(); i++) {
Chris Lattnerdc471462009-07-21 16:44:47 +0000296 const std::vector<const GlobalVariable*> &Items = ROSections[i]->Items;
297 if (!Items.size()) continue;
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000298 O << "\n";
Chris Lattnerf0144122009-07-28 03:13:23 +0000299 SwitchToSection(PTOF->ROSections[i]->S_);
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000300 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000301 O << Mang->getMangledName(Items[j]);
Sanjiv Gupta505996f2009-07-06 10:18:37 +0000302 Constant *C = Items[j]->getInitializer();
303 int AddrSpace = Items[j]->getType()->getAddressSpace();
304 EmitGlobalConstant(C, AddrSpace);
305 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000306 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000307}
308
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000309bool PIC16AsmPrinter::doFinalization(Module &M) {
Sanjiv Guptaad6585b2009-05-13 15:13:17 +0000310 printLibcallDecls();
Sanjiv Guptab157f252009-06-09 15:31:19 +0000311 EmitRemainingAutos();
Sanjiv Guptabde79422009-06-16 09:45:18 +0000312 DbgInfo.EndModule(M);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000313 O << "\n\t" << "END\n";
Chris Lattner40bbebd2009-07-21 18:38:57 +0000314 return AsmPrinter::doFinalization(M);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000315}
316
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000317void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000318 const Function *F = MF.getFunction();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000319 std::string FuncName = Mang->getMangledName(F);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000320 const TargetData *TD = TM.getTargetData();
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000321 // Emit the data section name.
322 O << "\n";
Chris Lattnerf0144122009-07-28 03:13:23 +0000323 std::string T = PAN::getFrameSectionName(CurrentFnName);
324 const char *SectionName = T.c_str();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000325
Chris Lattner5fe575f2009-07-27 05:32:16 +0000326 const Section *fPDataSection =
Chris Lattnerf0144122009-07-28 03:13:23 +0000327 getObjFileLowering().getOrCreateSection(SectionName, false,
328 SectionKind::DataRel);
Sanjiv Gupta2bdf4902009-04-20 16:59:35 +0000329 SwitchToSection(fPDataSection);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000330
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000331 // Emit function frame label
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000332 O << PAN::getFrameLabel(CurrentFnName) << ":\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000333
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +0000334 const Type *RetType = F->getReturnType();
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000335 unsigned RetSize = 0;
336 if (RetType->getTypeID() != Type::VoidTyID)
Duncan Sands777d2302009-05-09 07:06:46 +0000337 RetSize = TD->getTypeAllocSize(RetType);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000338
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000339 //Emit function return value space
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000340 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
341 // we will need to avoid printing a global directive for Retval label
342 // in emitExternandGloblas.
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000343 if(RetSize > 0)
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000344 O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000345 else
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000346 O << PAN::getRetvalLabel(CurrentFnName) << ": \n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000347
348 // Emit variable to hold the space for function arguments
349 unsigned ArgSize = 0;
350 for (Function::const_arg_iterator argi = F->arg_begin(),
351 arge = F->arg_end(); argi != arge ; ++argi) {
352 const Type *Ty = argi->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000353 ArgSize += TD->getTypeAllocSize(Ty);
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000354 }
Sanjiv Gupta211f3622009-05-10 05:23:47 +0000355
356 O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n";
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000357
Sanjiv Gupta85be4082009-04-14 02:49:52 +0000358 // Emit temporary space
359 int TempSize = PTLI->GetTmpSize();
Chris Lattnerdc471462009-07-21 16:44:47 +0000360 if (TempSize > 0)
361 O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize << '\n';
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000362}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000363
Chris Lattnerdc471462009-07-21 16:44:47 +0000364void PIC16AsmPrinter::EmitIData(Module &M) {
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000365
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000366 // Print all IDATA sections.
Chris Lattnerf0144122009-07-28 03:13:23 +0000367 const std::vector<PIC16Section*> &IDATASections = PTOF->IDATASections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000368 for (unsigned i = 0; i < IDATASections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000369 O << "\n";
Sanjiv Gupta024e94c2009-07-06 18:07:06 +0000370 if (IDATASections[i]->S_->getName().find("llvm.") != std::string::npos)
371 continue;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000372 SwitchToSection(IDATASections[i]->S_);
373 std::vector<const GlobalVariable*> Items = IDATASections[i]->Items;
374 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000375 std::string Name = Mang->getMangledName(Items[j]);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000376 Constant *C = Items[j]->getInitializer();
377 int AddrSpace = Items[j]->getType()->getAddressSpace();
378 O << Name;
379 EmitGlobalConstant(C, AddrSpace);
380 }
381 }
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000382}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000383
Chris Lattnerdc471462009-07-21 16:44:47 +0000384void PIC16AsmPrinter::EmitUData(Module &M) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000385 const TargetData *TD = TM.getTargetData();
386
387 // Print all BSS sections.
Chris Lattnerf0144122009-07-28 03:13:23 +0000388 const std::vector<PIC16Section*> &BSSSections = PTOF->BSSSections;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000389 for (unsigned i = 0; i < BSSSections.size(); i++) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000390 O << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000391 SwitchToSection(BSSSections[i]->S_);
392 std::vector<const GlobalVariable*> Items = BSSSections[i]->Items;
393 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000394 std::string Name = Mang->getMangledName(Items[j]);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000395 Constant *C = Items[j]->getInitializer();
396 const Type *Ty = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000397 unsigned Size = TD->getTypeAllocSize(Ty);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000398
Chris Lattnerdc471462009-07-21 16:44:47 +0000399 O << Name << " RES " << Size << "\n";
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000400 }
401 }
402}
403
Chris Lattnerdc471462009-07-21 16:44:47 +0000404void PIC16AsmPrinter::EmitAutos(std::string FunctName) {
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000405 // Section names for all globals are already set.
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000406 const TargetData *TD = TM.getTargetData();
407
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000408 // Now print Autos section for this function.
409 std::string SectionName = PAN::getAutosSectionName(FunctName);
Chris Lattnerf0144122009-07-28 03:13:23 +0000410 const std::vector<PIC16Section*> &AutosSections = PTOF->AutosSections;
Sanjiv Gupta2364cfe2009-05-12 17:07:27 +0000411 for (unsigned i = 0; i < AutosSections.size(); i++) {
412 O << "\n";
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000413 if (AutosSections[i]->S_->getName() == SectionName) {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000414 // Set the printing status to true
415 AutosSections[i]->setPrintedStatus(true);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000416 SwitchToSection(AutosSections[i]->S_);
Chris Lattnerdc471462009-07-21 16:44:47 +0000417 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000418 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000419 std::string VarName = Mang->getMangledName(Items[j]);
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000420 Constant *C = Items[j]->getInitializer();
421 const Type *Ty = C->getType();
422 unsigned Size = TD->getTypeAllocSize(Ty);
423 // Emit memory reserve directive.
424 O << VarName << " RES " << Size << "\n";
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000425 }
Sanjiv Guptadd4694b2009-05-28 18:24:11 +0000426 break;
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000427 }
428 }
Sanjiv Guptaa57bc3b2009-05-22 13:58:45 +0000429}
430
Sanjiv Guptab157f252009-06-09 15:31:19 +0000431// Print autos that were not printed during the code printing of functions.
432// As the functions might themselves would have got deleted by the optimizer.
Chris Lattnerdc471462009-07-21 16:44:47 +0000433void PIC16AsmPrinter::EmitRemainingAutos() {
Sanjiv Guptab157f252009-06-09 15:31:19 +0000434 const TargetData *TD = TM.getTargetData();
435
436 // Now print Autos section for this function.
Chris Lattnerf0144122009-07-28 03:13:23 +0000437 std::vector <PIC16Section *>AutosSections = PTOF->AutosSections;
Sanjiv Guptab157f252009-06-09 15:31:19 +0000438 for (unsigned i = 0; i < AutosSections.size(); i++) {
439
440 // if the section is already printed then don't print again
441 if (AutosSections[i]->isPrinted())
442 continue;
443
444 // Set status as printed
445 AutosSections[i]->setPrintedStatus(true);
446
447 O << "\n";
448 SwitchToSection(AutosSections[i]->S_);
Chris Lattnerdc471462009-07-21 16:44:47 +0000449 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
Sanjiv Guptab157f252009-06-09 15:31:19 +0000450 for (unsigned j = 0; j < Items.size(); j++) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000451 std::string VarName = Mang->getMangledName(Items[j]);
Sanjiv Guptab157f252009-06-09 15:31:19 +0000452 Constant *C = Items[j]->getInitializer();
453 const Type *Ty = C->getType();
454 unsigned Size = TD->getTypeAllocSize(Ty);
455 // Emit memory reserve directive.
456 O << VarName << " RES " << Size << "\n";
457 }
458 }
459}
460
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000461
462extern "C" void LLVMInitializePIC16Target() {
463 // Register the targets
464 RegisterTargetMachine<PIC16TargetMachine> A(ThePIC16Target);
465 RegisterTargetMachine<CooperTargetMachine> B(TheCooperTarget);
466 RegisterAsmPrinter<PIC16AsmPrinter> C(ThePIC16Target);
467 RegisterAsmPrinter<PIC16AsmPrinter> D(TheCooperTarget);
468}