aboutsummaryrefslogtreecommitdiff
path: root/lib/Target/PIC16/PIC16DebugInfo.cpp
blob: faf4590b43ee60dffdb1917eca28b2be405579e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//===-- PIC16DebugInfo.cpp - Implementation for PIC16 Debug Information ======//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source 
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains the helper functions for representing debug information.
//
//===----------------------------------------------------------------------===//

#include "PIC16.h"
#include "PIC16DebugInfo.h" 
#include "llvm/GlobalVariable.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;

PIC16DbgInfo::~PIC16DbgInfo() {
  for(std::map<std::string, DISubprogram *>::iterator i = FunctNameMap.begin();
      i!=FunctNameMap.end(); i++) 
    delete i->second;
  FunctNameMap.clear();
}

void PIC16DbgInfo::PopulateDebugInfo(DIType Ty, unsigned short &TypeNo,
                                     bool &HasAux, int Aux[], 
                                     std::string &TypeName) {
  if (Ty.isBasicType(Ty.getTag())) {
    std::string Name = "";
    Ty.getName(Name);
    unsigned short BaseTy = GetTypeDebugNumber(Name);
    TypeNo = TypeNo << PIC16Dbg::S_BASIC;
    TypeNo = TypeNo | (0xffff & BaseTy);
  }
  else if (Ty.isDerivedType(Ty.getTag())) {
    switch(Ty.getTag())
    {
      case dwarf::DW_TAG_pointer_type:
        TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
        TypeNo = TypeNo | PIC16Dbg::DT_PTR;
        break;
      default:
        TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
    }
    DIType BaseType = DIDerivedType(Ty.getGV()).getTypeDerivedFrom();
    PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TypeName);
  }
  else if (Ty.isCompositeType(Ty.getTag())) {
    switch (Ty.getTag()) {
      case dwarf::DW_TAG_array_type: {
        DICompositeType CTy = DICompositeType(Ty.getGV());
        DIArray Elements = CTy.getTypeArray();
        unsigned short size = 1;
        unsigned short Dimension[4]={0,0,0,0};
        for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
          DIDescriptor Element = Elements.getElement(i);
          if (Element.getTag() == dwarf::DW_TAG_subrange_type) {
            TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
            TypeNo = TypeNo | PIC16Dbg::DT_ARY;
            DISubrange SubRange = DISubrange(Element.getGV());
            Dimension[i] = SubRange.getHi() - SubRange.getLo() + 1;
            // Each dimension is represented by 2 bytes starting at byte 9.
            Aux[8+i*2+0] = Dimension[i];
            Aux[8+i*2+1] = Dimension[i] >> 8;
            size = size * Dimension[i];
          }
        }
        HasAux = true;
        // In auxillary entry for array, 7th and 8th byte represent array size.
        Aux[6] = size;
        Aux[7] = size >> 8;
        DIType BaseType = CTy.getTypeDerivedFrom();
        PopulateDebugInfo(BaseType, TypeNo, HasAux, Aux, TypeName);

        break;
      }
      case dwarf:: DW_TAG_union_type:
      case dwarf::DW_TAG_structure_type: {
        DICompositeType CTy = DICompositeType(Ty.getGV());
        TypeNo = TypeNo << PIC16Dbg::S_BASIC;
        if (Ty.getTag() == dwarf::DW_TAG_structure_type)
          TypeNo = TypeNo | PIC16Dbg::T_STRUCT;
        else
          TypeNo = TypeNo | PIC16Dbg::T_UNION;
        CTy.getName(TypeName);
        unsigned size = CTy.getSizeInBits()/8;
        // 7th and 8th byte represent size.   
        HasAux = true;
        Aux[6] = size;
        Aux[7] = size >> 8;
        break;
      }
      case dwarf::DW_TAG_enumeration_type: {
        TypeNo = TypeNo << PIC16Dbg::S_BASIC;
        TypeNo = TypeNo | PIC16Dbg::T_ENUM;
        break;
      }
      default:
        TypeNo = TypeNo << PIC16Dbg::S_DERIVED;
    }
  }
  else {
    TypeNo = PIC16Dbg::T_NULL;
    HasAux = false;
  }
  return;
}


unsigned PIC16DbgInfo::GetTypeDebugNumber(std::string &type)  {
  if (type == "char")
    return PIC16Dbg::T_CHAR;
  else if (type == "short")
    return PIC16Dbg::T_SHORT;
  else if (type == "int")
    return PIC16Dbg::T_INT;
  else if (type == "long")
    return PIC16Dbg::T_LONG;
  else if (type == "unsigned char")
    return PIC16Dbg::T_UCHAR;
  else if (type == "unsigned short")
    return PIC16Dbg::T_USHORT;
  else if (type == "unsigned int")
    return PIC16Dbg::T_UINT;
  else if (type == "unsigned long")
    return PIC16Dbg::T_ULONG;
  else
    return 0;
}

short PIC16DbgInfo::getClass(DIGlobalVariable DIGV) {
  short ClassNo;
  if (PAN::isLocalName(DIGV.getGlobal()->getName())) {
    // Generating C_AUTO here fails due to error in linker. Change it once
    // linker is fixed.
    ClassNo = PIC16Dbg::C_STAT;
  }
  else if (DIGV.isLocalToUnit())
    ClassNo = PIC16Dbg::C_STAT;
  else
    ClassNo = PIC16Dbg::C_EXT;
  return ClassNo;
}

