<commit_before>//===--------------------------- libuwind.cpp -----------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//
//  Implements unw_* functions from <libunwind.h>
//
//===----------------------------------------------------------------------===//

#include <libunwind.h>

#include <new>

#include "libunwind_ext.h"
#include "config.h"


#if _LIBUNWIND_BUILD_ZERO_COST_APIS

#include "UnwindCursor.hpp"

using namespace libunwind;

/// internal object to represent this processes address space
LocalAddressSpace LocalAddressSpace::sThisAddressSpace;

/// record the registers and stack position of the caller
extern int unw_getcontext(unw_context_t *);
// note: unw_getcontext() implemented in assembly

/// Create a cursor of a thread in this process given 'context' recorded by
/// unw_getcontext().
_LIBUNWIND_EXPORT int unw_init_local(unw_cursor_t *cursor,
                                     unw_context_t *context) {
  _LIBUNWIND_TRACE_API("unw_init_local(cursor=%p, context=%p)\n",
                              cursor, context);
  // Use "placement new" to allocate UnwindCursor in the cursor buffer.
#if __i386__
  new ((void *)cursor) UnwindCursor<LocalAddressSpace, Registers_x86>(
                                 context, LocalAddressSpace::sThisAddressSpace);
#elif __x86_64__
  new ((void *)cursor) UnwindCursor<LocalAddressSpace, Registers_x86_64>(
                                 context, LocalAddressSpace::sThisAddressSpace);
#elif __ppc__
  new ((void *)cursor) UnwindCursor<LocalAddressSpace, Registers_ppc>(
                                 context, LocalAddressSpace::sThisAddressSpace);
#elif __arm64__
  new ((void *)cursor) UnwindCursor<LocalAddressSpace, Registers_arm64>(
                                 context, LocalAddressSpace::sThisAddressSpace);
#endif
  AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
  co->setInfoBasedOnIPRegister();

  return UNW_ESUCCESS;
}

#if UNW_REMOTE

_LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space =
    (unw_addr_space_t) & sThisAddressSpace;

/// Create a cursor into a thread in another process.
_LIBUNWIND_EXPORT int unw_init_remote_thread(unw_cursor_t *cursor,
                                             unw_addr_space_t as,
                                             void *arg) {
  // special case: unw_init_remote(xx, unw_local_addr_space, xx)
  if (as == (unw_addr_space_t) & sThisAddressSpace)
    return unw_init_local(cursor, NULL); //FIXME

  // use "placement new" to allocate UnwindCursor in the cursor buffer
  switch (as->cpuType) {
  case CPU_TYPE_I386:
    new ((void *)cursor)
        UnwindCursor<OtherAddressSpace<Pointer32<LittleEndian> >,
                     Registers_x86>(((unw_addr_space_i386 *)as)->oas, arg);
    break;
  case CPU_TYPE_X86_64:
    new ((void *)cursor) UnwindCursor<
        OtherAddressSpace<Pointer64<LittleEndian> >, Registers_x86_64>(
        ((unw_addr_space_x86_64 *)as)->oas, arg);
    break;
  case CPU_TYPE_POWERPC:
    new ((void *)cursor)
        UnwindCursor<OtherAddressSpace<Pointer32<BigEndian> >, Registers_ppc>(
            ((unw_addr_space_ppc *)as)->oas, arg);
    break;
  default:
    return UNW_EUNSPEC;
  }
  return UNW_ESUCCESS;
}


static bool is64bit(task_t task) {
  return false; // FIXME
}

/// Create an address_space object for use in examining another task.
_LIBUNWIND_EXPORT unw_addr_space_t unw_create_addr_space_for_task(task_t task) {
#if __i386__
  if (is64bit(task)) {
    unw_addr_space_x86_64 *as = new unw_addr_space_x86_64(task);
    as->taskPort = task;
    as->cpuType = CPU_TYPE_X86_64;
    //as->oas
  } else {
    unw_addr_space_i386 *as = new unw_addr_space_i386(task);
    as->taskPort = task;
    as->cpuType = CPU_TYPE_I386;
    //as->oas
  }
#else
// FIXME
#endif
}


/// Delete an address_space object.
_LIBUNWIND_EXPORT void unw_destroy_addr_space(unw_addr_space_t asp) {
  switch (asp->cpuType) {
#if __i386__ || __x86_64__
  case CPU_TYPE_I386: {
    unw_addr_space_i386 *as = (unw_addr_space_i386 *)asp;
    delete as;
  }
  break;
  case CPU_TYPE_X86_64: {
    unw_addr_space_x86_64 *as = (unw_addr_space_x86_64 *)asp;
    delete as;
  }
  break;
#endif
  case CPU_TYPE_POWERPC: {
    unw_addr_space_ppc *as = (unw_addr_space_ppc *)asp;
    delete as;
  }
  break;
  }
}
#endif // UNW_REMOTE


