//Mike Maxim
//Memory operations translation interface

package edu.cmu.cs.l3.translate;

import edu.cmu.cs.l3.tree.*;
import edu.cmu.cs.l3.errormsg.*;
import edu.cmu.cs.l3.frame.*;
import edu.cmu.cs.l3.semant.*;
import edu.cmu.cs.l3.temp.*;

public abstract class TranslateMemory {

    protected ErrorMsg m_errorMsg;
    protected static Temp m_nulltemp=null;

    public void setErrorHandler(ErrorMsg error) {
        m_errorMsg = error;
    }

// Allocates memory on the heap, optionally creating a fat pointer to it
// The fat pointer is also on the heap. Reason: Global scope has no stack.
// Size is # of machine words, not bytes!
    public abstract IRExpression allocateHeapMemory(Frame frame, IRExpression size,
                                           boolean createpointer);

    public abstract IRExpression addressToFatPointer(Frame frame, IRExpression address);

    public static IRTemp getNullTemp() {
        return new IRTemp(m_nulltemp);
    }

    public static void createNullPointer(Frame frame) {
        if (m_nulltemp == null) {
            m_nulltemp = new Temp();
            m_nulltemp.setGlobal(true);
            m_nulltemp.setGlobalName("np");
            Translator.addFragment(
                new FragmentGlobal(frame, new IRTemp(m_nulltemp), true));
        }
    }

// Makes sure the fat pointer is within the bounds originally set out
    public abstract IRExpression checkFatHeapPointer(Frame frame, IRExpression pointer,
                                            int pos) throws TranslateException;

// Unsafe heap dereference
    public abstract IRExpression unsafeFatHeapPointer(Frame frame, IRExpression ptr);

    public abstract IRExpression addressOf(Frame frame, IRExpression exp) throws
        TranslateException;

    public abstract IRExpression safeDereference(Frame frame, IRExpression pointer,
                                        int pos) throws TranslateException;

    public abstract IRExpression unsafeArrayAccess(Frame frame, IRExpression array,
                                          IRExpression offset) throws
        TranslateException;

    public abstract IRExpression thinPointerAccess(Frame frame, IRExpression array,
                                          IRExpression offset);

    public abstract IRExpression pointerIncrement(Frame frame, IRExpression ptr);

    public abstract IRExpression pointerDecrement(Frame frame, IRExpression ptr);

    public abstract IRExpression pointerAddition(Frame frame, IRExpression ptr,
                                        IRExpression additive, boolean addition);

    public abstract IRExpression pointerInfo(Frame frame, IRExpression ptr,
                                    int operation);

    public abstract IRExpression pointerEquality(Frame frame, IRExpression left,
                                        IRExpression right, int equal);

    public abstract IRStatement copyExpression(Frame frame, IRExpression left,
                                      IRExpression right, Ty rty) throws
        TranslateException;

    public abstract IRExpression copyFatPointer(Frame frame, IRExpression ptr);
}

