12a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner//===- MemoryObject.cpp - Abstract memory interface -----------------------===//
2251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan//
3251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan//                     The LLVM Compiler Infrastructure
4251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan//
5251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan// This file is distributed under the University of Illinois Open Source
6251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan// License. See LICENSE.TXT for details.
7251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan//
8251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan//===----------------------------------------------------------------------===//
9251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan
10251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan#include "llvm/Support/MemoryObject.h"
112a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattnerusing namespace llvm;
12251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan
132a1744f553442fbc3cd74114cbcd1305d91d8a90Chris LattnerMemoryObject::~MemoryObject() {
142a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner}
152a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner
162a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattnerint MemoryObject::readBytes(uint64_t address,
172a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner                            uint64_t size,
182a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner                            uint8_t* buf,
192a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner                            uint64_t* copied) const {
202a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner  uint64_t current = address;
212a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner  uint64_t limit = getBase() + getExtent();
22f66f76c1a31539c65007a81fe28a1e06f638b4daBenjamin Kramer
23f66f76c1a31539c65007a81fe28a1e06f638b4daBenjamin Kramer  if (current + size > limit)
24f66f76c1a31539c65007a81fe28a1e06f638b4daBenjamin Kramer    return -1;
25f66f76c1a31539c65007a81fe28a1e06f638b4daBenjamin Kramer
26f66f76c1a31539c65007a81fe28a1e06f638b4daBenjamin Kramer  while (current - address < size) {
272a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner    if (readByte(current, &buf[(current - address)]))
282a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner      return -1;
29251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan
302a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner    current++;
31251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan  }
322a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner
332a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner  if (copied)
342a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner    *copied = current - address;
352a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner
362a1744f553442fbc3cd74114cbcd1305d91d8a90Chris Lattner  return 0;
37251ef612a812ac99edeab6c08a752bf8ca220921Sean Callanan}
38