/// Get value of specified register at cursor position in stack frame.
_LIBUNWIND_EXPORT int unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
                                  unw_word_t *value) {
  _LIBUNWIND_TRACE_API("unw_get_reg(cursor=%p, regNum=%d, &value=%p)\n",
                              cursor, regNum, value);
  AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
  if (co->validReg(regNum)) {
    *value = co->getReg(regNum);
    return UNW_ESUCCESS;
  }
  return UNW_EBADREG;
}


/// Set value of specified register at cursor position in stack frame.
_LIBUNWIND_EXPORT int unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
                                  unw_word_t value) {
  _LIBUNWIND_TRACE_API("unw_set_reg(cursor=%p, regNum=%d, value=0x%llX)\n",
                             cursor, regNum, value);
  typedef LocalAddressSpace::pint_t pint_t;
  AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
  if (co->validReg(regNum)) {
    co->setReg(regNum, (pint_t)value);
    // specical case altering IP to re-find info (being called by personality
    // function)
    if (regNum == UNW_REG_IP) {
      unw_proc_info_t info;
      co->getInfo(&info);
      pint_t orgArgSize = (pint_t)info.gp;
      uint64_t orgFuncStart = info.start_ip;
      co->setInfoBasedOnIPRegister(false);
    }
    return UNW_ESUCCESS;
  }
  return UNW_EBADREG;
}


/// Get value of specified float register at cursor position in stack frame.
_LIBUNWIND_EXPORT int unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
                                    unw_fpreg_t *value) {
  _LIBUNWIND_TRACE_API("unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)\n",
                             cursor, regNum, value);
  AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
  if (co->validFloatReg(regNum)) {
    *value = co->getFloatReg(regNum);
    return UNW_ESUCCESS;
  }
  return UNW_EBADREG;
}


/// Set value of specified float register at cursor position in stack frame.
_LIBUNWIND_EXPORT int unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,<commit_msg>Simplify.<commit_after>//===--------------------------- libuwind.cpp -----------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//
//  Implements unw_* functions from <libunwind.h>
//
//===----------------------------------------------------------------------===//

#include <libunwind.h>

#include <new>

#include "libunwind_ext.h"
#include "config.h"


#if _LIBUNWIND_BUILD_ZERO_COST_APIS

#include "UnwindCursor.hpp"

using namespace libunwind;

/// internal object to represent this processes address space
LocalAddressSpace LocalAddressSpace::sThisAddressSpace;

/// record the registers and stack position of the caller
extern int unw_getcontext(unw_context_t *);
// note: unw_getcontext() implemented in assembly

/// Create a cursor of a thread in this process given 'context' recorded by
/// unw_getcontext().
_LIBUNWIND_EXPORT int unw_init_local(unw_cursor_t *cursor,
                                     unw_context_t *context) {
  _LIBUNWIND_TRACE_API("unw_init_local(cursor=%p, context=%p)\n",
                              cursor, context);
  // Use "placement new" to allocate UnwindCursor in the cursor buffer.
#if __i386__
  new ((void *)cursor) UnwindCursor<LocalAddressSpace, Registers_x86>(
                                 context, LocalAddressSpace::sThisAddressSpace);
#elif __x86_64__
  new ((void *)cursor) UnwindCursor<LocalAddressSpace, Registers_x86_64>(
                                 context, LocalAddressSpace::sThisAddressSpace);
#elif __ppc__
  new ((void *)cursor) UnwindCursor<LocalAddressSpace, Registers_ppc>(
                                 context, LocalAddressSpace::sThisAddressSpace);
#elif __arm64__
  new ((void *)cursor) UnwindCursor<LocalAddressSpace, Registers_arm64>(
                                 context, LocalAddressSpace::sThisAddressSpace);
#endif
  AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
  co->setInfoBasedOnIPRegister();

  return UNW_ESUCCESS;
}

#if UNW_REMOTE

_LIBUNWIND_EXPORT unw_addr_space_t unw_local_addr_space =
    (unw_addr_space_t) & sThisAddressSpace;

