[llvm-commits] CVS: reopt/lib/TraceCache/VirtualMem.cpp

Brian Gaeke gaeke at cs.uiuc.edu
Wed Apr 21 15:33:02 PDT 2004


Changes in directory reopt/lib/TraceCache:

VirtualMem.cpp updated: 1.14 -> 1.15

---
Log message:

Add better comments, including doxygen comments.
Throw exception in constructor if open() fails.
Other minor cleanups.
Make the more complex versions of VirtualMem::readInstrFrmVm() call the
 simple one, instead of duplicating its functionality.
Make changeBranchTarget() and getBranchTarget() look as much alike as possible.


---
Diffs of the changes:  (+42 -62)

Index: reopt/lib/TraceCache/VirtualMem.cpp
diff -u reopt/lib/TraceCache/VirtualMem.cpp:1.14 reopt/lib/TraceCache/VirtualMem.cpp:1.15
--- reopt/lib/TraceCache/VirtualMem.cpp:1.14	Wed Nov 19 16:51:52 2003
+++ reopt/lib/TraceCache/VirtualMem.cpp	Wed Apr 21 15:32:47 2004
@@ -10,22 +10,26 @@
 #include "reopt/TraceCache.h"
 #include "reopt/GetTraceTime.h"
 #include <cassert>
+#include <string>
+#include <cstdio>
 
 using namespace llvm;
 
-VirtualMem::VirtualMem(){
-  //open file pointer
+VirtualMem::VirtualMem() {
+  // I/O into the process's address space is accomplished by reading
+  // and writing the /proc/<pid>/as file exported by SVR4 procfs.
+  // Get a file descriptor open to that file now.
   char ctr[25];
-  int pid = getpid();
-  sprintf(ctr, "/proc/%d/as",pid);
+  sprintf(ctr, "/proc/%d/as", (int) getpid ());
   fp = open(ctr, O_RDWR);
-  assert(fp>=0 && "Error opening file!");
+  if (fp < 0)
+    throw std::string ("Error opening address space file in procfs.");
 }
 
+/// Emit the instruction words in TRACE to memory starting at address traceStartAddr.
+///
 void VirtualMem::copyToVM(std::vector<unsigned int> &trace, 
                           uint64_t traceStartAddr){
-  //copy sz instructions starting from
-  //location trace to virtual memory address traceStartAddr
   lseek(fp, traceStartAddr, SEEK_SET);
 
   for(int i=0, sz = trace.size(); i<sz; i++){
@@ -42,34 +46,21 @@
 }
 
 unsigned int VirtualMem::readInstrFrmVm(uint64_t frm, TraceCache *tr){
-  unsigned int instr=0;
-
-  instr = tr->getAddr(frm);
-  if(instr)
-      return instr;  
-    
-  lseek(fp, frm, SEEK_SET);
-
-  read(fp, &instr, 4);
-  return instr;
+  unsigned int instr = tr->getAddr(frm);
+  if (instr)
+    return instr;
+  return readInstrFrmVm (frm);
 }
 
 unsigned int VirtualMem::readInstrFrmVm(uint64_t frm, TraceCache *tr, 
 					TraceCache *tr2){
-  unsigned int instr=0;
-  
-  instr = tr->getAddr(frm);
-  if(instr)
-      return instr;  
-  
+  unsigned int instr = tr->getAddr(frm);
+  if (instr)
+    return instr;  
   instr = tr2->getAddr(frm);
-  if(instr)
+  if (instr)
     return instr;
-
-  lseek(fp, frm, SEEK_SET);
-
-  read(fp, &instr, 4);
-  return instr;
+  return readInstrFrmVm (frm);
 }
 
 void VirtualMem::writeInstToVM(uint64_t dest, unsigned int newInstr){
@@ -77,64 +68,53 @@
   write(fp, &newInstr, 4);
 }
 
-//two kinds of branches being handled for now
-//BIcc, and BPcc instructions
-//(sparcV9 manual, pg 146-150)
-//
+/// changeBranchTarget - Handles two kinds of branches for now, namely,
+/// BIcc and BPcc instructions. (see the SPARC-V9 manual, pg 146-150).
+///
 void VirtualMem::changeBranchTarget(uint64_t frm, uint64_t to){ 
   unsigned int instr = readInstrFrmVm(frm);
-  //std::cerr<<"Instruction at: "<<(void *)frm<<" Is:"<<(void *)instr<<"\n";
   
-  //check if instr is a jump
-  assert(isBranchInstr(instr) && "Not a jump instruction");
+  // Check if instr is a jump/branch instruction.
+  assert(isBranchInstr(instr) && "Not a jump/branch instruction");
 
-  //std::cerr<<"\t frm Address: "<<(void *)frm<<"\n";
-  //std::cerr<<"\t to address: "<<(void *)to<<"\n";
-  
-  if(isNonDepJump(instr)){     
+  if (isNonDepJump(instr)) {
     unsigned int newInstr = getUndepJumpInstr(instr, to, frm);
     writeInstToVM(frm, newInstr);
-    //std::cerr<<"New instruction:"<<(void *)newInstr<<"\n"; 
-  }
-
-  else if(isDepJump(instr)){      
+  } else if (isDepJump(instr)) {
     unsigned int newInstr = getDepJumpInstr(instr, to, frm);
     writeInstToVM(frm, newInstr);
-    //std::cerr<<"New instruction:"<<(void *)newInstr<<"\n";
-  }
-
-  else{
-    //std::cerr<<(void *) instr<<"\n";
+  } else {
     assert(0 && "This jump/branch not yet handled!");
   }
 }
 
+/// getBranchTarget - Handles two kinds of branches for now, namely,
+/// BIcc and BPcc instructions. (see the SPARC-V9 manual, pg 146-150).
+/// Reads the branch instruction from the second half of the given pair.
+///
 uint64_t VirtualMem::getBranchTarget(const std::pair<uint64_t, uint64_t> &n){
+  uint64_t frm = n.second;
+  unsigned int instr = readInstrFrmVm(frm);
   
-  unsigned int instr = readInstrFrmVm(n.second);
-  
-  assert(isBranchInstr(instr) && "Not a branch Instr");
+  // Check if instr is a jump/branch instruction.
+  assert(isBranchInstr(instr) && "Not a jump/branch instruction");
   
-  //only handles BIcc and BPcc instructions for now
-  //(sparcV9 manual, pg 146-150)
-  if(isNonDepJump(instr))
+  if (isNonDepJump(instr)) {
     return getNonDepJmpTarget(instr, n.first);
-  else if(isDepJump(instr))
+  } else if(isDepJump(instr)) {
     return getDepJmpTarget(instr, n.first);
-  
-  //std::cerr<<(void *) instr<<"\n";
-  assert(0 && "This branch type is not yet handled!");
-  return 0;
+  } else {
+    assert(0 && "This jump/branch not yet handled!");
+    return 0;
+  }
 }
 
 //write branch inst, followed by a null inst in delay slot
 //
 void VirtualMem::writeBranchInstruction(uint64_t location, uint64_t target){
-  //uint64_t instToWrite = getDepJumpInstr(0x10800000, target, location);
   //create branch will annul bit set
   unsigned int instToWrite = getDepJumpInstr(0x30800000, target, location);
   writeInstToVM(location, instToWrite);
-  //writeInstToVM(location+4, 0x01000000); 
 }
 
 #ifdef GET_TRACE_TIME





More information about the llvm-commits mailing list