1*fce4895dSRene Gollent /*
2*fce4895dSRene Gollent * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3*fce4895dSRene Gollent * Distributed under the terms of the MIT License.
4*fce4895dSRene Gollent */
5*fce4895dSRene Gollent
6*fce4895dSRene Gollent
7*fce4895dSRene Gollent #include <new>
8*fce4895dSRene Gollent
9*fce4895dSRene Gollent #include "CfaContext.h"
10*fce4895dSRene Gollent
11*fce4895dSRene Gollent
CfaContext()12*fce4895dSRene Gollent CfaContext::CfaContext()
13*fce4895dSRene Gollent :
14*fce4895dSRene Gollent fTargetLocation(0),
15*fce4895dSRene Gollent fLocation(0),
16*fce4895dSRene Gollent fCodeAlignment(0),
17*fce4895dSRene Gollent fDataAlignment(0),
18*fce4895dSRene Gollent fReturnAddressRegister(0),
19*fce4895dSRene Gollent fRuleSet(NULL),
20*fce4895dSRene Gollent fInitialRuleSet(NULL),
21*fce4895dSRene Gollent fRuleSetStack(10, true)
22*fce4895dSRene Gollent {
23*fce4895dSRene Gollent }
24*fce4895dSRene Gollent
25*fce4895dSRene Gollent
~CfaContext()26*fce4895dSRene Gollent CfaContext::~CfaContext()
27*fce4895dSRene Gollent {
28*fce4895dSRene Gollent delete fRuleSet;
29*fce4895dSRene Gollent delete fInitialRuleSet;
30*fce4895dSRene Gollent }
31*fce4895dSRene Gollent
32*fce4895dSRene Gollent
33*fce4895dSRene Gollent void
SetLocation(target_addr_t targetLocation,target_addr_t initialLocation)34*fce4895dSRene Gollent CfaContext::SetLocation(target_addr_t targetLocation,
35*fce4895dSRene Gollent target_addr_t initialLocation)
36*fce4895dSRene Gollent {
37*fce4895dSRene Gollent fTargetLocation = targetLocation;
38*fce4895dSRene Gollent fLocation = initialLocation;
39*fce4895dSRene Gollent }
40*fce4895dSRene Gollent
41*fce4895dSRene Gollent
42*fce4895dSRene Gollent status_t
Init(uint32 registerCount)43*fce4895dSRene Gollent CfaContext::Init(uint32 registerCount)
44*fce4895dSRene Gollent {
45*fce4895dSRene Gollent fRuleSet = new(std::nothrow) CfaRuleSet;
46*fce4895dSRene Gollent if (fRuleSet == NULL)
47*fce4895dSRene Gollent return B_NO_MEMORY;
48*fce4895dSRene Gollent
49*fce4895dSRene Gollent return fRuleSet->Init(registerCount);
50*fce4895dSRene Gollent }
51*fce4895dSRene Gollent
52*fce4895dSRene Gollent
53*fce4895dSRene Gollent status_t
SaveInitialRuleSet()54*fce4895dSRene Gollent CfaContext::SaveInitialRuleSet()
55*fce4895dSRene Gollent {
56*fce4895dSRene Gollent fInitialRuleSet = fRuleSet->Clone();
57*fce4895dSRene Gollent if (fInitialRuleSet == NULL)
58*fce4895dSRene Gollent return B_NO_MEMORY;
59*fce4895dSRene Gollent return B_OK;
60*fce4895dSRene Gollent }
61*fce4895dSRene Gollent
62*fce4895dSRene Gollent
63*fce4895dSRene Gollent status_t
PushRuleSet()64*fce4895dSRene Gollent CfaContext::PushRuleSet()
65*fce4895dSRene Gollent {
66*fce4895dSRene Gollent CfaRuleSet* ruleSet = fRuleSet->Clone();
67*fce4895dSRene Gollent if (ruleSet == NULL || !fRuleSetStack.AddItem(ruleSet)) {
68*fce4895dSRene Gollent delete ruleSet;
69*fce4895dSRene Gollent return B_NO_MEMORY;
70*fce4895dSRene Gollent }
71*fce4895dSRene Gollent
72*fce4895dSRene Gollent return B_OK;
73*fce4895dSRene Gollent }
74*fce4895dSRene Gollent
75*fce4895dSRene Gollent
76*fce4895dSRene Gollent status_t
PopRuleSet()77*fce4895dSRene Gollent CfaContext::PopRuleSet()
78*fce4895dSRene Gollent {
79*fce4895dSRene Gollent if (fRuleSetStack.IsEmpty())
80*fce4895dSRene Gollent return B_BAD_DATA;
81*fce4895dSRene Gollent
82*fce4895dSRene Gollent delete fRuleSet;
83*fce4895dSRene Gollent fRuleSet = fRuleSetStack.RemoveItemAt(
84*fce4895dSRene Gollent fRuleSetStack.CountItems() - 1);
85*fce4895dSRene Gollent
86*fce4895dSRene Gollent return B_OK;
87*fce4895dSRene Gollent }
88*fce4895dSRene Gollent
89*fce4895dSRene Gollent
90*fce4895dSRene Gollent void
SetLocation(target_addr_t location)91*fce4895dSRene Gollent CfaContext::SetLocation(target_addr_t location)
92*fce4895dSRene Gollent {
93*fce4895dSRene Gollent fLocation = location;
94*fce4895dSRene Gollent }
95*fce4895dSRene Gollent
96*fce4895dSRene Gollent
97*fce4895dSRene Gollent void
SetCodeAlignment(uint32 alignment)98*fce4895dSRene Gollent CfaContext::SetCodeAlignment(uint32 alignment)
99*fce4895dSRene Gollent {
100*fce4895dSRene Gollent fCodeAlignment = alignment;
101*fce4895dSRene Gollent }
102*fce4895dSRene Gollent
103*fce4895dSRene Gollent
104*fce4895dSRene Gollent void
SetDataAlignment(int32 alignment)105*fce4895dSRene Gollent CfaContext::SetDataAlignment(int32 alignment)
106*fce4895dSRene Gollent {
107*fce4895dSRene Gollent fDataAlignment = alignment;
108*fce4895dSRene Gollent }
109*fce4895dSRene Gollent
110*fce4895dSRene Gollent
111*fce4895dSRene Gollent void
SetReturnAddressRegister(uint32 reg)112*fce4895dSRene Gollent CfaContext::SetReturnAddressRegister(uint32 reg)
113*fce4895dSRene Gollent {
114*fce4895dSRene Gollent fReturnAddressRegister = reg;
115*fce4895dSRene Gollent }
116*fce4895dSRene Gollent
117*fce4895dSRene Gollent
118*fce4895dSRene Gollent void
RestoreRegisterRule(uint32 reg)119*fce4895dSRene Gollent CfaContext::RestoreRegisterRule(uint32 reg)
120*fce4895dSRene Gollent {
121*fce4895dSRene Gollent if (CfaRule* rule = RegisterRule(reg)) {
122*fce4895dSRene Gollent if (fInitialRuleSet != NULL)
123*fce4895dSRene Gollent *rule = *fInitialRuleSet->RegisterRule(reg);
124*fce4895dSRene Gollent }
125*fce4895dSRene Gollent }
126