fp.isa revision 6383:31c067ae3331
1// -*- mode:c++ -*-
2
3// Copyright (c) 2007 MIPS Technologies, Inc.
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met: redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer;
10// redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution;
13// neither the name of the copyright holders nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29// Authors: Korey Sewell
30
31////////////////////////////////////////////////////////////////////
32//
33// Floating Point operate instructions
34//
35
36output header {{
37        /**
38         * Base class for FP operations.
39         */
40        class FPOp : public MipsStaticInst
41        {
42                protected:
43
44                /// Constructor
45                FPOp(const char *mnem, MachInst _machInst, OpClass __opClass) : MipsStaticInst(mnem, _machInst, __opClass)
46                {
47                }
48
49            //std::string generateDisassembly(Addr pc, const SymbolTable *symtab) const;
50
51                //needs function to check for fpEnable or not
52        };
53
54        class FPCompareOp : public FPOp
55        {
56          protected:
57            FPCompareOp(const char *mnem, MachInst _machInst, OpClass __opClass) : FPOp(mnem, _machInst, __opClass)
58                {
59                }
60
61            std::string generateDisassembly(Addr pc, const SymbolTable *symtab) const;
62
63        };
64}};
65
66output decoder {{
67        std::string FPCompareOp::generateDisassembly(Addr pc, const SymbolTable *symtab) const
68        {
69            std::stringstream ss;
70
71            ccprintf(ss, "%-10s ", mnemonic);
72
73            ccprintf(ss,"%d",CC);
74
75            if(_numSrcRegs > 0) {
76                ss << ", ";
77                printReg(ss, _srcRegIdx[0]);
78            }
79
80            if(_numSrcRegs > 1) {
81                ss << ", ";
82                printReg(ss, _srcRegIdx[1]);
83            }
84
85            return ss.str();
86        }
87}};
88
89output exec {{
90        inline Fault checkFpEnableFault(%(CPU_exec_context)s *xc)
91        {
92            //@TODO: Implement correct CP0 checks to see if the CP1
93            // unit is enable or not
94          if (!isCoprocessorEnabled(xc, 1))
95             return new CoprocessorUnusableFault(1);
96
97          return NoFault;
98        }
99
100        //If any operand is Nan return the appropriate QNaN
101        template <class T>
102        bool
103        fpNanOperands(FPOp *inst, %(CPU_exec_context)s *xc, const T &src_type,
104                      Trace::InstRecord *traceData)
105        {
106            uint64_t mips_nan = 0;
107            assert(sizeof(T) == 4);
108
109            for (int i = 0; i < inst->numSrcRegs(); i++) {
110                uint64_t src_bits = xc->readFloatRegOperandBits(inst, 0);
111
112                if (isNan(&src_bits, 32) ) {
113                    mips_nan = MIPS32_QNAN;
114                    xc->setFloatRegOperandBits(inst, 0, mips_nan);
115                    if (traceData) { traceData->setData(mips_nan); }
116                    return true;
117                }
118            }
119            return false;
120        }
121
122        template <class T>
123        bool
124        fpInvalidOp(FPOp *inst, %(CPU_exec_context)s *cpu, const T dest_val,
125                    Trace::InstRecord *traceData)
126        {
127            uint64_t mips_nan = 0;
128            T src_op = dest_val;
129            assert(sizeof(T) == 4);
130
131            if (isNan(&src_op, 32)) {
132                mips_nan = MIPS32_QNAN;
133
134                //Set value to QNAN
135                cpu->setFloatRegOperandBits(inst, 0, mips_nan);
136
137                //Read FCSR from FloatRegFile
138                uint32_t fcsr_bits =
139                    cpu->tcBase()->readFloatRegBits(FLOATREG_FCSR);
140
141                uint32_t new_fcsr = genInvalidVector(fcsr_bits);
142
143                //Write FCSR from FloatRegFile
144                cpu->tcBase()->setFloatRegBits(FLOATREG_FCSR, new_fcsr);
145
146                if (traceData) { traceData->setData(mips_nan); }
147                return true;
148            }
149
150            return false;
151        }
152
153        void
154        fpResetCauseBits(%(CPU_exec_context)s *cpu)
155        {
156            //Read FCSR from FloatRegFile
157            uint32_t fcsr = cpu->tcBase()->readFloatRegBits(FLOATREG_FCSR);
158
159            // TODO: Use utility function here
160            fcsr = bits(fcsr, 31, 18) << 18 | bits(fcsr, 11, 0);
161
162            //Write FCSR from FloatRegFile
163            cpu->tcBase()->setFloatRegBits(FLOATREG_FCSR, fcsr);
164        }
165}};
166
167def template FloatingPointExecute {{
168        Fault %(class_name)s::execute(%(CPU_exec_context)s *xc, Trace::InstRecord *traceData) const
169        {
170                Fault fault = NoFault;
171
172                %(fp_enable_check)s;
173
174
175                //When is the right time to reset cause bits?
176                //start of every instruction or every cycle?
177#if FULL_SYSTEM
178                fpResetCauseBits(xc);
179#endif
180                %(op_decl)s;
181                %(op_rd)s;
182
183                //Check if any FP operand is a NaN value
184                if (!fpNanOperands((FPOp*)this, xc, Fd, traceData)) {
185                    %(code)s;
186
187                    //Change this code for Full-System/Sycall Emulation
188                    //separation
189                    //----
190                    //Should Full System-Mode throw a fault here?
191                    //----
192                    //Check for IEEE 754 FP Exceptions
193                    //fault = fpNanOperands((FPOp*)this, xc, Fd, traceData);
194                    if (
195#if FULL_SYSTEM
196                        !fpInvalidOp((FPOp*)this, xc, Fd, traceData) &&
197#endif
198                        fault == NoFault)
199                    {
200                        %(op_wb)s;
201                    }
202                }
203
204                return fault;
205        }
206}};
207
208// Primary format for float point operate instructions:
209def format FloatOp(code, *flags) {{
210        iop = InstObjParams(name, Name, 'FPOp', code, flags)
211        header_output = BasicDeclare.subst(iop)
212        decoder_output = BasicConstructor.subst(iop)
213        decode_block = BasicDecode.subst(iop)
214        exec_output = FloatingPointExecute.subst(iop)
215}};
216
217def format FloatCompareOp(cond_code, *flags) {{
218    import sys
219
220    code = 'bool cond;\n'
221    if '.sf' in cond_code or 'SinglePrecision' in flags:
222        if 'QnanException' in flags:
223            code += 'if (isQnan(&Fs.sf, 32) || isQnan(&Ft.sf, 32)) {\n'
224            code += '\tFCSR = genInvalidVector(FCSR);\n'
225            code += '\treturn NoFault;'
226            code += '}\n else '
227        code += 'if (isNan(&Fs.sf, 32) || isNan(&Ft.sf, 32)) {\n'
228    elif '.df' in cond_code or 'DoublePrecision' in flags:
229        if 'QnanException' in flags:
230            code += 'if (isQnan(&Fs.df, 64) || isQnan(&Ft.df, 64)) {\n'
231            code += '\tFCSR = genInvalidVector(FCSR);\n'
232            code += '\treturn NoFault;'
233            code += '}\n else '
234        code += 'if (isNan(&Fs.df, 64) || isNan(&Ft.df, 64)) {\n'
235    else:
236       sys.exit('Decoder Failed: Can\'t Determine Operand Type\n')
237
238    if 'UnorderedTrue' in flags:
239       code += 'cond = 1;\n'
240    elif 'UnorderedFalse' in flags:
241       code += 'cond = 0;\n'
242    else:
243       sys.exit('Decoder Failed: Float Compare Instruction Needs A Unordered Flag\n')
244
245    code += '} else {\n'
246    code +=  cond_code + '}'
247    code += 'FCSR = genCCVector(FCSR, CC, cond);\n'
248
249    iop = InstObjParams(name, Name, 'FPCompareOp', code)
250    header_output = BasicDeclare.subst(iop)
251    decoder_output = BasicConstructor.subst(iop)
252    decode_block = BasicDecode.subst(iop)
253    exec_output = BasicExecute.subst(iop)
254}};
255
256def format FloatConvertOp(code, *flags) {{
257    import sys
258
259    #Determine Source Type
260    convert = 'fpConvert('
261    if '.sf' in code:
262        code = 'float ' + code + '\n'
263        convert += 'SINGLE_TO_'
264    elif '.df' in code:
265        code = 'double ' + code + '\n'
266        convert += 'DOUBLE_TO_'
267    elif '.uw' in code:
268        code = 'uint32_t ' + code + '\n'
269        convert += 'WORD_TO_'
270    elif '.ud' in code:
271        code = 'uint64_t ' + code + '\n'
272        convert += 'LONG_TO_'
273    else:
274        sys.exit("Error Determining Source Type for Conversion")
275
276    #Determine Destination Type
277    if 'ToSingle' in flags:
278        code += 'Fd.uw = ' + convert + 'SINGLE, '
279    elif 'ToDouble' in flags:
280        code += 'Fd.ud = ' + convert + 'DOUBLE, '
281    elif 'ToWord' in flags:
282        code += 'Fd.uw = ' + convert + 'WORD, '
283    elif 'ToLong' in flags:
284        code += 'Fd.ud = ' + convert + 'LONG, '
285    else:
286        sys.exit("Error Determining Destination Type for Conversion")
287
288    #Figure out how to round value
289    if 'Ceil' in flags:
290        code += 'ceil(val)); '
291    elif 'Floor' in flags:
292        code += 'floor(val)); '
293    elif 'Round' in flags:
294        code += 'roundFP(val, 0)); '
295    elif 'Trunc' in flags:
296        code += 'truncFP(val));'
297    else:
298        code += 'val); '
299
300    iop = InstObjParams(name, Name, 'FPOp', code)
301    header_output = BasicDeclare.subst(iop)
302    decoder_output = BasicConstructor.subst(iop)
303    decode_block = BasicDecode.subst(iop)
304    exec_output = BasicExecute.subst(iop)
305}};
306
307def format FloatAccOp(code, *flags) {{
308        iop = InstObjParams(name, Name, 'FPOp', code, flags)
309        header_output = BasicDeclare.subst(iop)
310        decoder_output = BasicConstructor.subst(iop)
311        decode_block = BasicDecode.subst(iop)
312        exec_output = BasicExecute.subst(iop)
313}};
314
315// Primary format for float64 operate instructions:
316def format Float64Op(code, *flags) {{
317        iop = InstObjParams(name, Name, 'MipsStaticInst', code, flags)
318        header_output = BasicDeclare.subst(iop)
319        decoder_output = BasicConstructor.subst(iop)
320        decode_block = BasicDecode.subst(iop)
321        exec_output = BasicExecute.subst(iop)
322}};
323
324def format FloatPSCompareOp(cond_code1, cond_code2, *flags) {{
325    import sys
326
327    code = 'bool cond1, cond2;\n'
328    code += 'bool code_block1, code_block2;\n'
329    code += 'code_block1 = code_block2 = true;\n'
330
331    if 'QnanException' in flags:
332        code += 'if (isQnan(&Fs1.sf, 32) || isQnan(&Ft1.sf, 32)) {\n'
333        code += '\tFCSR = genInvalidVector(FCSR);\n'
334        code += 'code_block1 = false;'
335        code += '}\n'
336        code += 'if (isQnan(&Fs2.sf, 32) || isQnan(&Ft2.sf, 32)) {\n'
337        code += '\tFCSR = genInvalidVector(FCSR);\n'
338        code += 'code_block2 = false;'
339        code += '}\n'
340
341    code += 'if (code_block1) {'
342    code += '\tif (isNan(&Fs1.sf, 32) || isNan(&Ft1.sf, 32)) {\n'
343    if 'UnorderedTrue' in flags:
344       code += 'cond1 = 1;\n'
345    elif 'UnorderedFalse' in flags:
346       code += 'cond1 = 0;\n'
347    else:
348       sys.exit('Decoder Failed: Float Compare Instruction Needs A Unordered Flag\n')
349    code += '} else {\n'
350    code +=  cond_code1
351    code += 'FCSR = genCCVector(FCSR, CC, cond1);}\n}\n'
352
353    code += 'if (code_block2) {'
354    code += '\tif (isNan(&Fs2.sf, 32) || isNan(&Ft2.sf, 32)) {\n'
355    if 'UnorderedTrue' in flags:
356       code += 'cond2 = 1;\n'
357    elif 'UnorderedFalse' in flags:
358       code += 'cond2 = 0;\n'
359    else:
360       sys.exit('Decoder Failed: Float Compare Instruction Needs A Unordered Flag\n')
361    code += '} else {\n'
362    code +=  cond_code2
363    code += 'FCSR = genCCVector(FCSR, CC, cond2);}\n}'
364
365    iop = InstObjParams(name, Name, 'FPCompareOp', code)
366    header_output = BasicDeclare.subst(iop)
367    decoder_output = BasicConstructor.subst(iop)
368    decode_block = BasicDecode.subst(iop)
369    exec_output = BasicExecute.subst(iop)
370}};
371
372