/// Create a cursor into a thread in another process.
_LIBUNWIND_EXPORT int unw_init_remote_thread(unw_cursor_t *cursor,
                                             unw_addr_space_t as,
                                             void *arg) {
  // special case: unw_init_remote(xx, unw_local_addr_space, xx)
  if (as == (unw_addr_space_t) & sThisAddressSpace)
    return unw_init_local(cursor, NULL); //FIXME

  // use "placement new" to allocate UnwindCursor in the cursor buffer
  switch (as->cpuType) {
  case CPU_TYPE_I386:
    new ((void *)cursor)
        UnwindCursor<OtherAddressSpace<Pointer32<LittleEndian> >,
                     Registers_x86>(((unw_addr_space_i386 *)as)->oas, arg);
    break;
  case CPU_TYPE_X86_64:
    new ((void *)cursor) UnwindCursor<
        OtherAddressSpace<Pointer64<LittleEndian> >, Registers_x86_64>(
        ((unw_addr_space_x86_64 *)as)->oas, arg);
    break;
  case CPU_TYPE_POWERPC:
    new ((void *)cursor)
        UnwindCursor<OtherAddressSpace<Pointer32<BigEndian> >, Registers_ppc>(
            ((unw_addr_space_ppc *)as)->oas, arg);
    break;
  default:
    return UNW_EUNSPEC;
  }
  return UNW_ESUCCESS;
}


static bool is64bit(task_t task) {
  return false; // FIXME
}

/// Create an address_space object for use in examining another task.
_LIBUNWIND_EXPORT unw_addr_space_t unw_create_addr_space_for_task(task_t task) {
#if __i386__
  if (is64bit(task)) {
    unw_addr_space_x86_64 *as = new unw_addr_space_x86_64(task);
    as->taskPort = task;
    as->cpuType = CPU_TYPE_X86_64;
    //as->oas
  } else {
    unw_addr_space_i386 *as = new unw_addr_space_i386(task);
    as->taskPort = task;
    as->cpuType = CPU_TYPE_I386;
    //as->oas
  }
#else
// FIXME
#endif
}


/// Delete an address_space object.
_LIBUNWIND_EXPORT void unw_destroy_addr_space(unw_addr_space_t asp) {
  switch (asp->cpuType) {
#if __i386__ || __x86_64__
  case CPU_TYPE_I386: {
    unw_addr_space_i386 *as = (unw_addr_space_i386 *)asp;
    delete as;
  }
  break;
  case CPU_TYPE_X86_64: {
    unw_addr_space_x86_64 *as = (unw_addr_space_x86_64 *)asp;
    delete as;
  }
  break;
#endif
  case CPU_TYPE_POWERPC: {
    unw_addr_space_ppc *as = (unw_addr_space_ppc *)asp;
    delete as;
  }
  break;
  }
}
#endif // UNW_REMOTE


/// Get value of specified register at cursor position in stack frame.
_LIBUNWIND_EXPORT int unw_get_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
                                  unw_word_t *value) {
  _LIBUNWIND_TRACE_API("unw_get_reg(cursor=%p, regNum=%d, &value=%p)\n",
                              cursor, regNum, value);
  AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
  if (co->validReg(regNum)) {
    *value = co->getReg(regNum);
    return UNW_ESUCCESS;
  }
  return UNW_EBADREG;
}


/// Set value of specified register at cursor position in stack frame.
_LIBUNWIND_EXPORT int unw_set_reg(unw_cursor_t *cursor, unw_regnum_t regNum,
                                  unw_word_t value) {
  _LIBUNWIND_TRACE_API("unw_set_reg(cursor=%p, regNum=%d, value=0x%llX)\n",
                             cursor, regNum, value);
  typedef LocalAddressSpace::pint_t pint_t;
  AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
  if (co->validReg(regNum)) {
    co->setReg(regNum, (pint_t)value);
    // specical case altering IP to re-find info (being called by personality
    // function)
    if (regNum == UNW_REG_IP)
      co->setInfoBasedOnIPRegister(false);
    return UNW_ESUCCESS;
  }
  return UNW_EBADREG;
}


/// Get value of specified float register at cursor position in stack frame.
_LIBUNWIND_EXPORT int unw_get_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
                                    unw_fpreg_t *value) {
  _LIBUNWIND_TRACE_API("unw_get_fpreg(cursor=%p, regNum=%d, &value=%p)\n",
                             cursor, regNum, value);
  AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
  if (co->validFloatReg(regNum)) {
    *value = co->getFloatReg(regNum);
    return UNW_ESUCCESS;
  }
  return UNW_EBADREG;
}


/// Set value of specified float register at cursor position in stack frame.
_LIBUNWIND_EXPORT int unw_set_fpreg(unw_cursor_t *cursor, unw_regnum_t regNum,
{'fraction_non_alphanumeric': 0.11847389558232932, 'fraction_numerical': 0.015600864998455359, 'mean_word_length': 2.874625972471574, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '780078', 'n_tokens_mistral': 4445, 'n_tokens_neox': 4259, 'n_words': 962}