static_inst.cc revision 5202:ff56fa8c2091
1/*
2 * Copyright (c) 2007 The Hewlett-Packard Development Company
3 * All rights reserved.
4 *
5 * Redistribution and use of this software in source and binary forms,
6 * with or without modification, are permitted provided that the
7 * following conditions are met:
8 *
9 * The software must be used only for Non-Commercial Use which means any
10 * use which is NOT directed to receiving any direct monetary
11 * compensation for, or commercial advantage from such use.  Illustrative
12 * examples of non-commercial use are academic research, personal study,
13 * teaching, education and corporate research & development.
14 * Illustrative examples of commercial use are distributing products for
15 * commercial advantage and providing services using the software for
16 * commercial advantage.
17 *
18 * If you wish to use this software or functionality therein that may be
19 * covered by patents for commercial use, please contact:
20 *     Director of Intellectual Property Licensing
21 *     Office of Strategy and Technology
22 *     Hewlett-Packard Company
23 *     1501 Page Mill Road
24 *     Palo Alto, California  94304
25 *
26 * Redistributions of source code must retain the above copyright notice,
27 * this list of conditions and the following disclaimer.  Redistributions
28 * in binary form must reproduce the above copyright notice, this list of
29 * conditions and the following disclaimer in the documentation and/or
30 * other materials provided with the distribution.  Neither the name of
31 * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
32 * contributors may be used to endorse or promote products derived from
33 * this software without specific prior written permission.  No right of
34 * sublicense is granted herewith.  Derivatives of the software and
35 * output created using the software may be prepared, but only for
36 * Non-Commercial Uses.  Derivatives of the software may be shared with
37 * others provided: (i) the others agree to abide by the list of
38 * conditions herein which includes the Non-Commercial Use restrictions;
39 * and (ii) such Derivatives of the software include the above copyright
40 * notice to acknowledge the contribution from this software where
41 * applicable, this list of conditions and the disclaimer below.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 *
55 * Authors: Gabe Black
56 */
57
58#include "arch/x86/insts/static_inst.hh"
59
60namespace X86ISA
61{
62    void X86StaticInst::printMnemonic(std::ostream &os,
63            const char * mnemonic) const
64    {
65        ccprintf(os, "\t%s   ", mnemonic);
66    }
67
68    void X86StaticInst::printMnemonic(std::ostream &os,
69            const char * instMnemonic, const char * mnemonic) const
70    {
71        ccprintf(os, "\t%s : %s   ", instMnemonic, mnemonic);
72    }
73
74    void X86StaticInst::printSegment(std::ostream &os, int segment) const
75    {
76        switch (segment)
77        {
78          case 0:
79            ccprintf(os, "ES");
80            break;
81          case 1:
82            ccprintf(os, "CS");
83            break;
84          case 2:
85            ccprintf(os, "SS");
86            break;
87          case 3:
88            ccprintf(os, "DS");
89            break;
90          case 4:
91            ccprintf(os, "FS");
92            break;
93          case 5:
94            ccprintf(os, "GS");
95            break;
96          default:
97            panic("Unrecognized segment %d\n", segment);
98        }
99    }
100
101    void
102    X86StaticInst::printSrcReg(std::ostream &os, int reg, int size) const
103    {
104        if(_numSrcRegs > reg)
105            printReg(os, _srcRegIdx[reg], size);
106    }
107
108    void
109    X86StaticInst::printDestReg(std::ostream &os, int reg, int size) const
110    {
111        if(_numDestRegs > reg)
112            printReg(os, _destRegIdx[reg], size);
113    }
114
115    void
116    X86StaticInst::printReg(std::ostream &os, int reg, int size) const
117    {
118        assert(size == 1 || size == 2 || size == 4 || size == 8);
119        static const char * abcdFormats[9] =
120            {"", "%s",  "%sx",  "", "e%sx", "", "", "", "r%sx"};
121        static const char * piFormats[9] =
122            {"", "%s",  "%s",   "", "e%s",  "", "", "", "r%s"};
123        static const char * longFormats[9] =
124            {"", "r%sb", "r%sw", "", "r%sd", "", "", "", "r%s"};
125        static const char * microFormats[9] =
126            {"", "t%db", "t%dw", "", "t%dd", "", "", "", "t%d"};
127
128        if (reg < FP_Base_DepTag) {
129            const char * suffix = "";
130            bool fold = reg & (1 << 6);
131            reg &= ~(1 << 6);
132
133            if(fold)
134            {
135                suffix = "h";
136                reg -= 4;
137            }
138            else if(reg < 8 && size == 1)
139                suffix = "l";
140
141            switch (reg) {
142              case INTREG_RAX:
143                ccprintf(os, abcdFormats[size], "a");
144                break;
145              case INTREG_RBX:
146                ccprintf(os, abcdFormats[size], "b");
147                break;
148              case INTREG_RCX:
149                ccprintf(os, abcdFormats[size], "c");
150                break;
151              case INTREG_RDX:
152                ccprintf(os, abcdFormats[size], "d");
153                break;
154              case INTREG_RSP:
155                ccprintf(os, piFormats[size], "sp");
156                break;
157              case INTREG_RBP:
158                ccprintf(os, piFormats[size], "bp");
159                break;
160              case INTREG_RSI:
161                ccprintf(os, piFormats[size], "si");
162                break;
163              case INTREG_RDI:
164                ccprintf(os, piFormats[size], "di");
165                break;
166              case INTREG_R8W:
167                ccprintf(os, longFormats[size], "8");
168                break;
169              case INTREG_R9W:
170                ccprintf(os, longFormats[size], "9");
171                break;
172              case INTREG_R10W:
173                ccprintf(os, longFormats[size], "10");
174                break;
175              case INTREG_R11W:
176                ccprintf(os, longFormats[size], "11");
177                break;
178              case INTREG_R12W:
179                ccprintf(os, longFormats[size], "12");
180                break;
181              case INTREG_R13W:
182                ccprintf(os, longFormats[size], "13");
183                break;
184              case INTREG_R14W:
185                ccprintf(os, longFormats[size], "14");
186                break;
187              case INTREG_R15W:
188                ccprintf(os, longFormats[size], "15");
189                break;
190              default:
191                ccprintf(os, microFormats[size], reg - NUM_INTREGS);
192            }
193            ccprintf(os, suffix);
194        } else if (reg < Ctrl_Base_DepTag) {
195            int fpindex = reg - FP_Base_DepTag;
196            if(fpindex < NumMMXRegs) {
197                ccprintf(os, "%%mmx%d", reg - FP_Base_DepTag);
198                return;
199            }
200            fpindex -= NumMMXRegs;
201            if(fpindex < NumXMMRegs * 2) {
202                ccprintf(os, "%%xmm%d_%s", fpindex / 2,
203                        (fpindex % 2) ? "high": "low");
204                return;
205            }
206            fpindex -= NumXMMRegs * 2;
207            if(fpindex < NumMicroFpRegs) {
208                ccprintf(os, "%%ufp%d", fpindex);
209                return;
210            }
211            fpindex -= NumMicroFpRegs;
212            ccprintf(os, "%%st(%d)", fpindex);
213        } else {
214            switch (reg - Ctrl_Base_DepTag) {
215              default:
216                ccprintf(os, "%%ctrl%d", reg - Ctrl_Base_DepTag);
217            }
218        }
219    }
220
221    std::string X86StaticInst::generateDisassembly(Addr pc,
222        const SymbolTable *symtab) const
223    {
224        std::stringstream ss;
225
226        printMnemonic(ss, mnemonic);
227
228        return ss.str();
229    }
230}
231