Home | History | Annotate | Download | only in NVPTX
      1 //===-- NVPTXutil.cpp - Functions exported to CodeGen --*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file contains the functions that can be used in CodeGen.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "NVPTXutil.h"
     15 #include "NVPTX.h"
     16 
     17 using namespace llvm;
     18 
     19 namespace llvm {
     20 
     21 bool isParamLoad(const MachineInstr *MI) {
     22   if ((MI->getOpcode() != NVPTX::LD_i32_avar) &&
     23       (MI->getOpcode() != NVPTX::LD_i64_avar))
     24     return false;
     25   if (MI->getOperand(2).isImm() == false)
     26     return false;
     27   if (MI->getOperand(2).getImm() != NVPTX::PTXLdStInstCode::PARAM)
     28     return false;
     29   return true;
     30 }
     31 
     32 #define DATA_MASK 0x7f
     33 #define DIGIT_WIDTH 7
     34 #define MORE_BYTES 0x80
     35 
     36 static int encode_leb128(uint64_t val, int *nbytes, char *space, int splen) {
     37   char *a;
     38   char *end = space + splen;
     39 
     40   a = space;
     41   do {
     42     unsigned char uc;
     43 
     44     if (a >= end)
     45       return 1;
     46     uc = val & DATA_MASK;
     47     val >>= DIGIT_WIDTH;
     48     if (val != 0)
     49       uc |= MORE_BYTES;
     50     *a = uc;
     51     a++;
     52   } while (val);
     53   *nbytes = a - space;
     54   return 0;
     55 }
     56 
     57 #undef DATA_MASK
     58 #undef DIGIT_WIDTH
     59 #undef MORE_BYTES
     60 
     61 uint64_t encode_leb128(const char *str) {
     62   union {
     63     uint64_t x;
     64     char a[8];
     65   } temp64;
     66 
     67   temp64.x = 0;
     68 
     69   for (unsigned i = 0, e = strlen(str); i != e; ++i)
     70     temp64.a[i] = str[e - 1 - i];
     71 
     72   char encoded[16];
     73   int nbytes;
     74 
     75   int retval = encode_leb128(temp64.x, &nbytes, encoded, 16);
     76 
     77   (void) retval;
     78   assert(retval == 0 && "Encoding to leb128 failed");
     79 
     80   assert(nbytes <= 8 &&
     81          "Cannot support register names with leb128 encoding > 8 bytes");
     82 
     83   temp64.x = 0;
     84   for (int i = 0; i < nbytes; ++i)
     85     temp64.a[i] = encoded[i];
     86 
     87   return temp64.x;
     88 }
     89 
     90 } // end namespace llvm
     91