Created
May 28, 2016 02:02
-
-
Save synap5e/6809249eebf387ddac6cb15825510a3a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void translate(Expr.RecordAccess e, Location target, Context context) { | |
// Determine the field offset | |
Type.Record type = (Type.Record) typeOf(e.getSource()); | |
int offset = getFieldOffset(type, e.getName()); | |
// Translate source expression into a temporary stack location. This is | |
// unfortunately a little inefficient in some cases, as we could | |
// potentially avoid all memory usage. But, it will do for now! | |
// Note that 'llocateLocation' modifies the stack pointer so it invalidates | |
// 'target' if target is stack relative. | |
MemoryLocation recordLocation = (MemoryLocation)allocateLocation(e.getSource(), context); | |
translate(e.getSource(), recordLocation, context); | |
// get the absolute (i.e. non stack-relative) location of the record then | |
// free the location reserved for the record. | |
// 'recordBase' now points beyond the end of the stack to the record but | |
// the stack is as it was when we entered the function, so if 'target' is | |
// stack-relative then it becomes correct again. | |
RegisterLocation recordBase = context.selectFreeRegister(new Type.Int()); | |
bitwiseCopy(new RegisterLocation(new Type.Int(), recordLocation.base), recordBase, context); | |
context = context.lockLocation(recordBase); | |
freeLocations(context, recordLocation); | |
// Finally, copy bits into target location | |
MemoryLocation fieldLocation = new MemoryLocation(target.type(), recordBase.register, recordLocation.offset + offset); | |
bitwiseCopy(fieldLocation, target, context); | |
context = context.unlockLocation(recordBase); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment