static_inst.cc revision 7087
1/*
2 * Copyright (c) 2007 The Hewlett-Packard Development Company
3 * All rights reserved.
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Gabe Black
38 */
39
40#include "arch/x86/insts/static_inst.hh"
41#include "arch/x86/segmentregs.hh"
42
43namespace X86ISA
44{
45    void X86StaticInst::printMnemonic(std::ostream &os,
46            const char * mnemonic) const
47    {
48        ccprintf(os, "  %s   ", mnemonic);
49    }
50
51    void X86StaticInst::printMnemonic(std::ostream &os,
52            const char * instMnemonic, const char * mnemonic) const
53    {
54        ccprintf(os, "  %s : %s   ", instMnemonic, mnemonic);
55    }
56
57    void X86StaticInst::printSegment(std::ostream &os, int segment) const
58    {
59        switch (segment)
60        {
61          case SEGMENT_REG_ES:
62            ccprintf(os, "ES");
63            break;
64          case SEGMENT_REG_CS:
65            ccprintf(os, "CS");
66            break;
67          case SEGMENT_REG_SS:
68            ccprintf(os, "SS");
69            break;
70          case SEGMENT_REG_DS:
71            ccprintf(os, "DS");
72            break;
73          case SEGMENT_REG_FS:
74            ccprintf(os, "FS");
75            break;
76          case SEGMENT_REG_GS:
77            ccprintf(os, "GS");
78            break;
79          case SEGMENT_REG_HS:
80            ccprintf(os, "HS");
81            break;
82          case SEGMENT_REG_TSL:
83            ccprintf(os, "TSL");
84            break;
85          case SEGMENT_REG_TSG:
86            ccprintf(os, "TSG");
87            break;
88          case SEGMENT_REG_LS:
89            ccprintf(os, "LS");
90            break;
91          case SEGMENT_REG_MS:
92            ccprintf(os, "MS");
93            break;
94          case SYS_SEGMENT_REG_TR:
95            ccprintf(os, "TR");
96            break;
97          case SYS_SEGMENT_REG_IDTR:
98            ccprintf(os, "IDTR");
99            break;
100          default:
101            panic("Unrecognized segment %d\n", segment);
102        }
103    }
104
105    void
106    X86StaticInst::printSrcReg(std::ostream &os, int reg, int size) const
107    {
108        if(_numSrcRegs > reg)
109            printReg(os, _srcRegIdx[reg], size);
110    }
111
112    void
113    X86StaticInst::printDestReg(std::ostream &os, int reg, int size) const
114    {
115        if(_numDestRegs > reg)
116            printReg(os, _destRegIdx[reg], size);
117    }
118
119    void
120    X86StaticInst::printReg(std::ostream &os, int reg, int size) const
121    {
122        assert(size == 1 || size == 2 || size == 4 || size == 8);
123        static const char * abcdFormats[9] =
124            {"", "%s",  "%sx",  "", "e%sx", "", "", "", "r%sx"};
125        static const char * piFormats[9] =
126            {"", "%s",  "%s",   "", "e%s",  "", "", "", "r%s"};
127        static const char * longFormats[9] =
128            {"", "r%sb", "r%sw", "", "r%sd", "", "", "", "r%s"};
129        static const char * microFormats[9] =
130            {"", "t%db", "t%dw", "", "t%dd", "", "", "", "t%d"};
131
132        if (reg < FP_Base_DepTag) {
133            const char * suffix = "";
134            bool fold = reg & IntFoldBit;
135            reg &= ~IntFoldBit;
136
137            if(fold)
138                suffix = "h";
139            else if(reg < 8 && size == 1)
140                suffix = "l";
141
142            switch (reg) {
143              case INTREG_RAX:
144                ccprintf(os, abcdFormats[size], "a");
145                break;
146              case INTREG_RBX:
147                ccprintf(os, abcdFormats[size], "b");
148                break;
149              case INTREG_RCX:
150                ccprintf(os, abcdFormats[size], "c");
151                break;
152              case INTREG_RDX:
153                ccprintf(os, abcdFormats[size], "d");
154                break;
155              case INTREG_RSP:
156                ccprintf(os, piFormats[size], "sp");
157                break;
158              case INTREG_RBP:
159                ccprintf(os, piFormats[size], "bp");
160                break;
161              case INTREG_RSI:
162                ccprintf(os, piFormats[size], "si");
163                break;
164              case INTREG_RDI:
165                ccprintf(os, piFormats[size], "di");
166                break;
167              case INTREG_R8W:
168                ccprintf(os, longFormats[size], "8");
169                break;
170              case INTREG_R9W:
171                ccprintf(os, longFormats[size], "9");
172                break;
173              case INTREG_R10W:
174                ccprintf(os, longFormats[size], "10");
175                break;
176              case INTREG_R11W:
177                ccprintf(os, longFormats[size], "11");
178                break;
179              case INTREG_R12W:
180                ccprintf(os, longFormats[size], "12");
181                break;
182              case INTREG_R13W:
183                ccprintf(os, longFormats[size], "13");
184                break;
185              case INTREG_R14W:
186                ccprintf(os, longFormats[size], "14");
187                break;
188              case INTREG_R15W:
189                ccprintf(os, longFormats[size], "15");
190                break;
191              default:
192                ccprintf(os, microFormats[size], reg - NUM_INTREGS);
193            }
194            ccprintf(os, suffix);
195        } else if (reg < Ctrl_Base_DepTag) {
196            int fpindex = reg - FP_Base_DepTag;
197            if(fpindex < NumMMXRegs) {
198                ccprintf(os, "%%mmx%d", reg - FP_Base_DepTag);
199                return;
200            }
201            fpindex -= NumMMXRegs;
202            if(fpindex < NumXMMRegs * 2) {
203                ccprintf(os, "%%xmm%d_%s", fpindex / 2,
204                        (fpindex % 2) ? "high": "low");
205                return;
206            }
207            fpindex -= NumXMMRegs * 2;
208            if(fpindex < NumMicroFpRegs) {
209                ccprintf(os, "%%ufp%d", fpindex);
210                return;
211            }
212            fpindex -= NumMicroFpRegs;
213            ccprintf(os, "%%st(%d)", fpindex);
214        } else {
215            switch (reg - Ctrl_Base_DepTag) {
216              default:
217                ccprintf(os, "%%ctrl%d", reg - Ctrl_Base_DepTag);
218            }
219        }
220    }
221
222    void X86StaticInst::printMem(std::ostream &os, uint8_t segment,
223            uint8_t scale, RegIndex index, RegIndex base,
224            uint64_t disp, uint8_t addressSize, bool rip) const
225    {
226        bool someAddr = false;
227        printSegment(os, segment);
228        os << ":[";
229        if (rip) {
230            os << "rip";
231            someAddr = true;
232        } else {
233            if (scale != 0 && index != ZeroReg)
234            {
235                if(scale != 1)
236                    ccprintf(os, "%d*", scale);
237                printReg(os, index, addressSize);
238                someAddr = true;
239            }
240            if (base != ZeroReg)
241            {
242                if(someAddr)
243                    os << " + ";
244                printReg(os, base, addressSize);
245                someAddr = true;
246            }
247        }
248        if (disp != 0)
249        {
250            if(someAddr)
251                os << " + ";
252            ccprintf(os, "%#x", disp);
253            someAddr = true;
254        }
255        if (!someAddr)
256            os << "0";
257        os << "]";
258    }
259
260    std::string X86StaticInst::generateDisassembly(Addr pc,
261        const SymbolTable *symtab) const
262    {
263        std::stringstream ss;
264
265        printMnemonic(ss, mnemonic);
266
267        return ss.str();
268    }
269}
270