1 //===-- DNBArch.cpp ---------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  Created by Greg Clayton on 6/24/07.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "DNBArch.h"
15 #include <assert.h>
16 #include <mach/mach.h>
17 
18 #include <map>
19 
20 #include "DNBLog.h"
21 
22 typedef std::map<uint32_t, DNBArchPluginInfo> CPUPluginInfoMap;
23 
24 #if defined (__i386__)
25 uint32_t g_current_cpu_type = CPU_TYPE_I386;
26 #elif defined (__x86_64__)
27 uint32_t g_current_cpu_type = CPU_TYPE_X86_64;
28 #elif defined (__arm__)
29 uint32_t g_current_cpu_type = CPU_TYPE_ARM;
30 #else
31 uint32_t g_current_cpu_type = 0;
32 #endif
33 
34 CPUPluginInfoMap g_arch_plugins;
35 
36 
37 static const DNBArchPluginInfo *
38 GetArchInfo ()
39 {
40     CPUPluginInfoMap::const_iterator pos = g_arch_plugins.find(g_current_cpu_type);
41     if (pos != g_arch_plugins.end())
42         return &pos->second;
43     return NULL;
44 }
45 
46 void
47 DNBArchProtocol::SetDefaultArchitecture (uint32_t cpu_type)
48 {
49     DNBLogThreadedIf (LOG_PROCESS, "DNBArchProtocol::SetDefaultArchitecture (cpu_type=0x%8.8x)", cpu_type);
50     g_current_cpu_type = cpu_type;
51     assert (g_arch_plugins.find(g_current_cpu_type) != g_arch_plugins.end());
52 }
53 
54 void
55 DNBArchProtocol::RegisterArchPlugin (const DNBArchPluginInfo &arch_info)
56 {
57     if (arch_info.cpu_type)
58         g_arch_plugins[arch_info.cpu_type] = arch_info;
59 }
60 
61 const DNBRegisterSetInfo *
62 DNBArchProtocol::GetRegisterSetInfo (nub_size_t *num_reg_sets)
63 {
64     const DNBArchPluginInfo *arch_info = GetArchInfo ();
65     if (arch_info)
66         return arch_info->GetRegisterSetInfo (num_reg_sets);
67     *num_reg_sets = 0;
68     return NULL;
69 }
70 
71 DNBArchProtocol *
72 DNBArchProtocol::Create (MachThread *thread)
73 {
74     const DNBArchPluginInfo *arch_info = GetArchInfo ();
75     if (arch_info)
76         return arch_info->Create (thread);
77     return NULL;
78 
79 }
80 
81 const uint8_t * const
82 DNBArchProtocol::GetBreakpointOpcode (nub_size_t byte_size)
83 {
84     const DNBArchPluginInfo *arch_info = GetArchInfo ();
85     if (arch_info)
86         return arch_info->GetBreakpointOpcode (byte_size);
87     return NULL;
88 }
89 
90