LLVM API Documentation

PIC16.h

Go to the documentation of this file.
00001 //===-- PIC16.h - Top-level interface for PIC16 representation --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source 
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the entry points for global functions defined in 
00011 // the LLVM PIC16 back-end.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_TARGET_PIC16_H
00016 #define LLVM_TARGET_PIC16_H
00017 
00018 #include "llvm/Support/ErrorHandling.h"
00019 #include "llvm/Target/TargetMachine.h"
00020 #include <iosfwd>
00021 #include <cassert>
00022 #include <sstream>
00023 #include <cstring>
00024 #include <string>
00025 
00026 namespace llvm {
00027   class PIC16TargetMachine;
00028   class FunctionPass;
00029   class MachineCodeEmitter;
00030   class formatted_raw_ostream;
00031 
00032 namespace PIC16CC {
00033   enum CondCodes {
00034     EQ,
00035     NE,
00036     LT,
00037     LE,
00038     GT,
00039     GE,
00040     ULT,
00041     UGT,
00042     ULE,
00043     UGE
00044   };
00045 }
00046   // A Central class to manage all ABI naming conventions.
00047   // PAN - [P]ic16 [A]BI [N]ames
00048   class PAN {
00049     public:
00050     // Map the name of the symbol to its section name.
00051     // Current ABI:
00052     // -----------------------------------------------------
00053     // ALL Names are prefixed with the symobl '@'.
00054     // ------------------------------------------------------
00055     // Global variables do not have any '.' in their names.
00056     // These are maily function names and global variable names.
00057     // Example - @foo,  @i
00058     // -------------------------------------------------------
00059     // Functions and auto variables.
00060     // Names are mangled as <prefix><funcname>.<tag>.<varname>
00061     // Where <prefix> is '@' and <tag> is any one of
00062     // the following
00063     // .auto. - an automatic var of a function.
00064     // .temp. - temproray data of a function.
00065     // .ret.  - return value label for a function.
00066     // .frame. - Frame label for a function where retval, args
00067     //           and temps are stored.
00068     // .args. - Label used to pass arguments to a direct call.
00069     // Example - Function name:   @foo
00070     //           Its frame:       @foo.frame.
00071     //           Its retval:      @foo.ret.
00072     //           Its local vars:  @foo.auto.a
00073     //           Its temp data:   @foo.temp.
00074     //           Its arg passing: @foo.args.
00075     //----------------------------------------------
00076     // Libcall - compiler generated libcall names must start with .lib.
00077     //           This id will be used to emit extern decls for libcalls.
00078     // Example - libcall name:   @.lib.sra.i8
00079     //           To pass args:   @.lib.sra.i8.args.
00080     //           To return val:  @.lib.sra.i8.ret.
00081     //----------------------------------------------
00082     // SECTION Names
00083     // uninitialized globals - @udata.<num>.#
00084     // initialized globals - @idata.<num>.#
00085     // Function frame - @<func>.frame_section.
00086     // Function autos - @<func>.autos_section.
00087     // Declarations - Enclosed in comments. No section for them.
00088     //----------------------------------------------------------
00089     
00090     // Tags used to mangle different names. 
00091     enum TAGS {
00092       PREFIX_SYMBOL,
00093       GLOBAL,
00094       STATIC_LOCAL,
00095       AUTOS_LABEL,
00096       FRAME_LABEL,
00097       RET_LABEL,
00098       ARGS_LABEL,
00099       TEMPS_LABEL,
00100       
00101       LIBCALL,
00102       
00103       FRAME_SECTION,
00104       AUTOS_SECTION,
00105       CODE_SECTION
00106     };
00107 
00108     // Textual names of the tags.
00109     inline static const char *getTagName(TAGS tag) {
00110       switch (tag) {
00111       default: return "";
00112       case PREFIX_SYMBOL:    return "@";
00113       case AUTOS_LABEL:       return ".auto.";
00114       case FRAME_LABEL:       return ".frame.";
00115       case TEMPS_LABEL:       return ".temp.";
00116       case ARGS_LABEL:       return ".args.";
00117       case RET_LABEL:       return ".ret.";
00118       case LIBCALL:       return ".lib.";
00119       case FRAME_SECTION:       return ".frame_section.";
00120       case AUTOS_SECTION:       return ".autos_section.";
00121       case CODE_SECTION:       return ".code_section.";
00122       }
00123     }
00124 
00125     // Get tag type for the Symbol.
00126     inline static TAGS getSymbolTag(const std::string &Sym) {
00127       if (Sym.find(getTagName(TEMPS_LABEL)) != std::string::npos)
00128         return TEMPS_LABEL;
00129 
00130       if (Sym.find(getTagName(FRAME_LABEL)) != std::string::npos)
00131         return FRAME_LABEL;
00132 
00133       if (Sym.find(getTagName(RET_LABEL)) != std::string::npos)
00134         return RET_LABEL;
00135 
00136       if (Sym.find(getTagName(ARGS_LABEL)) != std::string::npos)
00137         return ARGS_LABEL;
00138 
00139       if (Sym.find(getTagName(AUTOS_LABEL)) != std::string::npos)
00140         return AUTOS_LABEL;
00141 
00142       if (Sym.find(getTagName(LIBCALL)) != std::string::npos)
00143         return LIBCALL;
00144 
00145       // It does not have any Tag. So its a true global or static local.
00146       if (Sym.find(".") == std::string::npos) 
00147         return GLOBAL;
00148       
00149       // If a . is there, then it may be static local.
00150       // We should mangle these as well in clang.
00151       if (Sym.find(".") != std::string::npos) 
00152         return STATIC_LOCAL;
00153  
00154       assert (0 && "Could not determine Symbol's tag");
00155       return PREFIX_SYMBOL; // Silence warning when assertions are turned off.
00156     }
00157 
00158     // addPrefix - add prefix symbol to a name if there isn't one already.
00159     inline static std::string addPrefix (const std::string &Name) {
00160       std::string prefix = getTagName (PREFIX_SYMBOL);
00161 
00162       // If this name already has a prefix, nothing to do.
00163       if (Name.compare(0, prefix.size(), prefix) == 0)
00164         return Name;
00165 
00166       return prefix + Name;
00167     }
00168 
00169     // Get mangled func name from a mangled sym name.
00170     // In all cases func name is the first component before a '.'.
00171     static inline std::string getFuncNameForSym(const std::string &Sym1) {
00172       assert (getSymbolTag(Sym1) != GLOBAL && "not belongs to a function");
00173 
00174       std::string Sym = addPrefix(Sym1);
00175 
00176       // Position of the . after func name. That's where func name ends.
00177       size_t func_name_end = Sym.find ('.');
00178 
00179       return Sym.substr (0, func_name_end);
00180     }
00181 
00182     // Get Frame start label for a func.
00183     static std::string getFrameLabel(const std::string &Func) {
00184       std::string Func1 = addPrefix(Func);
00185       std::string tag = getTagName(FRAME_LABEL);
00186       return Func1 + tag;
00187     }
00188 
00189     static std::string getRetvalLabel(const std::string &Func) {
00190       std::string Func1 = addPrefix(Func);
00191       std::string tag = getTagName(RET_LABEL);
00192       return Func1 + tag;
00193     }
00194 
00195     static std::string getArgsLabel(const std::string &Func) {
00196       std::string Func1 = addPrefix(Func);
00197       std::string tag = getTagName(ARGS_LABEL);
00198       return Func1 + tag;
00199     }
00200 
00201     static std::string getTempdataLabel(const std::string &Func) {
00202       std::string Func1 = addPrefix(Func);
00203       std::string tag = getTagName(TEMPS_LABEL);
00204       return Func1 + tag;
00205     }
00206 
00207     static std::string getFrameSectionName(const std::string &Func) {
00208       std::string Func1 = addPrefix(Func);
00209       std::string tag = getTagName(FRAME_SECTION);
00210       return Func1 + tag + "# UDATA_OVR";
00211     }
00212 
00213     static std::string getAutosSectionName(const std::string &Func) {
00214       std::string Func1 = addPrefix(Func);
00215       std::string tag = getTagName(AUTOS_SECTION);
00216       return Func1 + tag + "# UDATA_OVR";
00217     }
00218 
00219     static std::string getCodeSectionName(const std::string &Func) {
00220       std::string Func1 = addPrefix(Func);
00221       std::string tag = getTagName(CODE_SECTION);
00222       return Func1 + tag + "# CODE";
00223     }
00224 
00225     // udata, romdata and idata section names are generated by a given number.
00226     // @udata.<num>.# 
00227     static std::string getUdataSectionName(unsigned num, 
00228                                            std::string prefix = "") {
00229        std::ostringstream o;
00230        o << getTagName(PREFIX_SYMBOL) << prefix << "udata." << num 
00231          << ".# UDATA"; 
00232        return o.str(); 
00233     }
00234 
00235     static std::string getRomdataSectionName(unsigned num,
00236                                              std::string prefix = "") {
00237        std::ostringstream o;
00238        o << getTagName(PREFIX_SYMBOL) << prefix << "romdata." << num 
00239          << ".# ROMDATA";
00240        return o.str();
00241     }
00242 
00243     static std::string getIdataSectionName(unsigned num,
00244                                            std::string prefix = "") {
00245        std::ostringstream o;
00246        o << getTagName(PREFIX_SYMBOL) << prefix << "idata." << num 
00247          << ".# IDATA"; 
00248        return o.str(); 
00249     }
00250 
00251     inline static bool isLocalName (const std::string &Name) {
00252       if (getSymbolTag(Name) == AUTOS_LABEL)
00253         return true;
00254 
00255       return false;
00256     }
00257 
00258     inline static bool isMemIntrinsic (const std::string &Name) {
00259       if (Name.compare("@memcpy") == 0 || Name.compare("@memset") == 0 ||
00260           Name.compare("@memmove") == 0) {
00261         return true;
00262       }
00263       
00264       return false;
00265     }
00266 
00267     inline static bool isLocalToFunc (std::string &Func, std::string &Var) {
00268       if (! isLocalName(Var)) return false;
00269 
00270       std::string Func1 = addPrefix(Func);
00271       // Extract func name of the varilable.
00272       const std::string &fname = getFuncNameForSym(Var);
00273 
00274       if (fname.compare(Func1) == 0)
00275         return true;
00276 
00277       return false;
00278     }
00279 
00280 
00281     // Get the section for the given external symbol names.
00282     // This tries to find the type (Tag) of the symbol from its mangled name
00283     // and return appropriate section name for it.
00284     static inline std::string getSectionNameForSym(const std::string &Sym1) {
00285       std::string Sym = addPrefix(Sym1);
00286 
00287       std::string SectionName;
00288  
00289       std::string Fname = getFuncNameForSym (Sym);
00290       TAGS id = getSymbolTag (Sym);
00291 
00292       switch (id) {
00293         default : assert (0 && "Could not determine external symbol type");
00294         case FRAME_LABEL:
00295         case RET_LABEL:
00296         case TEMPS_LABEL:
00297         case ARGS_LABEL:  {
00298           return getFrameSectionName(Fname);
00299         }
00300         case AUTOS_LABEL: {
00301           return getAutosSectionName(Fname);
00302         }
00303       }
00304     }
00305   }; // class PAN.
00306 
00307 
00308   // External symbol names require memory to live till the program end.
00309   // So we have to allocate it and keep.
00310   inline static const char *createESName (const std::string &name) {
00311     char *tmpName = new char[name.size() + 1];
00312     strcpy (tmpName, name.c_str());
00313     return tmpName;
00314   }
00315 
00316 
00317 
00318   inline static const char *PIC16CondCodeToString(PIC16CC::CondCodes CC) {
00319     switch (CC) {
00320     default: llvm_unreachable("Unknown condition code");
00321     case PIC16CC::NE:  return "ne";
00322     case PIC16CC::EQ:   return "eq";
00323     case PIC16CC::LT:   return "lt";
00324     case PIC16CC::ULT:   return "lt";
00325     case PIC16CC::LE:  return "le";
00326     case PIC16CC::ULE:  return "le";
00327     case PIC16CC::GT:  return "gt";
00328     case PIC16CC::UGT:  return "gt";
00329     case PIC16CC::GE:   return "ge";
00330     case PIC16CC::UGE:   return "ge";
00331     }
00332   }
00333 
00334   inline static bool isSignedComparison(PIC16CC::CondCodes CC) {
00335     switch (CC) {
00336     default: llvm_unreachable("Unknown condition code");
00337     case PIC16CC::NE:  
00338     case PIC16CC::EQ: 
00339     case PIC16CC::LT:
00340     case PIC16CC::LE:
00341     case PIC16CC::GE:
00342     case PIC16CC::GT:
00343       return true;
00344     case PIC16CC::ULT:
00345     case PIC16CC::UGT:
00346     case PIC16CC::ULE:
00347     case PIC16CC::UGE:
00348       return false;   // condition codes for unsigned comparison. 
00349     }
00350   }
00351 
00352 
00353 
00354   FunctionPass *createPIC16ISelDag(PIC16TargetMachine &TM);
00355   // Banksel optimizer pass.
00356   FunctionPass *createPIC16MemSelOptimizerPass();
00357 
00358   extern Target ThePIC16Target;
00359   extern Target TheCooperTarget;
00360   
00361 } // end namespace llvm;
00362 
00363 // Defines symbolic names for PIC16 registers.  This defines a mapping from
00364 // register name to register number.
00365 #include "PIC16GenRegisterNames.inc"
00366 
00367 // Defines symbolic names for the PIC16 instructions.
00368 #include "PIC16GenInstrNames.inc"
00369 
00370 #endif