blob: 2505b111f1e80ac5f83058aaa1807f77f4d1c885 [file] [log] [blame]
Benjamin Kramer5814fef2009-10-15 19:46:34 +00001//===-- PIC16Section.cpp - PIC16 Section ----------- --------------------===//
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#include "PIC16.h"
11#include "PIC16ABINames.h"
12#include "PIC16Section.h"
13#include "llvm/MC/MCContext.h"
14#include "llvm/Support/raw_ostream.h"
15using namespace llvm;
16
17
18// This is the only way to create a PIC16Section. Sections created here
19// do not need to be explicitly deleted as they are managed by auto_ptrs.
Benjamin Kramer91b1d8f2010-03-30 13:28:42 +000020PIC16Section *PIC16Section::Create(StringRef Name, PIC16SectionType Ty,
21 StringRef Address, int Color,
22 MCContext &Ctx) {
Benjamin Kramer5814fef2009-10-15 19:46:34 +000023
24 /// Determine the internal SectionKind info.
25 /// Users of PIC16Section class should not need to know the internal
26 /// SectionKind. They should work only with PIC16SectionType.
27 ///
28 /// PIC16 Terminology for section kinds is as below.
29 /// UDATA - BSS
30 /// IDATA - initialized data (equiv to Metadata)
31 /// ROMDATA - ReadOnly.
32 /// UDATA_OVR - Sections that can be overlaid. Section of such type is
33 /// used to contain function autos an frame. We can think of
34 /// it as equiv to llvm ThreadBSS)
35 /// UDATA_SHR - Shared RAM. Memory area that is mapped to all banks.
36
37 SectionKind K;
38 switch (Ty) {
39 default: llvm_unreachable ("can not create unknown section type");
40 case UDATA_OVR: {
41 K = SectionKind::getThreadBSS();
42 break;
43 }
44 case UDATA_SHR:
45 case UDATA: {
46 K = SectionKind::getBSS();
47 break;
48 }
49 case ROMDATA:
50 case IDATA: {
51 K = SectionKind::getMetadata();
52 break;
53 }
54 case CODE: {
55 K = SectionKind::getText();
56 break;
57 }
58
59 }
60
Benjamin Kramer91b1d8f2010-03-30 13:28:42 +000061 // Copy strings into context allocated memory so they get free'd when the
62 // context is destroyed.
63 char *NameCopy = static_cast<char*>(Ctx.Allocate(Name.size(), 1));
64 memcpy(NameCopy, Name.data(), Name.size());
65 char *AddressCopy = static_cast<char*>(Ctx.Allocate(Address.size(), 1));
66 memcpy(AddressCopy, Address.data(), Address.size());
67
Benjamin Kramer5814fef2009-10-15 19:46:34 +000068 // Create the Section.
Benjamin Kramer91b1d8f2010-03-30 13:28:42 +000069 PIC16Section *S =
70 new (Ctx) PIC16Section(StringRef(NameCopy, Name.size()), K,
71 StringRef(AddressCopy, Address.size()), Color);
Benjamin Kramer5814fef2009-10-15 19:46:34 +000072 S->T = Ty;
73 return S;
74}
75
76// A generic way to print all types of sections.
77void PIC16Section::PrintSwitchToSection(const MCAsmInfo &MAI,
78 raw_ostream &OS) const {
Sanjiv Gupta4e4bba52009-10-21 10:42:44 +000079
80 // If the section is overlaid(i.e. it has a color), print overlay name for
81 // it. Otherwise print its normal name.
82 if (Color != -1)
83 OS << PAN::getOverlayName(getName(), Color) << '\t';
84 else
85 OS << getName() << '\t';
Benjamin Kramer5814fef2009-10-15 19:46:34 +000086
87 // Print type.
88 switch (getType()) {
89 default : llvm_unreachable ("unknown section type");
90 case UDATA: OS << "UDATA"; break;
91 case IDATA: OS << "IDATA"; break;
92 case ROMDATA: OS << "ROMDATA"; break;
93 case UDATA_SHR: OS << "UDATA_SHR"; break;
94 case UDATA_OVR: OS << "UDATA_OVR"; break;
95 case CODE: OS << "CODE"; break;
96 }
97
98 OS << '\t';
99
100 // Print Address.
101 OS << Address;
102
103 OS << '\n';
104}