1// Copyright (c) 2007 The Hewlett-Packard Development Company
2// Copyright (c) 2012-2013 Mark D. Hill and David A. Wood
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// Nilay Vaish
39
40//////////////////////////////////////////////////////////////////////////
41//
42// FpOp Microop templates
43//
44//////////////////////////////////////////////////////////////////////////
45
46def template MicroFpOpExecute {{
47 Fault %(class_name)s::execute(%(CPU_exec_context)s *xc,
48 Trace::InstRecord *traceData) const
49 {
50 Fault fault = NoFault;
51
52 DPRINTF(X86, "The data size is %d\n", dataSize);
53 %(op_decl)s;
54 %(op_rd)s;
55
56 if(%(cond_check)s)
57 {
58 %(code)s;
59 %(flag_code)s;
60 %(top_code)s;
61 }
62 else
63 {
64 %(else_code)s;
65 }
66
67 //Write the resulting state to the execution context
68 if(fault == NoFault)
69 {
70 %(op_wb)s;
71 }
72 return fault;
73 }
74}};
75
76def template MicroFpOpDeclare {{
77 class %(class_name)s : public %(base_class)s
78 {
79 public:
80 %(class_name)s(ExtMachInst _machInst,
81 const char * instMnem, uint64_t setFlags,
82 InstRegIndex _src1, InstRegIndex _src2, InstRegIndex _dest,
83 uint8_t _dataSize, int8_t _spm);
84
85 %(BasicExecDeclare)s
86 };
87}};
88
89def template MicroFpOpConstructor {{
90 inline %(class_name)s::%(class_name)s(
91 ExtMachInst machInst, const char * instMnem, uint64_t setFlags,
92 InstRegIndex _src1, InstRegIndex _src2, InstRegIndex _dest,
93 uint8_t _dataSize, int8_t _spm) :
94 %(base_class)s(machInst, "%(mnemonic)s", instMnem, setFlags,
95 _src1, _src2, _dest, _dataSize, _spm,
96 %(op_class)s)
97 {
98 %(constructor)s;
99 }
100}};
101
102let {{
103 # Make these empty strings so that concatenating onto
104 # them will always work.
105 header_output = ""
106 decoder_output = ""
107 exec_output = ""
108
109 class FpOpMeta(type):
110 def buildCppClasses(self, name, Name, suffix, \
111 code, flag_code, cond_check, else_code):
112
113 # Globals to stick the output in
114 global header_output
115 global decoder_output
116 global exec_output
117
118 # Stick all the code together so it can be searched at once
119 allCode = "|".join((code, flag_code, cond_check, else_code))
120
121 # If there's something optional to do with flags, generate
122 # a version without it and fix up this version to use it.
123 if flag_code is not "" or cond_check is not "true":
124 self.buildCppClasses(name, Name, suffix,
125 code, "", "true", else_code)
126 suffix = "Flags" + suffix
127
128 base = "X86ISA::FpOp"
129
130 # Get everything ready for the substitution
131 iop_top = InstObjParams(name, Name + suffix + "Top", base,
132 {"code" : code,
133 "flag_code" : flag_code,
134 "cond_check" : cond_check,
135 "else_code" : else_code,
136 "top_code" : "TOP = (TOP + spm + 8) % 8;"})
137 iop = InstObjParams(name, Name + suffix, base,
138 {"code" : code,
139 "flag_code" : flag_code,
140 "cond_check" : cond_check,
141 "else_code" : else_code,
142 "top_code" : ";"})
143
144 # Generate the actual code (finally!)
145 header_output += MicroFpOpDeclare.subst(iop_top)
146 decoder_output += MicroFpOpConstructor.subst(iop_top)
147 exec_output += MicroFpOpExecute.subst(iop_top)
148 header_output += MicroFpOpDeclare.subst(iop)
149 decoder_output += MicroFpOpConstructor.subst(iop)
150 exec_output += MicroFpOpExecute.subst(iop)
151
152
153 def __new__(mcls, Name, bases, dict):
154 abstract = False
155 name = Name.lower()
156 if "abstract" in dict:
157 abstract = dict['abstract']
158 del dict['abstract']
159
160 cls = super(FpOpMeta, mcls).__new__(mcls, Name, bases, dict)
161 if not abstract:
162 cls.className = Name
163 cls.mnemonic = name
164 code = cls.code
165 flag_code = cls.flag_code
166 cond_check = cls.cond_check
167 else_code = cls.else_code
168
169 # Set up the C++ classes
170 mcls.buildCppClasses(cls, name, Name, "",
171 code, flag_code, cond_check, else_code)
172
173 # Hook into the microassembler dict
174 global microopClasses
175 microopClasses[name] = cls
176
177 return cls
178
179 class FpUnaryOp(X86Microop):
180 __metaclass__ = FpOpMeta
181 # This class itself doesn't act as a microop
182 abstract = True
183
184 # Default template parameter values
185 flag_code = ""
186 cond_check = "true"
187 else_code = ";"
188
189 def __init__(self, dest, src1, spm=0, \
190 SetStatus=False, dataSize="env.dataSize"):
191 self.dest = dest
192 self.src1 = src1
193 self.src2 = "InstRegIndex(0)"
194 self.spm = spm
195 self.dataSize = dataSize
196 if SetStatus:
197 self.className += "Flags"
198 if spm:
199 self.className += "Top"
200
201 def getAllocator(self, microFlags):
202 return '''new %(class_name)s(machInst, macrocodeBlock,
203 %(flags)s, %(src1)s, %(src2)s, %(dest)s,
204 %(dataSize)s, %(spm)d)''' % {
205 "class_name" : self.className,
206 "flags" : self.microFlagsText(microFlags),
207 "src1" : self.src1, "src2" : self.src2,
208 "dest" : self.dest,
209 "dataSize" : self.dataSize,
210 "spm" : self.spm}
211
212 class FpBinaryOp(X86Microop):
213 __metaclass__ = FpOpMeta
214 # This class itself doesn't act as a microop
215 abstract = True
216
217 # Default template parameter values
218 flag_code = ""
219 cond_check = "true"
220 else_code = ";"
221
222 def __init__(self, dest, src1, src2, spm=0, \
223 SetStatus=False, dataSize="env.dataSize"):
224 self.dest = dest
225 self.src1 = src1
226 self.src2 = src2
227 self.spm = spm
228 self.dataSize = dataSize
229 if SetStatus:
230 self.className += "Flags"
231 if spm:
232 self.className += "Top"
233
234 def getAllocator(self, microFlags):
235 return '''new %(class_name)s(machInst, macrocodeBlock,
236 %(flags)s, %(src1)s, %(src2)s, %(dest)s,
237 %(dataSize)s, %(spm)d)''' % {
238 "class_name" : self.className,
239 "flags" : self.microFlagsText(microFlags),
240 "src1" : self.src1, "src2" : self.src2,
241 "dest" : self.dest,
242 "dataSize" : self.dataSize,
243 "spm" : self.spm}
244
245 class Movfp(FpUnaryOp):
246 code = 'FpDestReg_uqw = FpSrcReg1_uqw;'
247 else_code = 'FpDestReg_uqw = FpDestReg_uqw;'
248 cond_check = "checkCondition(ccFlagBits | cfofBits | dfBit | \
249 ecfBit | ezfBit, src2)"
250
251 class Xorfp(FpBinaryOp):
252 code = 'FpDestReg_uqw = FpSrcReg1_uqw ^ FpSrcReg2_uqw;'
253
254 class Sqrtfp(FpBinaryOp):
255 code = 'FpDestReg = sqrt(FpSrcReg2);'
256
257 class Cosfp(FpUnaryOp):
258 code = 'FpDestReg = cos(FpSrcReg1);'
259
260 class Sinfp(FpUnaryOp):
261 code = 'FpDestReg = sin(FpSrcReg1);'
262
263 class Tanfp(FpUnaryOp):
264 code = 'FpDestReg = tan(FpSrcReg1);'
265
266
267 # Conversion microops
268 class ConvOp(FpBinaryOp):
269 abstract = True
270 def __init__(self, dest, src1):
271 super(ConvOp, self).__init__(dest, src1, \
272 "InstRegIndex(FLOATREG_MICROFP0)")
273
274 # These probably shouldn't look at the ExtMachInst directly to figure
275 # out what size to use and should instead delegate that to the macroop's
276 # constructor. That would be more efficient, and it would make the
277 # microops a little more modular.
278 class cvtf_i2d(ConvOp):
279 code = '''
280 X86IntReg intReg = SSrcReg1;
281 if (REX_W)
282 FpDestReg = intReg.SR;
283 else
284 FpDestReg = intReg.SE;
285 '''
286
287 class cvtf_i2d_hi(ConvOp):
288 code = 'FpDestReg = bits(SSrcReg1, 63, 32);'
289
290 class cvtf_d2i(ConvOp):
291 code = '''
292 int64_t intSrcReg1 = static_cast<int64_t>(FpSrcReg1);
293 if (REX_W)
294 SDestReg = intSrcReg1;
295 else
296 SDestReg = merge(SDestReg, intSrcReg1, 4);
297 '''
298
299 # These need to consider size at some point. They'll always use doubles
300 # for the moment.
301 class addfp(FpBinaryOp):
302 code = 'FpDestReg = FpSrcReg1 + FpSrcReg2;'
303
304 class mulfp(FpBinaryOp):
305 code = 'FpDestReg = FpSrcReg1 * FpSrcReg2;'
306
307 class divfp(FpBinaryOp):
308 code = 'FpDestReg = FpSrcReg1 / FpSrcReg2;'
309
310 class subfp(FpBinaryOp):
311 code = 'FpDestReg = FpSrcReg1 - FpSrcReg2;'
312
313 class Yl2xFp(FpBinaryOp):
314 code = '''
315 FpDestReg = FpSrcReg2 * (log(FpSrcReg1) / log(2));
316 '''
317
318 class PremFp(FpBinaryOp):
319 code = '''
320 FpDestReg = fmod(FpSrcReg1, FpSrcReg2);
321 DPRINTF(X86, "src1: %lf, src2: %lf, dest: %lf\\n", FpSrcReg1, FpSrcReg2, FpDestReg);
322 '''
323
324 class Compfp(FpBinaryOp):
325 def __init__(self, src1, src2, spm=0, setStatus=False, \
326 dataSize="env.dataSize"):
327 super(Compfp, self).__init__("InstRegIndex(FLOATREG_MICROFP0)", \
328 src1, src2, spm, setStatus, dataSize)
329 # This class sets the condition codes in rflags according to the
330 # rules for comparing floating point.
331 code = '''
332 // ZF PF CF
333 // Unordered 1 1 1
334 // Greater than 0 0 0
335 // Less than 0 0 1
336 // Equal 1 0 0
337 // OF = SF = AF = 0
338 ccFlagBits = ccFlagBits & ~(SFBit | AFBit | ZFBit | PFBit);
339 cfofBits = cfofBits & ~(OFBit | CFBit);
340
341 if (std::isnan(FpSrcReg1) || std::isnan(FpSrcReg2)) {
342 ccFlagBits = ccFlagBits | (ZFBit | PFBit);
343 cfofBits = cfofBits | CFBit;
344 }
345 else if(FpSrcReg1 < FpSrcReg2)
346 cfofBits = cfofBits | CFBit;
347 else if(FpSrcReg1 == FpSrcReg2)
348 ccFlagBits = ccFlagBits | ZFBit;
349 '''
350
351 class absfp(FpUnaryOp):
352 code = 'FpDestReg = fabs(FpSrcReg1);'
353 flag_code = 'FSW &= (~CC1Bit);'
354
355 class chsfp(FpUnaryOp):
356 code = 'FpDestReg = (-1) * (FpSrcReg1);'
357 flag_code = 'FSW &= (~CC1Bit);'
358}};