liberty/lib/SLAMP/SLAMP.cpp
// Replace external functions with SLAMP prefixed ones (SLAMP_xxx)
// The list of SLAMP functions are given in `externs.h`
Replace external functions with SLAMP prefixed ones (SLAMP_xxx)
The list of SLAMP functions are given in `externs.h`
Replace external functions with SLAMP prefixed ones ( SLAMP_xxx ) The list of SLAMP functions are given in ` externs . h `
void SLAMP::replaceExternalFunctionCalls(Module &m) {
  set<string> externs;
  for (unsigned i = 0, e = sizeof(externs_str) / sizeof(externs_str[0]); i < e;
       i++)
    externs.insert(externs_str[i]);
  set<string> ignores;
  for (unsigned i = 0,
                e = sizeof(ignore_externs_str) / sizeof(ignore_externs_str[0]);
       i < e; i++)
    ignores.insert(ignore_externs_str[i]);
  vector<Function *> funcs;
  for (auto & fi : m) {
    Function *func = &fi;
    if (!func->isDeclaration())
      continue;
    if (ignores.find(func->getName()) != ignores.end())
      continue;
    if (func->isIntrinsic()) {
      for (Value::user_iterator ui = func->user_begin(); ui != func->user_end();
           ui++)
        assert(isa<IntrinsicInst>(*ui));
      continue;
    }
    funcs.push_back(func);
  }
  bool hasUnrecognizedFunction = false;
  for (auto func : funcs) {
     string name = func->getName();
    if (externs.find(name) == externs.end()) {
      if (func->hasFnAttribute(llvm::Attribute::AttrKind::ReadNone)) {
        continue;
      }
      errs() << "WARNING: Wrapper for external function " << name
                        << " not implemented.\n";
      hasUnrecognizedFunction = true;
    } else {
      string wrapper_name = "SLAMP_" + name;
      FunctionCallee wrapper =
          m.getOrInsertFunction(wrapper_name, func->getFunctionType());
      func->replaceAllUsesWith(wrapper.getCallee());
    }
  }
  if (hasUnrecognizedFunction) {
    LLVM_DEBUG(errs() << "Wrapper for external function not implemented.\n");
  }
}
void SLAMP :: replaceExternalFunctionCalls ( Module & m ) { set < string > externs ; for ( unsigned i = 0 , e = sizeof ( externs_str ) / sizeof ( externs_str [ 0 ] ) ; i < e ; i ++ ) externs . insert ( externs_str [ i ] ) ; set < string > ignores ; for ( unsigned i = 0 , e = sizeof ( ignore_externs_str ) / sizeof ( ignore_externs_str [ 0 ] ) ; i < e ; i ++ ) ignores . insert ( ignore_externs_str [ i ] ) ; vector < Function * > funcs ; for ( auto & fi : m ) { Function * func = & fi ; if ( ! func -> isDeclaration ( ) ) continue ; if ( ignores . find ( func -> getName ( ) ) != ignores . end ( ) ) continue ; if ( func -> isIntrinsic ( ) ) { for ( Value :: user_iterator ui = func -> user_begin ( ) ; ui != func -> user_end ( ) ; ui ++ ) assert ( isa < IntrinsicInst > ( * ui ) ) ; continue ; } funcs . push_back ( func ) ; } bool hasUnrecognizedFunction = false ; for ( auto func : funcs ) { string name = func -> getName ( ) ; if ( externs . find ( name ) == externs . end ( ) ) { if ( func -> hasFnAttribute ( llvm :: Attribute :: AttrKind :: ReadNone ) ) { continue ; } errs ( ) << " " << name << " \n " ; hasUnrecognizedFunction = true ; } else { string wrapper_name = " " + name ; FunctionCallee wrapper = m . getOrInsertFunction ( wrapper_name , func -> getFunctionType ( ) ) ; func -> replaceAllUsesWith ( wrapper . getCallee ( ) ) ; } } if ( hasUnrecognizedFunction ) { LLVM_DEBUG ( errs ( ) << " \n " ) ; } }
Replace external functions with SLAMP prefixed ones (SLAMP_xxx)
The list of SLAMP functions are given in `externs.h`
Replace external functions with SLAMP prefixed ones ( SLAMP_xxx ) The list of SLAMP functions are given in ` externs . h `
// initialize a set of external function names // initialize a set of external functions not to be implemented // only external functions are of interest // filter functions to ignore // FIXME: malloc can be an intrinsic function, not all intrinsics can be ignored // just confirm that all uses is an intrinsic instruction // check if the function argument is `readnone`, then it's pure /* Function* wrapper = cast<Function>( m.getOrInsertFunction(wrapper_name,
       * func->getFunctionType() ) ); */ // replace 'func' to 'wrapper' in uses // assert only turned on for debug // assert(false && "Wrapper for external function not implemented.\n");