3 // Copyright (c) 2007 The Hewlett-Packard Development Company
4 // All rights reserved.
6 // Redistribution and use of this software in source and binary forms,
7 // with or without modification, are permitted provided that the
8 // following conditions are met:
10 // The software must be used only for Non-Commercial Use which means any
11 // use which is NOT directed to receiving any direct monetary
12 // compensation for, or commercial advantage from such use. Illustrative
13 // examples of non-commercial use are academic research, personal study,
14 // teaching, education and corporate research & development.
15 // Illustrative examples of commercial use are distributing products for
16 // commercial advantage and providing services using the software for
17 // commercial advantage.
19 // If you wish to use this software or functionality therein that may be
20 // covered by patents for commercial use, please contact:
21 // Director of Intellectual Property Licensing
22 // Office of Strategy and Technology
23 // Hewlett-Packard Company
24 // 1501 Page Mill Road
25 // Palo Alto, California 94304
27 // Redistributions of source code must retain the above copyright notice,
28 // this list of conditions and the following disclaimer. Redistributions
29 // in binary form must reproduce the above copyright notice, this list of
30 // conditions and the following disclaimer in the documentation and/or
31 // other materials provided with the distribution. Neither the name of
32 // the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
33 // contributors may be used to endorse or promote products derived from
34 // this software without specific prior written permission. No right of
35 // sublicense is granted herewith. Derivatives of the software and
36 // output created using the software may be prepared, but only for
37 // Non-Commercial Uses. Derivatives of the software may be shared with
38 // others provided: (i) the others agree to abide by the list of
39 // conditions herein which includes the Non-Commercial Use restrictions;
40 // and (ii) such Derivatives of the software include the above copyright
41 // notice to acknowledge the contribution from this software where
42 // applicable, this list of conditions and the disclaimer below.
44 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
45 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
46 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
47 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
48 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
54 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 // Authors: Gabe Black
58 //////////////////////////////////////////////////////////////////////////////
60 // Architecture independent
63 // Execute method for macroops.
64 def template MacroExecPanic {{
65 Fault execute(%(CPU_exec_context)s *, Trace::InstRecord *) const
67 panic("Tried to execute macroop directly!");
74 // Base class for combinationally generated macroops
75 class Macroop : public X86ISA::MacroopBase
78 Macroop(const char *mnem, ExtMachInst _machInst,
79 uint32_t _numMicroops, X86ISA::EmulEnv _env)
80 : MacroopBase(mnem, _machInst, _numMicroops, _env)
86 //////////////////////////////////////////////////////////////////////////////
90 //////////////////////////////////////////////////////////////////////////////
92 // Basic instruction class declaration template.
93 def template MacroDeclare {{
97 * Static instruction class for "%(mnemonic)s".
99 class %(class_name)s : public %(base_class)s
105 %(class_name)s(ExtMachInst machInst, X86ISA::EmulEnv _env);
108 generateDisassembly(Addr pc, const SymbolTable *symtab) const;
113 def template MacroDisassembly {{
115 X86Macroop::%(class_name)s::generateDisassembly(Addr pc,
116 const SymbolTable *symtab) const
118 std::stringstream out;
119 out << mnemonic << "\t";
121 int regSize = %(regSize)s;
129 // Basic instruction class constructor template.
130 def template MacroConstructor {{
131 inline X86Macroop::%(class_name)s::%(class_name)s(
132 ExtMachInst machInst, EmulEnv _env)
133 : %(base_class)s("%(mnemonic)s", machInst, %(num_microops)s, _env)
140 const char *macrocodeBlock = "%(class_name)s";
141 //alloc_microops is the code that sets up the microops
142 //array in the parent class.
148 from micro_asm import Combinational_Macroop, Rom_Macroop
149 class X86Macroop(Combinational_Macroop):
150 def add_microop(self, mnemonic, microop):
151 microop.mnemonic = mnemonic
152 microop.micropc = len(self.microops)
153 self.microops.append(microop)
154 def setAdjustEnv(self, val):
155 self.adjust_env = val
156 def adjustImm(self, val):
157 self.adjust_imm += val
158 def adjustDisp(self, val):
159 self.adjust_disp += val
160 def __init__(self, name):
161 super(X86Macroop, self).__init__(name)
163 "adjust_env" : self.setAdjustEnv,
164 "adjust_imm" : self.adjustImm,
165 "adjust_disp" : self.adjustDisp
167 self.declared = False
170 self.adjust_imm = '''
171 uint64_t adjustedImm = IMMEDIATE;
172 //This is to pacify gcc in case the immediate isn't used.
173 adjustedImm = adjustedImm;
175 self.adjust_disp = '''
176 uint64_t adjustedDisp = DISPLACEMENT;
177 //This is to pacify gcc in case the displacement isn't used.
178 adjustedDisp = adjustedDisp;
180 def getAllocator(self, env):
181 return "new X86Macroop::%s(machInst, %s)" % \
182 (self.name, env.getAllocator())
183 def getMnemonic(self):
184 mnemonic = self.name.lower()
185 mnemonic = re.match(r'[^_]*', mnemonic).group(0)
187 def getDeclaration(self):
188 #FIXME This first parameter should be the mnemonic. I need to
189 #write some code which pulls that out
191 for (label, microop) in self.labels.items():
192 declareLabels += "const static uint64_t label_%s = %d;\n" \
193 % (label, microop.micropc)
194 iop = InstObjParams(self.getMnemonic(), self.name, "Macroop",
196 "declareLabels" : declareLabels
198 return MacroDeclare.subst(iop);
199 def getDefinition(self, env):
200 #FIXME This first parameter should be the mnemonic. I need to
201 #write some code which pulls that out
202 numMicroops = len(self.microops)
205 for op in self.microops:
206 isLast = (micropc == numMicroops - 1)
208 "microops[%d] = %s;\n" % \
209 (micropc, op.getAllocator(True, not isLast,
210 micropc == 0, isLast))
213 useStackSize = "true"
215 useStackSize = "false"
220 regSize = '''(%s || (env.base == INTREG_RSP && %s) ?
222 env.dataSize)''' % (useStackSize, memoryInst)
223 iop = InstObjParams(self.getMnemonic(), self.name, "Macroop",
224 {"code" : "", "num_microops" : numMicroops,
225 "alloc_microops" : allocMicroops,
226 "adjust_env" : self.adjust_env,
227 "adjust_imm" : self.adjust_imm,
228 "adjust_disp" : self.adjust_disp,
229 "disassembly" : env.disassembly,
231 "init_env" : self.initEnv})
232 return MacroConstructor.subst(iop) + \
233 MacroDisassembly.subst(iop);
237 class EmulEnv(object):
242 self.regmUsed = False
243 self.seg = "SEGMENT_REG_DS"
245 self.addressSize = "ADDRSIZE"
246 self.dataSize = "OPSIZE"
247 self.stackSize = "STACKSIZE"
249 self.disassembly = ""
250 self.firstArgument = True
251 self.useStackSize = False
252 self.memoryInst = False
254 def addToDisassembly(self, code):
255 if not self.firstArgument:
256 self.disassembly += "out << \", \";\n"
257 self.firstArgument = False
258 self.disassembly += code
260 def getAllocator(self):
263 elif self.size == 'd':
265 #This is for "double plus" which is normally a double word unless
266 #the REX W bit is set, in which case it's a quad word. It's used
267 #for some SSE instructions.
268 elif self.size == 'dp':
269 self.dataSize = "(REX_W ? 8 : 4)"
270 elif self.size == 'q':
272 elif self.size == 'v':
273 self.dataSize = "OPSIZE"
274 elif self.size == 'w':
276 elif self.size == 'z':
277 self.dataSize = "((OPSIZE == 8) ? 4 : OPSIZE)"
279 raise Exception, "Unrecognized size type %s!" % self.size
280 return '''EmulEnv(%(reg)s,
284 %(stackSize)s)''' % \
287 def addReg(self, reg):
291 elif not self.regmUsed:
295 raise Exception, "EmulEnv is out of register specialization spots."
296 def setSize(self, size):
300 if self.size != size:
301 raise Exception, "Conflicting register sizes %s and %s!" %\
306 doModRMString = "env.doModRM(machInst);\n"
307 noModRMString = "env.setSeg(machInst);\n"
308 def genMacroop(Name, env):
309 blocks = OutputBlocks()
310 if not macroopDict.has_key(Name):
311 raise Exception, "Unrecognized instruction: %s" % Name
312 macroop = macroopDict[Name]
313 if not macroop.declared:
315 macroop.initEnv = doModRMString
317 macroop.initEnv = noModRMString
318 blocks.header_output = macroop.getDeclaration()
319 blocks.decoder_output = macroop.getDefinition(env)
320 macroop.declared = True
321 blocks.decode_block = "return %s;\n" % macroop.getAllocator(env)