aboutsummaryrefslogtreecommitdiff
path: root/lib/Target/PIC16/PIC16Passes/PIC16Cloner.cpp
blob: 79f49465427f9a04f72df273ceb9b4e5a67afae3 (plain)
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
//===-- PIC16Cloner.cpp - PIC16 LLVM Cloner for shared functions -*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains code to clone all functions that are shared between
// the main line code (ML) and interrupt line code (IL). It clones all such
// shared functions and their automatic global vars by adding the .IL suffix. 
//
// This pass is supposed to be run on the linked .bc module.
// It traveses the module call graph twice. Once starting from the main function
// and marking each reached function as "ML". Again, starting from the ISR
// and cloning any reachable function that was marked as "ML". After cloning
// the function, it remaps all the call sites in IL functions to call the
// cloned functions. 
//===----------------------------------------------------------------------===//

#include "llvm/Analysis/CallGraph.h"
#include "llvm/Pass.h"
#include "llvm/Module.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "PIC16Cloner.h"
#include "../PIC16ABINames.h"
#include <vector>

using namespace llvm;
using std::vector;
using std::string;
using std::map;

namespace llvm {
  char PIC16Cloner::ID = 0;

  ModulePass *createPIC16ClonerPass() { return new PIC16Cloner(); }
}

// We currently intend to run these passes in opt, which does not have any
// diagnostic support. So use these functions for now. In future
// we will probably write our own driver tool.
//
void PIC16Cloner::reportError(string ErrorString) {
  errs() << "ERROR : " << ErrorString << "\n";
  exit(1);
}

void PIC16Cloner::
reportError (string ErrorString, vector<string> &Values) {
  unsigned ValCount = Values.size();
  string TargetString;
  for (unsigned i=0; i<ValCount; ++i) {
    TargetString = "%";
    TargetString += ((char)i + '0');
    ErrorString.replace(ErrorString.find(TargetString), TargetString.length(), 
                        Values[i]);
  }
  errs() << "ERROR : " << ErrorString << "\n";
  exit(1);
}


// Entry point
//
bool PIC16Cloner::runOnModule(Module &M) {
   CallGraph &CG = getAnalysis<CallGraph>();
   // Initially record that no interrupt has been found
   foundISR = false;

   // First mark the MainLine. 
   for (CallGraph::iterator it = CG.begin() ; it != CG.end(); it++)
   {
     // External calling node doesn't have any function associated 
     // with it
     if (!it->first)
       continue;
     
     if (it->first->getName().str() == "main") {
       // See if the main itself is interrupt function then report an error.
       if (PAN::isISR(it->first->getSection()))
          reportError("Function 'main' can't be interrupt function");
       else  { 
         // Function main itself is MainLine function.
         it->second->getFunction()->setSection("ML");
         // mark the hierarchy 
         markCallGraph(it->second, "ML");
         // MainLine has been marked now break ; don't search any further.
         break;
       }
     } 
   }

   // When the MainLine has been marked only then mark the InterruptLine.
   for (CallGraph::iterator it = CG.begin() ; it != CG.end(); it++)
   {
     // External calling node doesn't have any function associated 
     // with it
     if (!it->first)
       continue;

     if (PAN::isISR(it->first->getSection())) {

       if (foundISR)
         reportError("More than one interrupt functions defined in the module");

       foundISR = true;
       
       markCallGraph(it->second, "IL");
       // InterruptLine handled now break; don't search any further.
       break;
     }
   } 
   
  return true;
}

// For PIC16, automatic variables of a function are emitted as globals.
// Clone the auto variables of a function  and put them in ValueMap, 
// this ValueMap will be used while
// Cloning the code of function itself.
//
void PIC16Cloner::CloneAutos(Function *F) {
  // We'll need to update module's globals list as well. So keep a reference
  // handy.
  Module *M = F->getParent();
  Module::GlobalListType &Globals = M->getGlobalList();

  // Clear the leftovers in ValueMap by any previous cloning.
  ValueMap.clear();

  // Find the auto globls for this function and clone them, and put them
  // in ValueMap.
  std::string FnName = F->getName().str();
  std::string VarName, ClonedVarName;
  for (Module::global_iterator I = M->global_begin(), E = M->global_end();
       I != E; ++I) {
    VarName = I->getName().str();
    if (PAN::isLocalToFunc(FnName, VarName)) {
      // Auto variable for current function found. Clone it.
      GlobalVariable *GV = I;

      const Type *InitTy = GV->getInitializer()->getType();
      GlobalVariable *ClonedGV = 
        new GlobalVariable(InitTy, false, GV->getLinkage(), 
                           GV->getInitializer());
      ClonedGV->setName(PAN::getCloneVarName(FnName, VarName));
      // Add these new globals to module's globals list.
      Globals.push_back(ClonedGV);
 
      // Update ValueMap.
      ValueMap[GV] = ClonedGV;
     }
  }
}


// Mark all reachable functions from the given node, with the given mark.
//
void PIC16Cloner::markCallGraph(CallGraphNode *CGN, string StringMark) {
  string AlternateMark;
  if (StringMark == "ML")
    AlternateMark = "IL";
  else
    AlternateMark = "ML";

  // Mark all the called functions
  for(CallGraphNode::iterator cgn_it = CGN->begin(); 
              cgn_it != CGN->end(); ++cgn_it) {
     Function *CalledF = cgn_it->second->getFunction();

     // If calling an external function then CallGraphNode
     // will not be associated with any function.
     if (!CalledF)
       continue;
  
     // Issue diagnostic if interrupt function is being called.
     if (PAN::isISR(CalledF->getSection())) {
       vector<string> Values;
       Values.push_back(CalledF->getName().str());
       reportError("Interrupt function (%0) can't be called", Values); 
     }

     // Has already been mark 
     if (CalledF->getSection().find(StringMark) != string::npos) {
        // Should we do anything here?
     } else {
       // Mark now
       if (CalledF->getSection().find(AlternateMark) != string::npos) {
         // Function is alternatively marked. It should be a shared one.
         // Create IL copy. Passing called function as first argument
         // and the caller as the second argument.

         // Before making IL copy, first ensure that this function has a 
         // body. If the function does have a body. It can't be cloned.
         // Such a case may occur when the function has been declarated
         // in the C source code but its body exists in assembly file.
         if (!CalledF->isDeclaration()) {
           // FIXME: Not implemented yet. Clone the function here.
         }
       } else {
         // Function is not marked. It should be marked now.
         CalledF->setSection(StringMark);
       }
     }
     // Before going any further mark all the called function by current
     // function.
     markCallGraph(cgn_it->second ,StringMark);
  } // end of loop of all called functions.
}