<commit_before>//===-- IRToDWARF.cpp ---------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Expression/IRToDWARF.h"

#include "llvm/Support/raw_ostream.h"
#include "llvm/InstrTypes.h"
#include "llvm/Module.h"

#include "lldb/Core/dwarf.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/Scalar.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Expression/ClangExpressionDeclMap.h"
#include "lldb/Expression/ClangExpressionVariable.h"

#include <map>

using namespace llvm;

IRToDWARF::IRToDWARF(const void *pid,
                     lldb_private::ClangExpressionVariableList &variable_list, 
                     lldb_private::ClangExpressionDeclMap *decl_map,
                     lldb_private::StreamString &strm) :
    ModulePass(pid),
    m_variable_list(variable_list),
    m_decl_map(decl_map),
    m_strm(strm)
{
}

IRToDWARF::~IRToDWARF()
{
}

class Relocator
{
public:
    Relocator()
    {
    }
    
    ~Relocator()
    {
    }
    
    void MarkBasicBlock(BasicBlock *bb, uint16_t offset)
    {
        m_basic_blocks[bb] = offset;
    }
    
    bool BasicBlockIsMarked(BasicBlock *bb)
    {
        return m_basic_blocks.find(bb) != m_basic_blocks.end();
    }
    
    void MarkRelocation(BasicBlock *bb, uint16_t offset)
    {
        m_relocations[offset] = bb;
    }
    
    bool ResolveRelocations(lldb_private::StreamString &strm)
    {
        std::map<uint16_t, BasicBlock*>::const_iterator iter;
        
        lldb_private::StreamString swapper(0, 32, strm.GetByteOrder());
        
        // This array must be delete [] d at every exit
        size_t temporary_bufsize = strm.GetSize();
        uint8_t *temporary_buffer(new uint8_t[temporary_bufsize]);
        
        memcpy(temporary_buffer, strm.GetData(), temporary_bufsize);
                
        for (iter = m_relocations.begin();
             iter != m_relocations.end();
             ++iter)
        {
            const std::pair<uint16_t, BasicBlock*> &pair = *iter;
            
            uint16_t off = pair.first;
            BasicBlock *bb = pair.second;
            
            if (m_basic_blocks.find(bb) == m_basic_blocks.end())
            {
                delete [] temporary_buffer;
                return false;
            }
                
            uint16_t target_off = m_basic_blocks[bb];
            
            int16_t relative = (int16_t)target_off - (int16_t)off;
            
            swapper.Clear();
            swapper << target_off;
            
            memcpy(temporary_buffer + off, swapper.GetData(), sizeof(uint16_t));
        }
        
        strm.Clear();
        strm.Write(temporary_buffer, temporary_bufsize);
        
        delete [] temporary_buffer;<commit_msg>Small fixes to the DWARF relocator.<commit_after>//===-- IRToDWARF.cpp ---------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Expression/IRToDWARF.h"

#include "llvm/Support/raw_ostream.h"
#include "llvm/InstrTypes.h"
#include "llvm/Module.h"

#include "lldb/Core/dwarf.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/Scalar.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Expression/ClangExpressionDeclMap.h"
#include "lldb/Expression/ClangExpressionVariable.h"

#include <map>

using namespace llvm;

IRToDWARF::IRToDWARF(const void *pid,
                     lldb_private::ClangExpressionVariableList &variable_list, 
                     lldb_private::ClangExpressionDeclMap *decl_map,
                     lldb_private::StreamString &strm) :
    ModulePass(pid),
    m_variable_list(variable_list),
    m_decl_map(decl_map),
    m_strm(strm)
{
}

IRToDWARF::~IRToDWARF()
{
}

class Relocator
{
public:
    Relocator()
    {
    }
    
    ~Relocator()
    {
    }
    
    void MarkBasicBlock(BasicBlock *bb, uint16_t offset)
    {
        m_basic_blocks[bb] = offset;
    }
    
    bool BasicBlockIsMarked(BasicBlock *bb)
    {
        return m_basic_blocks.find(bb) != m_basic_blocks.end();
    }
    
    void MarkRelocation(BasicBlock *bb, uint16_t offset)
    {
        m_relocations[offset] = bb;
    }
    
    bool ResolveRelocations(lldb_private::StreamString &strm)
    {
        std::map<uint16_t, BasicBlock*>::const_iterator iter;
        
        lldb_private::StreamString swapper(0, 32, strm.GetByteOrder());
        
        // This array must be delete [] d at every exit
        size_t temporary_bufsize = strm.GetSize();
        uint8_t *temporary_buffer(new uint8_t[temporary_bufsize]);
        
        memcpy(temporary_buffer, strm.GetData(), temporary_bufsize);
                
        for (iter = m_relocations.begin();
             iter != m_relocations.end();
             ++iter)
        {
            const std::pair<uint16_t, BasicBlock*> &pair = *iter;
            
            uint16_t off = pair.first;
            BasicBlock *bb = pair.second;
            
            if (m_basic_blocks.find(bb) == m_basic_blocks.end())
            {
                delete [] temporary_buffer;
                return false;
            }
                
            uint16_t target_off = m_basic_blocks[bb];
            
            int16_t relative = (int16_t)target_off - (int16_t)off;
            
            swapper.Clear();
            swapper << relative;
            
            // off is intended to be the offset of the branch opcode (which is 
            // what the relative location is added to) so 
            // (temporary_buffer + off + 1) skips the opcode and writes to the
            // relative location
            memcpy(temporary_buffer + off + 1, swapper.GetData(), sizeof(uint16_t));
        }
        
        strm.Clear();
        strm.Write(temporary_buffer, temporary_bufsize);
        
        delete [] temporary_buffer;
{'fraction_non_alphanumeric': 0.14351925843055777, 'fraction_numerical': 0.008310692024932075, 'mean_word_length': 1.849726775956284, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '5693950', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1923, 'n_words': 354}