From 8e33e2828ffc62238afc32bb2593b8619f586077 Mon Sep 17 00:00:00 2001 From: bd-912 Date: Mon, 6 May 2024 00:59:26 -0600 Subject: Partial implementation for starter Condense --- condense/StackHelper.java | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 condense/StackHelper.java (limited to 'condense/StackHelper.java') diff --git a/condense/StackHelper.java b/condense/StackHelper.java new file mode 100644 index 0000000..a0a6e2d --- /dev/null +++ b/condense/StackHelper.java @@ -0,0 +1,70 @@ +package condense; + +import misc.*; +import cs132.vapor.ast.VFunction; +import cs132.vapor.ast.VMemRef; + +import java.util.*; + +public class StackHelper { + + private int in; + private int out; + private int local; + private int frame_size; + private HashMap idx_to_addr; + + public StackHelper(VFunction.Stack s) { + this.in = s.in; + this.out = s.out; + this.local = s.local; + this.frame_size = this.stackFrameFormula(); + this.idx_to_addr = new HashMap(); + MinimalLogger.info(String.format("Found in:%d out:%d local%d framesize:%d...", + this.in, + this.out, + this.local, + this.frame_size)); + + String key; + String value; + int loc; + for (int i = 0; i < this.local; ++i) { // locals + loc = (( -i - 1 ) * 4) - 8; + key = String.format("Local[%d]", i); + value = String.format("%d($fp)", loc); + this.idx_to_addr.put(key, value); + } + for (int i = 0; i < this.out; ++i) { // out + loc = ((this.out + this.local + 2) - i) * -4; + key = String.format("Out[%d]", i); + value = String.format("%d($fp)", loc); + this.idx_to_addr.put(key, value); + } + for (int i = 0; i < this.in; ++i) { // in + loc = i * 4; + key = String.format("In[%d]", i); + value = String.format("%d($fp)", loc); + this.idx_to_addr.put(key, value); + } + + MinimalLogger.info(String.format("Mapping: %s", this.idx_to_addr.toString())); + } + + public String get(VMemRef.Stack m) { + String key = String.format("%s[%d]", + m.region, + m.index); + MinimalLogger.severe(key); + return this.idx_to_addr.get(key); + } + + public int getFrameSize() { + return this.frame_size; + } + + private int stackFrameFormula() { + return (this.out + this.local + 2) * 4; + } + +} -- cgit v1.2.3