-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualMemoryManager.java
More file actions
39 lines (32 loc) · 1.2 KB
/
VirtualMemoryManager.java
File metadata and controls
39 lines (32 loc) · 1.2 KB
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
import java.util.*;
public class VirtualMemoryManager {
private int nextGlobalInt = 1000;
private int nextGlobalFloat = 2000;
private int nextLocalInt = 3000;
private int nextLocalFloat = 4000;
private int nextTempInt = 5000;
private int nextTempFloat = 6000;
private int nextConstInt = 7000;
private int nextConstFloat = 8000;
private Map<String,Integer> constTable = new HashMap<>();
public Map<String,Integer> getConstTable() {
return Collections.unmodifiableMap(constTable);
}
public int allocGlobal(String type) {
return type.equals("float") ? nextGlobalFloat++ : nextGlobalInt++;
}
public int allocLocal(String type) {
return type.equals("float") ? nextLocalFloat++ : nextLocalInt++;
}
public int allocTemp(String type) {
return type.equals("float") ? nextTempFloat++ : nextTempInt++;
}
public int allocConst(String value, String type) {
String key = type + ":" + value;
if (!constTable.containsKey(key)) {
int addr = type.equals("float") ? nextConstFloat++ : nextConstInt++;
constTable.put(key, addr);
}
return constTable.get(key);
}
}