15cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen//===-- SWIG Interface for SBInstructionList --------------------*- C++ -*-===//
25cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen//
35cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen//                     The LLVM Compiler Infrastructure
45cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen//
55cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen// This file is distributed under the University of Illinois Open Source
65cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen// License. See LICENSE.TXT for details.
75cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen//
85cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen//===----------------------------------------------------------------------===//
95cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
105cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen#include <stdio.h>
115cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
125cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chennamespace lldb {
135cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
14b44dc564f8e63adff462db6caac4cb99924bcc29Johnny Chen%feature("docstring",
15b44dc564f8e63adff462db6caac4cb99924bcc29Johnny Chen"Represents a list of machine instructions.  SBFunction and SBSymbol have
16b44dc564f8e63adff462db6caac4cb99924bcc29Johnny ChenGetInstructions() methods which return SBInstructionList instances.
17b44dc564f8e63adff462db6caac4cb99924bcc29Johnny Chen
18b44dc564f8e63adff462db6caac4cb99924bcc29Johnny ChenSBInstructionList supports instruction (SBInstruction instance) iteration.
19b44dc564f8e63adff462db6caac4cb99924bcc29Johnny ChenFor example (see also SBDebugger for a more complete example),
20b44dc564f8e63adff462db6caac4cb99924bcc29Johnny Chen
21b44dc564f8e63adff462db6caac4cb99924bcc29Johnny Chendef disassemble_instructions (insts):
22b44dc564f8e63adff462db6caac4cb99924bcc29Johnny Chen    for i in insts:
23b44dc564f8e63adff462db6caac4cb99924bcc29Johnny Chen        print i
24b44dc564f8e63adff462db6caac4cb99924bcc29Johnny Chen
2539f331058a6492f24e182de0e4fb052c3e00deb1Johnny Chendefines a function which takes an SBInstructionList instance and prints out
26b44dc564f8e63adff462db6caac4cb99924bcc29Johnny Chenthe machine instructions in assembly format."
27b44dc564f8e63adff462db6caac4cb99924bcc29Johnny Chen) SBInstructionList;
285cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chenclass SBInstructionList
295cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen{
305cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chenpublic:
315cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
325cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    SBInstructionList ();
335cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
345cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    SBInstructionList (const SBInstructionList &rhs);
355cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
365cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    ~SBInstructionList ();
375cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
385cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    bool
395cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    IsValid () const;
405cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
415cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    size_t
425cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    GetSize ();
435cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
445cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    lldb::SBInstruction
455cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    GetInstructionAtIndex (uint32_t idx);
465cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
475cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    void
485cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    Clear ();
495cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
505cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    void
515cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    AppendInstruction (lldb::SBInstruction inst);
525cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
535cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    void
545cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    Print (FILE *out);
555cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
565cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    bool
575cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    GetDescription (lldb::SBStream &description);
585cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
595cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    bool
605cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen    DumpEmulationForAllInstructions (const char *triple);
61b302dffacdadeef509d28133a4c66299418122f8Greg Clayton
62b302dffacdadeef509d28133a4c66299418122f8Greg Clayton    %pythoncode %{
63b302dffacdadeef509d28133a4c66299418122f8Greg Clayton        def __len__(self):
64b302dffacdadeef509d28133a4c66299418122f8Greg Clayton            '''Access len of the instruction list.'''
653cae38bc36877dbe1504c6fa2dedbcab71fe5780Filipe Cabecinhas            return int(self.GetSize())
66b302dffacdadeef509d28133a4c66299418122f8Greg Clayton
67b302dffacdadeef509d28133a4c66299418122f8Greg Clayton        def __getitem__(self, key):
682a94be1aa46c90d6749d8c96ed396d5ebde452cdGreg Clayton            '''Access instructions by integer index for array access or by lldb.SBAddress to find an instruction that matches a section offset address object.'''
69b302dffacdadeef509d28133a4c66299418122f8Greg Clayton            if type(key) is int:
70b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                # Find an instruction by index
71b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                if key < len(self):
72b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                    return self.GetInstructionAtIndex(key)
73b302dffacdadeef509d28133a4c66299418122f8Greg Clayton            elif type(key) is SBAddress:
74b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                # Find an instruction using a lldb.SBAddress object
75b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                lookup_file_addr = key.file_addr
76b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                closest_inst = None
77b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                for idx in range(self.GetSize()):
78b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                    inst = self.GetInstructionAtIndex(idx)
79b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                    inst_file_addr = inst.addr.file_addr
80b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                    if inst_file_addr == lookup_file_addr:
81b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                        return inst
82b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                    elif inst_file_addr > lookup_file_addr:
83b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                        return closest_inst
84b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                    else:
85b302dffacdadeef509d28133a4c66299418122f8Greg Clayton                        closest_inst = inst
86b302dffacdadeef509d28133a4c66299418122f8Greg Clayton            return None
87b302dffacdadeef509d28133a4c66299418122f8Greg Clayton    %}
88b302dffacdadeef509d28133a4c66299418122f8Greg Clayton
895cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen};
905cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen
915cb6cab6395a896ec5938acc0885858b8e2ba2c8Johnny Chen} // namespace lldb
92