LLVM API Documentation

NVPTXutil.cpp
Go to the documentation of this file.
00001 //===-- NVPTXutil.cpp - Functions exported to CodeGen --*- 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 functions that can be used in CodeGen.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "NVPTXutil.h"
00015 #include "NVPTX.h"
00016 
00017 using namespace llvm;
00018 
00019 namespace llvm {
00020 
00021 bool isParamLoad(const MachineInstr *MI) {
00022   if ((MI->getOpcode() != NVPTX::LD_i32_avar) &&
00023       (MI->getOpcode() != NVPTX::LD_i64_avar))
00024     return false;
00025   if (MI->getOperand(2).isImm() == false)
00026     return false;
00027   if (MI->getOperand(2).getImm() != NVPTX::PTXLdStInstCode::PARAM)
00028     return false;
00029   return true;
00030 }
00031 
00032 #define DATA_MASK 0x7f
00033 #define DIGIT_WIDTH 7
00034 #define MORE_BYTES 0x80
00035 
00036 static int encode_leb128(uint64_t val, int *nbytes, char *space, int splen) {
00037   char *a;
00038   char *end = space + splen;
00039 
00040   a = space;
00041   do {
00042     unsigned char uc;
00043 
00044     if (a >= end)
00045       return 1;
00046     uc = val & DATA_MASK;
00047     val >>= DIGIT_WIDTH;
00048     if (val != 0)
00049       uc |= MORE_BYTES;
00050     *a = uc;
00051     a++;
00052   } while (val);
00053   *nbytes = a - space;
00054   return 0;
00055 }
00056 
00057 #undef DATA_MASK
00058 #undef DIGIT_WIDTH
00059 #undef MORE_BYTES
00060 
00061 uint64_t encode_leb128(const char *str) {
00062   union {
00063     uint64_t x;
00064     char a[8];
00065   } temp64;
00066 
00067   temp64.x = 0;
00068 
00069   for (unsigned i = 0, e = strlen(str); i != e; ++i)
00070     temp64.a[i] = str[e - 1 - i];
00071 
00072   char encoded[16];
00073   int nbytes;
00074 
00075   int retval = encode_leb128(temp64.x, &nbytes, encoded, 16);
00076 
00077   (void) retval;
00078   assert(retval == 0 && "Encoding to leb128 failed");
00079 
00080   assert(nbytes <= 8 &&
00081          "Cannot support register names with leb128 encoding > 8 bytes");
00082 
00083   temp64.x = 0;
00084   for (int i = 0; i < nbytes; ++i)
00085     temp64.a[i] = encoded[i];
00086 
00087   return temp64.x;
00088 }
00089 
00090 } // end namespace llvm