<commit_before>#ifndef __SYM_EVAL_HH__
#define __SYM_EVAL_HH__

#include "llvm/IR/Instruction.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Debug.h"
#include "llvm/ADT/APInt.h"

#include <crab/cfg/Cfg.hpp>

namespace crab_llvm
{

  using namespace llvm;
  using namespace std;
  using namespace crab::cfg;

  //! Map llvm values to integer linear expressions
  template<typename VariableFactory, typename ZLinExp>
  struct SymEval {

    VariableFactory& m_vfac;
    TrackedPrecision m_track_lvl;

    typedef typename VariableFactory::varname_t varname_t;

    SymEval (VariableFactory &vfac, TrackedPrecision track_lvl): 
        m_vfac (vfac), m_track_lvl (track_lvl)
    { }

    bool isTracked (const llvm::Value &v) {
      // -- a pointer
      if (v.getType ()->isPointerTy ()) 
        return (m_track_lvl >= PTR); 

      // -- always track integer registers
      return v.getType ()->isIntegerTy ();
    }

    varname_t symVar (const Value &v) {
      assert (isTracked (v));
      return m_vfac [v];
    }

    varname_t symVar (int v) {
      return m_vfac.get (v);
    }
  
    bool isVar (ZLinExp e) {
      auto v = e.get_variable ();
      bool res = (v ? true: false);
      return res;
    }

    boost::optional<ZLinExp> lookup (const Value &v) {

      if (isa<const UndefValue> (&v))
        return boost::optional<ZLinExp> ();

      if (isa<ConstantPointerNull> (&v))
        return boost::optional<ZLinExp> (0);
      
      if (const ConstantInt *c = dyn_cast<const ConstantInt> (&v)) {
        if (c->getValue ().getMinSignedBits () > 64) {
          errs () << "Warning: " << toMpz (c->getValue ()).get_str ()  
                  << " too big for int64_t.\n";
        }
        else
          return ZLinExp (c->getValue ().getSExtValue ());
      }

      if (isTracked(v) && !isa<ConstantExpr> (v))
        return ZLinExp (symVar (v));
      
      return boost::optional<ZLinExp>();
    }
        
    /** Converts v to mpz_class. Assumes that v is signed */
    inline mpz_class toMpz (const APInt &v)
    {
      // Based on:
      // https://llvm.org/svn/llvm-project/polly/trunk/lib/Support/GICHelper.cpp
      // return v.getSExtValue ();
      
      APInt abs;
      abs = v.isNegative () ? v.abs () : v;
      
      const uint64_t *rawdata = abs.getRawData ();
      unsigned numWords = abs.getNumWords ();
      
      // TODO: Check if this is true for all platforms.
      mpz_class res;<commit_msg>[FIX] Boolean constants were translated to int64_t after signed extension which was converting true into -1../build/run/bin/llvm-as mierda.ll -o test-cond-1.bc The fix applies zero extension to booleans and signed extension to other integer constants.<commit_after>#ifndef __SYM_EVAL_HH__
#define __SYM_EVAL_HH__

#include "llvm/IR/Instruction.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Debug.h"
#include "llvm/ADT/APInt.h"

#include <crab/cfg/Cfg.hpp>

namespace crab_llvm
{

  using namespace llvm;
  using namespace std;
  using namespace crab::cfg;

  //! Map llvm values to integer linear expressions
  template<typename VariableFactory, typename ZLinExp>
  struct SymEval {

    VariableFactory& m_vfac;
    TrackedPrecision m_track_lvl;

    typedef typename VariableFactory::varname_t varname_t;

    SymEval (VariableFactory &vfac, TrackedPrecision track_lvl): 
        m_vfac (vfac), m_track_lvl (track_lvl)
    { }

    bool isTracked (const llvm::Value &v) {
      // -- a pointer
      if (v.getType ()->isPointerTy ()) 
        return (m_track_lvl >= PTR); 

      // -- always track integer registers
      return v.getType ()->isIntegerTy ();
    }

    varname_t symVar (const Value &v) {
      assert (isTracked (v));
      return m_vfac [v];
    }

    varname_t symVar (int v) {
      return m_vfac.get (v);
    }
  
    bool isVar (ZLinExp e) {
      auto v = e.get_variable ();
      bool res = (v ? true: false);
      return res;
    }

    boost::optional<ZLinExp> lookup (const Value &v) {

      if (isa<const UndefValue> (&v))
        return boost::optional<ZLinExp> ();

      if (isa<ConstantPointerNull> (&v))
        return boost::optional<ZLinExp> (0);
      
      if (const ConstantInt *c = dyn_cast<const ConstantInt> (&v)) {
        if (c->getType ()->isIntegerTy(1)) {
          return ZLinExp (c->getZExtValue ());
        }
        else if (c->getValue ().getMinSignedBits () <= 64) {
          return ZLinExp (c->getSExtValue ());
        }
        else {
          errs () << "Warning: " 
                  <<  toMpz (c->getValue ()).get_str ()  
                  << " does not fit in int64_t.\n";
        }
      }

      if (isTracked(v) && !isa<ConstantExpr> (v))
        return ZLinExp (symVar (v));
      
      return boost::optional<ZLinExp>();
    }
        
    /** Converts v to mpz_class. Assumes that v is signed */
    inline mpz_class toMpz (const APInt &v)
    {
      // Based on:
      // https://llvm.org/svn/llvm-project/polly/trunk/lib/Support/GICHelper.cpp
      // return v.getSExtValue ();
      
      APInt abs;
      abs = v.isNegative () ? v.abs () : v;
      
      const uint64_t *rawdata = abs.getRawData ();
      unsigned numWords = abs.getNumWords ();
      
      // TODO: Check if this is true for all platforms.
      mpz_class res;
{'fraction_non_alphanumeric': 0.1276555386949924, 'fraction_numerical': 0.003603945371775417, 'mean_word_length': 2.3310170562223624, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 39, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4525944', 'n_tokens_mistral': 1812, 'n_tokens_neox': 1749, 'n_words': 444}