src/Interface/OpClassCode.cpp

説明を見る。
00001 // 
00002 // Copyright (c) 2005-2008 Kenichi Watanabe.
00003 // Copyright (c) 2005-2008 Yasuhiro Watari.
00004 // Copyright (c) 2005-2008 Hironori Ichibayashi.
00005 // Copyright (c) 2008-2009 Kazuo Horio.
00006 // Copyright (c) 2009-2013 Naruki Kurata.
00007 // Copyright (c) 2005-2013 Ryota Shioya.
00008 // Copyright (c) 2005-2013 Masahiro Goshima.
00009 // 
00010 // This software is provided 'as-is', without any express or implied
00011 // warranty. In no event will the authors be held liable for any damages
00012 // arising from the use of this software.
00013 // 
00014 // Permission is granted to anyone to use this software for any purpose,
00015 // including commercial applications, and to alter it and redistribute it
00016 // freely, subject to the following restrictions:
00017 // 
00018 // 1. The origin of this software must not be misrepresented; you must not
00019 // claim that you wrote the original software. If you use this software
00020 // in a product, an acknowledgment in the product documentation would be
00021 // appreciated but is not required.
00022 // 
00023 // 2. Altered source versions must be plainly marked as such, and must not be
00024 // misrepresented as being the original software.
00025 // 
00026 // 3. This notice may not be removed or altered from any source
00027 // distribution.
00028 // 
00029 // 
00030 
00031 
00032 #include <pch.h>
00033 #include "Interface/OpClassCode.h"
00034 
00035 using namespace std;
00036 using namespace Onikiri;
00037 
00038 namespace {
00039     struct CodeStrElem {
00040         int         code;
00041         const char* str;
00042         bool operator<(const CodeStrElem& cs) const {
00043             return code < cs.code;
00044         }
00045     };
00046 #define CS_ELEM(name) {OpClassCode::name, #name}
00047     static struct CodeStrElem CodeStr[] = {
00048         CS_ELEM(CALL),
00049         CS_ELEM(CALL_JUMP),
00050         CS_ELEM(RET),
00051         CS_ELEM(RETC),
00052         CS_ELEM(iBC),
00053         CS_ELEM(iBU),
00054         CS_ELEM(iJUMP),
00055         CS_ELEM(iLD),
00056         CS_ELEM(iST),
00057         CS_ELEM(iIMM),
00058         CS_ELEM(iMOV),
00059         CS_ELEM(iALU),
00060         CS_ELEM(iSFT),
00061         CS_ELEM(iMUL),
00062         CS_ELEM(iDIV),
00063         CS_ELEM(iBYTE),
00064         CS_ELEM(fBC),
00065         CS_ELEM(fLD),
00066         CS_ELEM(fST),
00067         CS_ELEM(fMOV),
00068         CS_ELEM(fNEG),
00069         CS_ELEM(fADD),
00070         CS_ELEM(fMUL),
00071         CS_ELEM(fDIV),
00072         CS_ELEM(fCONV),
00073         CS_ELEM(fELEM),
00074         CS_ELEM(ifCONV),
00075         CS_ELEM(syscall),
00076         CS_ELEM(syscall_branch),
00077         CS_ELEM(ADDR),
00078         CS_ELEM(iNOP),
00079         CS_ELEM(fNOP),
00080         CS_ELEM(UNDEF),
00081         CS_ELEM(other),
00082     };
00083 #undef CS_ELEM
00084     const CodeStrElem* CodeStrBegin = &CodeStr[0];
00085     const CodeStrElem* CodeStrEnd = CodeStrBegin + sizeof(CodeStr)/sizeof(CodeStr[0]);
00086 }
00087 
00088 namespace Onikiri
00089 {
00090     namespace OpClassCode
00091     {
00092 
00093         const char* ToString(int c)
00094         {
00095             CodeStrElem cs;
00096             cs.code = c;
00097             const CodeStrElem* e = lower_bound(CodeStrBegin, CodeStrEnd, cs);
00098             
00099             if (e == CodeStrEnd || e->code != c) {
00100                 return "(unknown OpClassCode)";
00101             }
00102             else {
00103                 return e->str;
00104             }
00105         }
00106 
00107         int FromString(const char *s)
00108         {
00109             for (const CodeStrElem* e = CodeStrBegin; e != CodeStrEnd; ++e) {
00110                 if (strcmp(e->str, s) == 0)
00111                     return e->code;
00112             }
00113 
00114             THROW_RUNTIME_ERROR("unknown code (%s).", s);
00115             return -1;
00116         }
00117 
00118         //
00119         // ---
00120         //
00121 
00122         // ijumpj
00123         bool IsBranch(int code)         
00124         {
00125             return IsConditionalBranch(code) || IsUnconditionalBranch(code);
00126         }
00127 
00128         // Note: CConditional, IndirectJump, Call r
00129         //       ReturnCall/JumprCConditional Return
00130         // 
00131         bool IsConditionalBranch(int code)
00132         {
00133             return
00134                 code == iBC ||
00135                 code == fBC || 
00136                 code == RETC;
00137         }
00138 
00139         bool IsUnconditionalBranch(int code)
00140         {
00141             return
00142                 IsCall(code) || 
00143                 code == RET ||
00144                 code == iBU || 
00145                 code == iJUMP || 
00146                 code == syscall_branch;
00147         }
00148 
00149         // jump 
00150         bool IsIndirectJump(int code)
00151         {
00152             return
00153                 code == CALL_JUMP || 
00154                 code == iJUMP;
00155         }
00156 
00157         // call
00158         bool IsCall(int code)
00159         {
00160             return 
00161                 code == CALL || 
00162                 code == CALL_JUMP;
00163         }
00164 
00165         // return
00166         bool IsReturn(int code)
00167         {
00168             return 
00169                 code == RET || 
00170                 code == RETC;
00171         }
00172 
00173         bool IsSubroutine(int code)
00174         {
00175             return 
00176                 IsCall(code) || 
00177                 code == RET ||
00178                 code == RETC;       
00179         }
00180 
00181         // 
00182         bool IsMem(int code)
00183         {
00184             return IsLoad(code) || IsStore(code);
00185         }
00186 
00187         // [h
00188         bool IsLoad(int code)
00189         {
00190             return code == iLD || code == fLD;
00191         }
00192 
00193         // XgA
00194         bool IsStore(int code)
00195         {
00196             return code == iST || code == fST;
00197         }
00198 
00199         // Address calculation
00200         bool IsAddr( int code )
00201         {
00202             return code == ADDR;
00203         }
00204 
00205         // 
00206         bool IsInt(int code)
00207         {
00208             return 
00209                 (code >= INT_BEGIN) &&
00210                 (code <= INT_END);
00211         }
00212 
00213         // _
00214         bool IsFloat(int code)
00215         {
00216             return 
00217                 (code >= FLOAT_BEGIN) && 
00218                 (code <= FLOAT_END);
00219         }
00220 
00221         //  <-> _
00222         bool IsIFConversion( int code )
00223         {
00224             return code == ifCONV;
00225         }
00226 
00227         // VXeR[PタsBA
00228         // VXeR[タsO^CAA
00229         // VXeR[^CAtFb`
00230         bool IsSyscall(int code)
00231         {
00232             return 
00233                 code == syscall ||
00234                 code == syscall_branch || 
00235                 code == UNDEF;  
00236         }
00237 
00238         bool IsNop(int code)
00239         {
00240             return
00241                 code == iNOP ||
00242                 code == fNOP;
00243         }
00244 
00245     }   // namespace OpClassCode
00246 
00247 }   // namespace Onikiri
00248     

Onikiri2に対してTue Jun 18 14:34:21 2013に生成されました。  doxygen 1.4.7