-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.cpp
111 lines (95 loc) · 1.83 KB
/
memory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "memory.h"
string datum::toString()
{
char buf[100];
if (type == "float") {
sprintf(buf, "%f", fpoint);
return string(buf);
} else if (type == "int") {
sprintf(buf, "%d", integer);
return string(buf);
} else {
return string("ptr");
}
}
int memory::allocate(int n)
{
int r = heappointer;
heappointer += n;
return r;
}
// this is used for debugging record keeping
void memory::resetregdebug()
{
for (int i = 0 ; i < NUMREGISTERS ; i++) {
gets[i] = false;
sets[i] = false;
}
}
datum memory::stackget(int address)
{
return stack[address];
}
void memory::stackset(int address, datum n)
{
stack[address] = n;
}
/*
datum memory::stackpop()
{
return stack[--stackpointer];
}
void memory::stackpush(datum n)
{
stack[stackpointer++] = n;
}
*/
datum memory::registerget(registerid id)
{
gets[id] = true;
return registers[id];
}
void memory::registerset(registerid id, datum value)
{
sets[id] = true;
registers[id] = value;
}
datum memory::heapget(int address)
{
return heap[address];
}
void memory::heapset(int address, datum value)
{
heap[address] = value;
}
void memory::debugMemory()
{
const char* sp = " ";
cout << sp << "registers: " << endl;
for (registerid i = 0 ; i < NUMREGISTERS ; i++) {
if (gets[i] || sets[i]) {
string st;
if (sets && gets) st == "(s, g)";
else if (sets) st == "(s, )";
else st == "( ,g)";
cout
<< sp << " "
<< debugRegName(i)
<< " = " << registers[i].toString()
<< " " << st
<< endl;
}
}
cout << sp << "stack: " << endl;
for (int i = 0 ; i < registers[SPREGID].integer ; i++) {
cout << sp << i << "=" << stack[i].toString() << endl;
}
}
string memory::debugRegName(registerid r)
{
if (r == SPREGID) return "$SP";
if (r == FPREGID) return "$FP";
stringstream ss;
ss << "$" << r;
return ss.str();
}