summaryrefslogtreecommitdiff
path: root/utils/TableGen/SimpleInstrSelEmitter.cpp
blob: 5f4d20854d072eedde2b0a70aee80f2818f82012 (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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//===- SimpleInstrSelEmitter.cpp - Generate a Simple Instruction Selector ------------===//
// 
//                     The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
// 
//===----------------------------------------------------------------------===//
//
// This tablegen backend is responsible for emitting an instruction selector
// 
//
//===----------------------------------------------------------------------===//
#include "InstrInfoEmitter.h"
#include "SimpleInstrSelEmitter.h"
#include "CodeGenWrappers.h"
#include "Record.h"
#include "Support/Debug.h"
#include "Support/StringExtras.h"
#include <set>


#include "Record.h"
#include "Support/CommandLine.h"
#include "Support/Signals.h"
#include "Support/FileUtilities.h"
#include "CodeEmitterGen.h"
#include "RegisterInfoEmitter.h"
#include "InstrInfoEmitter.h"
#include "InstrSelectorEmitter.h"
#include "SimpleInstrSelEmitter.h"
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <vector>

namespace llvm {

  std::string FnDecs;

// run - Emit the main instruction description records for the target...
void SimpleInstrSelEmitter::run(std::ostream &OS) {


  //  EmitSourceFileHeader("Mark's Instruction Selector for the X86 target", OS);


//   OS << "#include \"llvm/CodeGen/MachineInstrBuilder.h\"\n";

//   OS << "#include \"llvm/Constants.h\"\n";
//   OS << "#include \"llvm/DerivedTypes.h\"\n";
//   OS << "#include \"llvm/Function.h\"\n";
//   OS << "#include \"llvm/Instructions.h\"\n";
//   OS << "#include \"llvm/IntrinsicLowering.h\"\n";
//   OS << "#include \"llvm/Pass.h\"\n";
//   OS << "#include \"llvm/CodeGen/MachineConstantPool.h\"\n";
//   OS << "#include \"llvm/CodeGen/MachineFrameInfo.h\"\n";
//   OS << "#include \"llvm/CodeGen/MachineFunction.h\"\n";
//   OS << "#include \"llvm/CodeGen/MachineInstrBuilder.h\"\n";
//   OS << "#include \"llvm/CodeGen/SSARegMap.h\"\n";
//   OS << "#include \"llvm/Target/MRegisterInfo.h\"\n";
//   OS << "#include \"llvm/Target/TargetMachine.h\"\n";
//   OS << "#include \"llvm/Support/InstVisitor.h\"\n";

//  OS << "using namespace llvm;\n\n";


  FnDecs = "";

  // for each InstrClass

  std::vector<Record*> Recs = Records.getAllDerivedDefinitions("InstrClass");
  for (unsigned i = 0, e = Recs.size(); i != e; ++i) {
    std::string InstrClassName = Recs[i]->getName();
    OS << "// Generate BMI instructions for " << InstrClassName << "\n\n";
    OS << "void ISel::visit";
    OS << Recs[i]->getValueAsString("FunctionName");
    OS << "(" <<  Recs[i]->getValueAsString("InstructionName") << " &I)\n{" << "\n";
    // for each supported InstrSubclass

    OS << spacing() << "unsigned DestReg = getReg(I);\n";
    OS << spacing() << "unsigned Op0Reg  = getReg(I.getOperand(0));\n";
    OS << spacing() << "unsigned Op1Reg  = getReg(I.getOperand(1));\n";
    OS << spacing() << "Value   *Op0Val  = I.getOperand(0);\n";
    OS << spacing() << "Value   *Op1Val  = I.getOperand(1);\n";

    OS << spacing() << "MachineBasicBlock::iterator IP = BB->end();\n";

    OS << std::endl;

    ListInit *SupportedSubclasses = Recs[i]->getValueAsListInit("Supports");

    //OS << spacing() << InstrClassName << "Prep();" << "\n";
    //FnDecs += "void ISel::" + InstrClassName + "Prep() {\n\n}\n\n";

    std::vector<std::string> vi;

    // generate subclasses nested switch statements
    InstrSubclasses(OS, InstrClassName, InstrClassName, SupportedSubclasses, vi, 0);

    //OS << spacing() << InstrClassName << "Post();\n";
    //FnDecs += "void ISel::" + InstrClassName + "Post() {\n\n}\n\n";

    OS << "}\n";
    OS << "\n\n\n";

  } // for each instrclass

  //  OS << "} //namespace\n";


#if 0
  // print out function stubs
  OS << "\n\n\n//Functions\n\n" << FnDecs;

  // print out getsubclass() definitions
  std::vector<Record*> SubclassColRec = Records.getAllDerivedDefinitions("InstrSubclassCollection");
  for (unsigned j=0, m=SubclassColRec.getSize(); j!=m; ++j) {
    std::string SubclassName = SubclassColRec[j]->getName();
    FnDecs += "unsigned ISel::get" + SubclassName + "() {\n\n";

    ListInit* list = dynamic_cast<ListInit*>(SubclassColRec[j].getValueAsListInit("List"));
    
    for (unsigned k=0; n=list.getSize(); k!=n; ++k) {

    FnDecs += "}\n\n";
  }
#endif

} //run

  

// find instructions that match all the subclasses (only support for 1 now)
Record* SimpleInstrSelEmitter::findInstruction(std::ostream &OS, std::string cl, std::vector<std::string>& vi) {
  std::vector<Record*> Recs = Records.getAllDerivedDefinitions("TargInstrSet");

  for (unsigned i = 0, e = Recs.size(); i != e; ++i) {
    Record* thisClass = Recs[i]->getValueAsDef("Class");

    if (thisClass->getName() == cl) {

      // get the Subclasses this supports
      ListInit* SubclassList = Recs[i]->getValueAsListInit("List");

      bool Match = true;

      if (SubclassList->getSize() != vi.size())
	Match = false;
      
      // match the instruction's supported subclasses with the subclasses we are looking for

      for (unsigned j=0, f=SubclassList->getSize(); j!=f; ++j) {
	DefInit* SubclassDef = dynamic_cast<DefInit*>(SubclassList->getElement(j));
	Record* thisSubclass = SubclassDef->getDef();

	std::string searchingFor = vi[j];

	if (thisSubclass->getName() != searchingFor) {
	  Match = false;
	}

      } // for each subclass list

      if (Match == true) { return Recs[i]; }
  
    } //if instrclass matches

  } // for all instructions

  // if no instructions found, return NULL
  return NULL;

} //findInstruction
  


Record* SimpleInstrSelEmitter::findRegister(std::ostream &OS, std::string regname) {
  std::vector<Record*> Recs = Records.getAllDerivedDefinitions("Register");

  for (unsigned i = 0, e = Recs.size(); i != e; ++i) {
    Record* thisReg = Recs[i];

    if (thisReg->getName() == regname) return Recs[i];
  }
  
  return NULL;

}

// handle "::" and "+" etc
std::string SimpleInstrSelEmitter::formatRegister(std::ostream &OS, std::string regname) {
  std::string Reg;
  std::string suffix;

  int x = std::strcspn(regname.c_str(),"+-");

  // operate on text before "+" or "-", append it back at the end
  Reg = regname.substr(0,x);
  suffix = regname.substr(x,regname.length());

  unsigned int y = std::strcspn(Reg.c_str(),":");

  if (y == Reg.length()) { // does not contain "::"
    
    Record* RegRec = findRegister(OS,Reg);

    assert(RegRec && "Register not found!");

    if (RegRec->getValueAsString("Namespace") != "Virtual") {
      Reg = RegRec->getValueAsString("Namespace") + "::" + RegRec->getName();
    } else {
      Reg = RegRec->getName();
    }
  } // regular case

  // append + or - at the end again (i.e. X86::EAX+1)
  Reg = Reg + suffix;

  return Reg;
}


// take information in the instruction class and generate the correct BMI call
void SimpleInstrSelEmitter::generateBMIcall(std::ostream &OS, std::string MBB, std::string IP, std::string Opcode, int NumOperands, ListInit &instroperands, ListInit &operands) {

  // find Destination Register
  StringInit* DestRegStr = dynamic_cast<StringInit*>(operands.getElement(0));
  std::string DestReg = formatRegister(OS,DestRegStr->getValue());

  OS << "BuildMI(";
  OS << MBB << ", ";
  OS << IP << ", ";
  OS << Opcode << ", ";
  OS << NumOperands;

  if (DestReg != "Pseudo") {
    OS << ", " << DestReg << ")";
  } else {
    OS << ")";
  }
  
  // handle the .add stuff
  for (unsigned i=0, e=instroperands.getSize(); i!=e; ++i) {
    DefInit* OpDef = dynamic_cast<DefInit*>(instroperands.getElement(i));
    StringInit* RegStr = dynamic_cast<StringInit*>(operands.getElement(i+1));

    Record* Op = OpDef->getDef();

    std::string opstr = Op->getValueAsString("Name");

    std::string regname;

    if (opstr == "Register") {
      regname = formatRegister(OS,RegStr->getValue());
    } else {
      regname = RegStr->getValue();
    }

    OS << ".add" << opstr << "(" << regname << ")";
  }
  
  OS << ";\n";
  
} //generateBMIcall

  
 std::string SimpleInstrSelEmitter::spacing() {
   return globalSpacing;
 }

 std::string SimpleInstrSelEmitter::addspacing() {
   globalSpacing += "  ";
   return globalSpacing;
 }

 std::string SimpleInstrSelEmitter::remspacing() {
   globalSpacing = globalSpacing.substr(0,globalSpacing.length()-2);
   return globalSpacing;
 }


 // recursively print out the subclasses of an instruction
 //
 void SimpleInstrSelEmitter::InstrSubclasses(std::ostream &OS, std::string prefix, std::string InstrClassName, ListInit* SupportedSubclasses, std::vector<std::string>& vi, unsigned depth) {

   
   if (depth >= SupportedSubclasses->getSize()) {
      return;
   }

   // get the subclass collection
   
   DefInit* InstrSubclassColl = dynamic_cast<DefInit*>(SupportedSubclasses->getElement(depth));

   Record* InstrSubclassRec = InstrSubclassColl->getDef();

   std::string SubclassName = InstrSubclassRec->getName();
   
   
   if (InstrSubclassRec->getValueAsString("PreCode") != "") {
     //	  OS << spacing() << prefix << "_" << Subclass->getName() << "_Prep();\n";
     OS << spacing() << InstrSubclassRec->getValueAsString("PreCode") << "\n\n";
   }
   
   
   OS << spacing() << "// Looping through " << SubclassName << "\n";
   
   OS << spacing() << "switch (" <<  SubclassName <<") {\n";
   addspacing();
   
   ListInit* SubclassList = InstrSubclassRec->getValueAsListInit("List");
   
   for (unsigned k=0, g = SubclassList->getSize(); k!=g; ++k) {
     
     DefInit* SubclassDef = dynamic_cast<DefInit*>(SubclassList->getElement(k));
     
     Record* Subclass = SubclassDef->getDef();
     
     OS << spacing() << "// " << prefix << "_" << Subclass->getName() << "\n";
     OS << spacing() << "case " << Subclass->getName() << ":\n";
     addspacing();
     OS << spacing() << "{\n";
     
     
     vi.push_back(Subclass->getName());
     
     // go down hierarchy
     InstrSubclasses(OS, prefix + "_" + Subclass->getName(), InstrClassName, SupportedSubclasses, vi, depth+1);
     
     // find the record that matches this
     Record *theInstructionSet = findInstruction(OS, InstrClassName, vi);
     
     // only print out the assertion if this is a leaf
     if ( (theInstructionSet == NULL) && (depth == (SupportedSubclasses->getSize() - 1)) ) {
       
       OS << spacing() << "assert(0 && \"No instructions defined for " << InstrClassName << " instructions of subclasses " << prefix << "_" << Subclass->getName() << "!\");" << "\n";
       
     } else if (theInstructionSet != NULL) {
       
       if (theInstructionSet->getValueAsString("PreCode") != "") {
	 OS << spacing() << theInstructionSet->getValueAsString("PreCode") << "\n\n";
       }
       
       ListInit *theInstructions = theInstructionSet->getValueAsListInit("Instructions");
       
       ListInit *registerlists = theInstructionSet->getValueAsListInit("Operands"); // not necessarily registers anymore, but the name will stay for now
       
       for (unsigned l=0, h=theInstructions->getSize(); l!=h; ++l) {
	 
	 DefInit *theInstructionDef = dynamic_cast<DefInit*>(theInstructions->getElement(l));
	 Record *theInstruction = theInstructionDef->getDef();
	 
	 ListInit *operands = theInstruction->getValueAsListInit("Params");
	 
	 OS << spacing();
	 
	 ListInit* registers = dynamic_cast<ListInit*>(registerlists->getElement(l));
	 
	 // handle virtual instructions here before going to generateBMIcall
	 
	 if (theInstruction->getValueAsString("Namespace") == "Virtual") {
	   
	   // create reg for different sizes
	   std::string Instr = theInstruction->getName();
	   StringInit* DestRegInit = dynamic_cast<StringInit*>(registers->getElement(0));
	   std::string DestReg = DestRegInit->getValue();
	   std::string theType;
	   
	   if (Instr == "NullInstruction") { } // do nothing
	   else if (Instr == "CreateRegByte")
	     OS << "unsigned " << DestReg << " = makeAnotherReg(Type::SByteTy);\n";
	   else if (Instr == "CreateRegShort")
	     OS << "unsigned " << DestReg << " = makeAnotherReg(Type::SShortTy);\n";
	   else if (Instr == "CreateRegInt")
	     OS << "unsigned " << DestReg << " = makeAnotherReg(Type::SIntTy);\n";
	   else if (Instr == "CreateRegLong")
	     OS << "unsigned " << DestReg << " = makeAnotherReg(Type::SLongTy);\n";
	   else if (Instr == "CreateRegUByte")
	     OS << "unsigned " << DestReg << " = makeAnotherReg(Type::UByteTy);\n";
	   else if (Instr == "CreateRegUShort")
	     OS << "unsigned " << DestReg << " = makeAnotherReg(Type::UShortTy);\n";
	   else if (Instr == "CreateRegUInt")
	     OS << "unsigned " << DestReg << " = makeAnotherReg(Type::UIntTy);\n";
	   else if (Instr == "CreateRegULong") 
	     OS << "unsigned " << DestReg << " = makeAnotherReg(Type::ULongTy);\n";
	   else if (Instr == "CreateRegFloat") 
	     OS << "unsigned " << DestReg << " = makeAnotherReg(Type::FloatTy);\n";
	   else if (Instr == "CreateRegDouble") 
	     OS << "unsigned " << DestReg << " = makeAnotherReg(Type::DoubleTy);\n";
	   else if (Instr == "CreateRegPointer") 
	     OS << "unsigned " << DestReg << " = makeAnotherReg(Type::PointerTy_;\n";
	   else 
	     OS << "unsigned " << DestReg << " = makeAnotherReg(Type::SByteTy);\n"; // create a byte by default
	   
	   
	 } else {
	   std::string InstrName;
	   
	   if (theInstruction->getValueAsString("Namespace") != "Virtual") {
	     InstrName = theInstruction->getValueAsString("Namespace") + "::" + theInstruction->getValueAsString("Name");
	   } else {
	     // shouldn't ever happen, virtual instrs should be caught before this
	     InstrName = theInstruction->getValueAsString("Name");
	   }
	   
	   generateBMIcall(OS, "*BB","IP",InstrName,theInstruction->getValueAsInt("NumOperands"),*operands,*registers);
	 }
	 
       }
       
       if (theInstructionSet->getValueAsString("PostCode") != "") {
	 OS << spacing() << theInstructionSet->getValueAsString("PostCode") << "\n\n";
       }
       
     }
     
     
     
     if (InstrSubclassRec->getValueAsString("PostCode") != "") {
       //OS << spacing() << "// " << prefix << "_" << Subclass->getName() << "_Prep();\n";
       OS << spacing() << InstrSubclassRec->getValueAsString("PostCode") << "\n\n";
     }
     
     
     OS << spacing() << "break;\n";

     OS << spacing() << "}\n\n";
     
     remspacing();
     
     vi.pop_back();
   }
   
   // provide a default case for the switch
   
   OS << spacing() << "default:\n";
   OS << spacing() << "  assert(0 && \"No instructions defined for " << InstrClassName << " instructions of subclasses " << prefix << "_" << SubclassName << "!\");" << "\n";
   OS << spacing() << "  break;\n\n";
   
   remspacing();
   OS << spacing() << "}\n";
   
 }


 // ret br switch invoke unwind
 // add sub mul div rem setcc (eq ne lt gt le ge)
 // and or xor sbl sbr
 // malloc free alloca load store
 // getelementptr phi cast call vanext vaarg
 
} // End llvm namespace