Skip to content

Instantly share code, notes, and snippets.

@synap5e
Created May 28, 2016 02:02
Show Gist options
  • Save synap5e/6809249eebf387ddac6cb15825510a3a to your computer and use it in GitHub Desktop.
Save synap5e/6809249eebf387ddac6cb15825510a3a to your computer and use it in GitHub Desktop.
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