Permalink
Browse files

Merge pull request #25 from zmpinqc/master

Updates to Data Memory Manager
  • Loading branch information...
2 parents b46a30c + 940cd84 commit 8342c7768961389175673b42799fd5c19334b268 @zanpreston zanpreston committed Apr 24, 2012
Showing with 32 additions and 8 deletions.
  1. +32 −8 src/org/zza/codegenerator/DataMemoryManager.java
@@ -1,18 +1,21 @@
package org.zza.codegenerator;
import java.util.HashMap;
+import java.util.Map;
import java.util.Stack;
public class DataMemoryManager {
private final int MAX = 1024;
private int current;
- private HashMap<String,String> memory; //<Variable,Address>
+ private HashMap<String,String> memory; //<Variable,Address>
+ private HashMap<Frame,Map<String,String>> newMemory;
private Stack<Frame> memoryStack;
public DataMemoryManager() {
current = 0;
+ newMemory = new HashMap<Frame,Map<String,String>>();
memory = new HashMap<String,String>();
memoryStack = new Stack<Frame>();
}
@@ -25,6 +28,7 @@ public void addStackFrame(Frame frame) throws MemoryOutOfBoundsException {
if ((current + frame.getSize())< MAX){
memoryStack.push(frame);
current += frame.getSize();
+ newMemory.put(frame, new HashMap<String,String>());
} else {
throw new MemoryOutOfBoundsException(
"Not enough Data Memory available to add a new stack frame" );
@@ -42,30 +46,50 @@ private Frame peekBottomOfStack(){
public Frame popMemoryStack(){
release(memoryStack.peek().getSize());
+ newMemory.remove(peekTopOfStack());
return memoryStack.pop();
}
public void addNewTemporaryVariable(String varName) throws MemoryOutOfBoundsException{
Frame frame = peekTopOfStack();
int address = frame.getNextTemporary();
+ addToMemory(varName, frame, address);
memory.put(varName, "" + (address));
}
public void addLocalVariable(String varName) throws MemoryOutOfBoundsException {
Frame frame = peekTopOfStack();
int address = frame.getNextLocal();
+ addToMemory(varName, frame, address);
memory.put(frame.getName()+varName, ""+(address));
}
+
public int getAddressOfVar(String varName){
- String frameName = peekTopOfStack().getName();
- String result = memory.get(frameName+varName);
- if (result==null){
- frameName = peekBottomOfStack().getName();
- result = memory.get(frameName+varName);
+ String r = getAddress(varName, peekTopOfStack());
+
+ if (r==null){
+ r = getAddress(varName,peekBottomOfStack());
}
- return Integer.parseInt(result);
+
+ return Integer.parseInt(r);
+
+// String frameName = peekTopOfStack().getName();
+// String result = memory.get(frameName+varName);
+// if (result==null){
+// frameName = peekBottomOfStack().getName();
+// result = memory.get(frameName+varName);
+// }
+// return Integer.parseInt(result);
}
-
+
+ private String getAddress(String varName, Frame frame) {
+ return newMemory.get(frame).get(varName);
+ }
+
+ private void addToMemory(String varName, Frame frame, int address) {
+ newMemory.get(frame).put(varName, "" +(address));
+ }
+
}

0 comments on commit 8342c77

Please sign in to comment.