void PIC16DbgInfo::PopulateFunctsDI(Module &M) {
  GlobalVariable *Root = M.getGlobalVariable("llvm.dbg.subprograms");
  if (!Root)
    return;
  Constant *RootC = cast<Constant>(*Root->use_begin());

  for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
       UI != UE; ++UI)
    for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
         UUI != UUE; ++UUI) {
      GlobalVariable *GVSP = cast<GlobalVariable>(*UUI);
      DISubprogram *SP = new DISubprogram(GVSP);
      std::string Name;
      SP->getLinkageName(Name);
      FunctNameMap[Name] = SP; 
    }
  return;
}

DISubprogram* PIC16DbgInfo::getFunctDI(std::string FunctName) {
  return FunctNameMap[FunctName];
}

void PIC16DbgInfo::EmitFunctBeginDI(const Function *F) {
  std::string FunctName = F->getName();
  DISubprogram *SP = getFunctDI(FunctName);
  if (SP) {
    std::string FunctBeginSym = ".bf." + FunctName;
    std::string BlockBeginSym = ".bb." + FunctName;

    int FunctBeginLine = SP->getLineNumber();
    int BFAux[PIC16Dbg::AuxSize] = {0};
    BFAux[4] = FunctBeginLine;
    BFAux[5] = FunctBeginLine >> 8;
    // Emit debug directives for beginning of function.
    EmitSymbol(FunctBeginSym, PIC16Dbg::C_FCN);
    EmitAuxEntry(FunctBeginSym, BFAux, PIC16Dbg::AuxSize);
    EmitSymbol(BlockBeginSym, PIC16Dbg::C_BLOCK);
    EmitAuxEntry(BlockBeginSym, BFAux, PIC16Dbg::AuxSize);
  }
}

void PIC16DbgInfo::EmitFunctEndDI(const Function *F, unsigned Line) {
  std::string FunctName = F->getName();
  DISubprogram *SP = getFunctDI(FunctName);
  if (SP) {
    std::string FunctEndSym = ".ef." + FunctName;
    std::string BlockEndSym = ".eb." + FunctName;

    // Emit debug directives for end of function.
    EmitSymbol(BlockEndSym, PIC16Dbg::C_BLOCK);
    int EFAux[PIC16Dbg::AuxSize] = {0};
    // 5th and 6th byte stand for line number.
    EFAux[4] = Line;
    EFAux[5] = Line >> 8;
    EmitAuxEntry(BlockEndSym, EFAux, PIC16Dbg::AuxSize);
    EmitSymbol(FunctEndSym, PIC16Dbg::C_FCN);
    EmitAuxEntry(FunctEndSym, EFAux, PIC16Dbg::AuxSize);
  }
}

/// EmitAuxEntry - Emit Auxiliary debug information.
///
void PIC16DbgInfo::EmitAuxEntry(const std::string VarName, int Aux[], int num) {
  O << "\n\t.dim " << VarName << ", 1" ;
  for (int i = 0; i<num; i++)
    O << "," << Aux[i];
}

void PIC16DbgInfo::EmitSymbol(std::string Name, int Class) {
  O << "\n\t" << ".def "<< Name << ", debug, class = " << Class;
}

void PIC16DbgInfo::EmitVarDebugInfo(Module &M) {
  GlobalVariable *Root = M.getGlobalVariable("llvm.dbg.global_variables");
  if (!Root)
    return;

  Constant *RootC = cast<Constant>(*Root->use_begin());
  for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end();
       UI != UE; ++UI) {
    for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end();
         UUI != UUE; ++UUI) {
      DIGlobalVariable DIGV(cast<GlobalVariable>(*UUI));
      DIType Ty = DIGV.getType();
      unsigned short TypeNo = 0;
      bool HasAux = false;
      int Aux[PIC16Dbg::AuxSize] = { 0 };
      std::string TypeName = "";
      std::string VarName = TAI->getGlobalPrefix()+DIGV.getGlobal()->getName();
      PopulateDebugInfo(Ty, TypeNo, HasAux, Aux, TypeName);
      // Emit debug info only if type information is availaible.
      if (TypeNo != PIC16Dbg::T_NULL) {
        O << "\n\t.type " << VarName << ", " << TypeNo;
        short ClassNo = getClass(DIGV);
        O << "\n\t.class " << VarName << ", " << ClassNo;
        if (HasAux) {
          if (TypeName != "") {
           // Emit debug info for structure and union objects after
           // .dim directive supports structure/union tag name in aux entry.
           /* O << "\n\t.dim " << VarName << ", 1," << TypeName;
            for (int i = 0; i<PIC16Dbg::AuxSize; i++)
              O << "," << Aux[i];*/
         }
          else {
            EmitAuxEntry(VarName, Aux, PIC16Dbg::AuxSize);
          }
        }
      }
    }
  }
  O << "\n";
}

void PIC16DbgInfo::EmitFileDirective(Module &M) {
  GlobalVariable *CU = M.getNamedGlobal("llvm.dbg.compile_unit");
  if (CU) {
    DICompileUnit DIUnit(CU);
    std::string Dir, FN;
    std::string File = DIUnit.getDirectory(Dir) + "/" + DIUnit.getFilename(FN);
    O << "\n\t.file\t\"" << File << "\"\n" ;
    CurFile = File;
  }
}

void PIC16DbgInfo::EmitFileDirective(const Function *F) {
  std::string FunctName = F->getName();
  DISubprogram *SP = getFunctDI(FunctName);
  if (SP) {
    std::string Dir, FN;
    DICompileUnit CU = SP->getCompileUnit();
    std::string File = CU.getDirectory(Dir) + "/" + CU.getFilename(FN);
    if ( File != CurFile) {
      EmitEOF();
      O << "\n\t.file\t\"" << File << "\"\n" ;
      CurFile = File;
    }
  }
}

void PIC16DbgInfo::EmitEOF() {
  if (CurFile != "")
    O << "\n\t.EOF";
}