FrontPentTest.cpp

Go to the documentation of this file.
00001 /*==============================================================================
00002  * FILE:       FrontPentTest.cc
00003  * OVERVIEW:   Provides the implementation for the FrontPentTest class, which
00004  *              tests the sparc front end
00005  *============================================================================*/
00006 /*
00007  * $Revision: 1.24 $
00008  *
00009  * 05 Apr 02 - Mike: Created
00010  * 21 May 02 - Mike: Mods for gcc 3.1
00011  */
00012 
00013 #define HELLO_PENT      "test/pentium/hello"
00014 #define BRANCH_PENT     "test/pentium/branch"
00015 #define FEDORA2_TRUE    "test/pentium/fedora2_true"
00016 #define FEDORA3_TRUE    "test/pentium/fedora3_true"
00017 #define SUSE_TRUE       "test/pentium/suse_true"
00018 
00019 #include "types.h"
00020 #include "rtl.h"
00021 #include "FrontPentTest.h"
00022 #include "prog.h"
00023 #include "frontend.h"
00024 #include "pentiumfrontend.h"
00025 #include "BinaryFile.h"
00026 #include "BinaryFileStub.h"
00027 
00028 /*==============================================================================
00029  * FUNCTION:        FrontPentTest::registerTests
00030  * OVERVIEW:        Register the test functions in the given suite
00031  * PARAMETERS:      Pointer to the test suite
00032  * RETURNS:         <nothing>
00033  *============================================================================*/
00034 #define MYTEST(name) \
00035 suite->addTest(new CppUnit::TestCaller<FrontPentTest> ("FrontPentTest", \
00036     &FrontPentTest::name, *this))
00037 
00038 void FrontPentTest::registerTests(CppUnit::TestSuite* suite) {
00039     MYTEST(test1);
00040     MYTEST(test2);
00041     MYTEST(test3);
00042     MYTEST(testBranch);
00043     MYTEST(testFindMain);
00044 }
00045 
00046 int FrontPentTest::countTestCases () const
00047 { return 3; }   // ? What's this for?
00048 
00049 /*==============================================================================
00050  * FUNCTION:        FrontPentTest::setUp
00051  * OVERVIEW:        Set up anything needed before all tests
00052  * NOTE:            Called before any tests
00053  * NOTE:            Also appears to be called before all tests!
00054  * PARAMETERS:      <none>
00055  * RETURNS:         <nothing>
00056  *============================================================================*/
00057 void FrontPentTest::setUp () {
00058 }
00059 
00060 /*==============================================================================
00061  * FUNCTION:        FrontPentTest::tearDown
00062  * OVERVIEW:        Delete objects created in setUp
00063  * NOTE:            Called after all tests
00064  * PARAMETERS:      <none>
00065  * RETURNS:         <nothing>
00066  *============================================================================*/
00067 void FrontPentTest::tearDown () {
00068 }
00069 
00070 /*==============================================================================
00071  * FUNCTION:        FrontPentTest::test1
00072  * OVERVIEW:        Test decoding some pentium instructions
00073  *============================================================================*/
00074 void FrontPentTest::test1 () {
00075     std::ostringstream ost;
00076 
00077     BinaryFileFactory bff;
00078     BinaryFile *pBF = bff.Load(HELLO_PENT);
00079     if (pBF == NULL)
00080         pBF = new BinaryFileStub();
00081     CPPUNIT_ASSERT(pBF != 0);
00082     CPPUNIT_ASSERT(pBF->GetMachine() == MACHINE_PENTIUM);
00083     Prog* prog = new Prog;
00084     FrontEnd *pFE = new PentiumFrontEnd(pBF, prog, &bff);
00085     prog->setFrontEnd(pFE);
00086 
00087     bool gotMain;
00088     ADDRESS addr = pFE->getMainEntryPoint(gotMain);
00089     CPPUNIT_ASSERT (addr != NO_ADDRESS);
00090 
00091     // Decode first instruction
00092     DecodeResult inst = pFE->decodeInstruction(addr);
00093     inst.rtl->print(ost);
00094     
00095     std::string expected(
00096         "08048328    0 *32* m[r28 - 4] := r29\n"
00097         "            0 *32* r28 := r28 - 4\n");
00098     CPPUNIT_ASSERT_EQUAL(expected, std::string(ost.str()));
00099 
00100     std::ostringstream o2;
00101     addr += inst.numBytes;
00102     inst = pFE->decodeInstruction(addr);
00103     inst.rtl->print(o2);
00104     expected = std::string("08048329    0 *32* r29 := r28\n");
00105     CPPUNIT_ASSERT_EQUAL(expected, std::string(o2.str()));
00106 
00107     std::ostringstream o3;
00108     addr = 0x804833b;
00109     inst = pFE->decodeInstruction(addr);
00110     inst.rtl->print(o3);
00111     expected = std::string(
00112         "0804833b    0 *32* m[r28 - 4] := 0x80483fc\n"
00113         "            0 *32* r28 := r28 - 4\n");
00114     CPPUNIT_ASSERT_EQUAL(expected, std::string(o3.str()));
00115 
00116     delete pFE;
00117     // delete pBF;
00118 }
00119 
00120 void FrontPentTest::test2() {
00121     DecodeResult inst;
00122     std::string expected;
00123 
00124     BinaryFileFactory bff;
00125     BinaryFile *pBF = bff.Load(HELLO_PENT);
00126     if (pBF == NULL)
00127         pBF = new BinaryFileStub();
00128     CPPUNIT_ASSERT(pBF != 0);
00129     CPPUNIT_ASSERT(pBF->GetMachine() == MACHINE_PENTIUM);
00130     Prog* prog = new Prog;
00131     FrontEnd *pFE = new PentiumFrontEnd(pBF, prog, &bff); 
00132     prog->setFrontEnd(pFE);
00133 
00134     std::ostringstream o1;
00135     inst = pFE->decodeInstruction(0x8048345);
00136     inst.rtl->print(o1);
00137     expected = std::string(
00138         "08048345    0 *32* tmp1 := r28\n"
00139         "            0 *32* r28 := r28 + 16\n"
00140         "            0 *v* %flags := ADDFLAGS32( tmp1, 16, r28 )\n");
00141     CPPUNIT_ASSERT_EQUAL(expected, std::string(o1.str()));
00142 
00143     std::ostringstream o2;
00144     inst = pFE->decodeInstruction(0x8048348);
00145     inst.rtl->print(o2);
00146     expected = std::string(
00147         "08048348    0 *32* r24 := 0\n");
00148     CPPUNIT_ASSERT_EQUAL(expected, std::string(o2.str()));
00149 
00150     std::ostringstream o3;
00151     inst = pFE->decodeInstruction(0x8048329);
00152     inst.rtl->print(o3);
00153     expected = std::string("08048329    0 *32* r29 := r28\n");
00154     CPPUNIT_ASSERT_EQUAL(expected, std::string(o3.str()));
00155 
00156     delete pFE;
00157     // delete pBF;
00158 }
00159 
00160 void FrontPentTest::test3() {
00161     DecodeResult inst;
00162     std::string expected;
00163 
00164     BinaryFileFactory bff;
00165     BinaryFile *pBF = bff.Load(HELLO_PENT);
00166     if (pBF == NULL)
00167         pBF = new BinaryFileStub();
00168     CPPUNIT_ASSERT(pBF != 0);
00169     CPPUNIT_ASSERT(pBF->GetMachine() == MACHINE_PENTIUM);
00170     Prog* prog = new Prog;
00171     FrontEnd *pFE = new PentiumFrontEnd(pBF, prog, &bff); 
00172     prog->setFrontEnd(pFE);
00173 
00174     std::ostringstream o1;
00175     inst = pFE->decodeInstruction(0x804834d);
00176     inst.rtl->print(o1);
00177     expected = std::string(
00178         "0804834d    0 *32* r28 := r29\n"
00179         "            0 *32* r29 := m[r28]\n"
00180         "            0 *32* r28 := r28 + 4\n");
00181     CPPUNIT_ASSERT_EQUAL(expected, std::string(o1.str()));
00182 
00183     std::ostringstream o2;
00184     inst = pFE->decodeInstruction(0x804834e);
00185     inst.rtl->print(o2);
00186     expected = std::string(
00187         "0804834e    0 *32* %pc := m[r28]\n"
00188         "            0 *32* r28 := r28 + 4\n"
00189         "            0 RET\n"
00190         "              Modifieds: \n"
00191         "              Reaching definitions: \n");
00192 
00193     CPPUNIT_ASSERT_EQUAL(expected, std::string(o2.str()));
00194 
00195     delete pFE;
00196     // delete pBF;
00197 }
00198 
00199 void FrontPentTest::testBranch() {
00200     DecodeResult inst;
00201     std::string expected;
00202 
00203     BinaryFileFactory bff;
00204     BinaryFile *pBF = bff.Load(BRANCH_PENT);
00205     if (pBF == NULL)
00206         pBF = new BinaryFileStub();
00207     CPPUNIT_ASSERT(pBF != 0);
00208     CPPUNIT_ASSERT(pBF->GetMachine() == MACHINE_PENTIUM);
00209     Prog* prog = new Prog;
00210     FrontEnd *pFE = new PentiumFrontEnd(pBF, prog, &bff); 
00211     prog->setFrontEnd(pFE);
00212 
00213     // jne
00214     std::ostringstream o1;
00215     inst = pFE->decodeInstruction(0x8048979);
00216     inst.rtl->print(o1);
00217     expected = std::string("08048979    0 BRANCH 0x8048988, condition "
00218       "not equals\n"
00219       "High level: %flags\n");
00220     CPPUNIT_ASSERT_EQUAL(expected, o1.str());
00221 
00222     // jg
00223     std::ostringstream o2;
00224     inst = pFE->decodeInstruction(0x80489c1);
00225     inst.rtl->print(o2);
00226     expected = std::string(
00227       "080489c1    0 BRANCH 0x80489d5, condition signed greater\n"
00228       "High level: %flags\n");
00229     CPPUNIT_ASSERT_EQUAL(expected, std::string(o2.str()));
00230 
00231     // jbe
00232     std::ostringstream o3;
00233     inst = pFE->decodeInstruction(0x8048a1b);
00234     inst.rtl->print(o3);
00235     expected = std::string(
00236         "08048a1b    0 BRANCH 0x8048a2a, condition unsigned less or equals\n"
00237         "High level: %flags\n");
00238     CPPUNIT_ASSERT_EQUAL(expected, std::string(o3.str()));
00239 
00240     delete pFE;
00241     // delete pBF;
00242 }
00243 
00244 void FrontPentTest::testFindMain() {
00245     // Test the algorithm for finding main, when there is a call to __libc_start_main
00246     // Also tests the loader hack
00247     BinaryFileFactory bff;
00248     BinaryFile* pBF = bff.Load(FEDORA2_TRUE);
00249     CPPUNIT_ASSERT(pBF != NULL);
00250     Prog* prog = new Prog;
00251     FrontEnd* pFE = new PentiumFrontEnd(pBF, prog, &bff); 
00252     prog->setFrontEnd(pFE);
00253     CPPUNIT_ASSERT(pFE != NULL);
00254     bool found;
00255     ADDRESS addr = pFE->getMainEntryPoint(found);
00256     ADDRESS expected = 0x8048b10;
00257     CPPUNIT_ASSERT_EQUAL(expected, addr);
00258     pBF->Close();
00259     bff.UnLoad();
00260 
00261     pBF = bff.Load(FEDORA3_TRUE);
00262     CPPUNIT_ASSERT(pBF != NULL);
00263     pFE = new PentiumFrontEnd(pBF, prog, &bff); 
00264     prog->setFrontEnd(pFE);
00265     CPPUNIT_ASSERT(pFE != NULL);
00266     addr = pFE->getMainEntryPoint(found);
00267     expected = 0x8048c4a;
00268     CPPUNIT_ASSERT_EQUAL(expected, addr);
00269     pBF->Close();
00270     bff.UnLoad();
00271 
00272     pBF = bff.Load(SUSE_TRUE);
00273     CPPUNIT_ASSERT(pBF != NULL);
00274     pFE = new PentiumFrontEnd(pBF, prog, &bff); 
00275     prog->setFrontEnd(pFE);
00276     CPPUNIT_ASSERT(pFE != NULL);
00277     addr = pFE->getMainEntryPoint(found);
00278     expected = 0x8048b60;
00279     CPPUNIT_ASSERT_EQUAL(expected, addr);
00280     pBF->Close();
00281 
00282     delete pFE;
00283 }

Generated on Tue Sep 19 21:18:24 2006 for Boomerang by  doxygen 1.4.6