regop.isa revision 5246
14519Sgblack@eecs.umich.edu// Copyright (c) 2007 The Hewlett-Packard Development Company 24519Sgblack@eecs.umich.edu// All rights reserved. 34519Sgblack@eecs.umich.edu// 44519Sgblack@eecs.umich.edu// Redistribution and use of this software in source and binary forms, 54519Sgblack@eecs.umich.edu// with or without modification, are permitted provided that the 64519Sgblack@eecs.umich.edu// following conditions are met: 74519Sgblack@eecs.umich.edu// 84519Sgblack@eecs.umich.edu// The software must be used only for Non-Commercial Use which means any 94519Sgblack@eecs.umich.edu// use which is NOT directed to receiving any direct monetary 104519Sgblack@eecs.umich.edu// compensation for, or commercial advantage from such use. Illustrative 114519Sgblack@eecs.umich.edu// examples of non-commercial use are academic research, personal study, 124519Sgblack@eecs.umich.edu// teaching, education and corporate research & development. 134519Sgblack@eecs.umich.edu// Illustrative examples of commercial use are distributing products for 144519Sgblack@eecs.umich.edu// commercial advantage and providing services using the software for 154519Sgblack@eecs.umich.edu// commercial advantage. 164519Sgblack@eecs.umich.edu// 174519Sgblack@eecs.umich.edu// If you wish to use this software or functionality therein that may be 184519Sgblack@eecs.umich.edu// covered by patents for commercial use, please contact: 194519Sgblack@eecs.umich.edu// Director of Intellectual Property Licensing 204519Sgblack@eecs.umich.edu// Office of Strategy and Technology 214519Sgblack@eecs.umich.edu// Hewlett-Packard Company 224519Sgblack@eecs.umich.edu// 1501 Page Mill Road 234519Sgblack@eecs.umich.edu// Palo Alto, California 94304 244519Sgblack@eecs.umich.edu// 254519Sgblack@eecs.umich.edu// Redistributions of source code must retain the above copyright notice, 264519Sgblack@eecs.umich.edu// this list of conditions and the following disclaimer. Redistributions 274519Sgblack@eecs.umich.edu// in binary form must reproduce the above copyright notice, this list of 284519Sgblack@eecs.umich.edu// conditions and the following disclaimer in the documentation and/or 294519Sgblack@eecs.umich.edu// other materials provided with the distribution. Neither the name of 304519Sgblack@eecs.umich.edu// the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its 314519Sgblack@eecs.umich.edu// contributors may be used to endorse or promote products derived from 324519Sgblack@eecs.umich.edu// this software without specific prior written permission. No right of 334519Sgblack@eecs.umich.edu// sublicense is granted herewith. Derivatives of the software and 344519Sgblack@eecs.umich.edu// output created using the software may be prepared, but only for 354519Sgblack@eecs.umich.edu// Non-Commercial Uses. Derivatives of the software may be shared with 364519Sgblack@eecs.umich.edu// others provided: (i) the others agree to abide by the list of 374519Sgblack@eecs.umich.edu// conditions herein which includes the Non-Commercial Use restrictions; 384519Sgblack@eecs.umich.edu// and (ii) such Derivatives of the software include the above copyright 394519Sgblack@eecs.umich.edu// notice to acknowledge the contribution from this software where 404519Sgblack@eecs.umich.edu// applicable, this list of conditions and the disclaimer below. 414519Sgblack@eecs.umich.edu// 424519Sgblack@eecs.umich.edu// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 434519Sgblack@eecs.umich.edu// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 444519Sgblack@eecs.umich.edu// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 454519Sgblack@eecs.umich.edu// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 464519Sgblack@eecs.umich.edu// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 474519Sgblack@eecs.umich.edu// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 484519Sgblack@eecs.umich.edu// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 494519Sgblack@eecs.umich.edu// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 504519Sgblack@eecs.umich.edu// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 514519Sgblack@eecs.umich.edu// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 524519Sgblack@eecs.umich.edu// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 534519Sgblack@eecs.umich.edu// 544519Sgblack@eecs.umich.edu// Authors: Gabe Black 554519Sgblack@eecs.umich.edu 564519Sgblack@eecs.umich.edu////////////////////////////////////////////////////////////////////////// 574519Sgblack@eecs.umich.edu// 584519Sgblack@eecs.umich.edu// RegOp Microop templates 594519Sgblack@eecs.umich.edu// 604519Sgblack@eecs.umich.edu////////////////////////////////////////////////////////////////////////// 614519Sgblack@eecs.umich.edu 624519Sgblack@eecs.umich.edudef template MicroRegOpExecute {{ 634519Sgblack@eecs.umich.edu Fault %(class_name)s::execute(%(CPU_exec_context)s *xc, 644519Sgblack@eecs.umich.edu Trace::InstRecord *traceData) const 654519Sgblack@eecs.umich.edu { 664519Sgblack@eecs.umich.edu Fault fault = NoFault; 674519Sgblack@eecs.umich.edu 684809Sgblack@eecs.umich.edu DPRINTF(X86, "The data size is %d\n", dataSize); 694519Sgblack@eecs.umich.edu %(op_decl)s; 704519Sgblack@eecs.umich.edu %(op_rd)s; 714688Sgblack@eecs.umich.edu 724688Sgblack@eecs.umich.edu if(%(cond_check)s) 734688Sgblack@eecs.umich.edu { 744688Sgblack@eecs.umich.edu %(code)s; 754688Sgblack@eecs.umich.edu %(flag_code)s; 764688Sgblack@eecs.umich.edu } 774708Sgblack@eecs.umich.edu else 784708Sgblack@eecs.umich.edu { 794708Sgblack@eecs.umich.edu %(else_code)s; 804708Sgblack@eecs.umich.edu } 814519Sgblack@eecs.umich.edu 824519Sgblack@eecs.umich.edu //Write the resulting state to the execution context 834519Sgblack@eecs.umich.edu if(fault == NoFault) 844519Sgblack@eecs.umich.edu { 854519Sgblack@eecs.umich.edu %(op_wb)s; 864519Sgblack@eecs.umich.edu } 874519Sgblack@eecs.umich.edu return fault; 884519Sgblack@eecs.umich.edu } 894519Sgblack@eecs.umich.edu}}; 904519Sgblack@eecs.umich.edu 914519Sgblack@eecs.umich.edudef template MicroRegOpImmExecute {{ 924951Sgblack@eecs.umich.edu Fault %(class_name)s::execute(%(CPU_exec_context)s *xc, 934519Sgblack@eecs.umich.edu Trace::InstRecord *traceData) const 944519Sgblack@eecs.umich.edu { 954519Sgblack@eecs.umich.edu Fault fault = NoFault; 964519Sgblack@eecs.umich.edu 974519Sgblack@eecs.umich.edu %(op_decl)s; 984519Sgblack@eecs.umich.edu %(op_rd)s; 994688Sgblack@eecs.umich.edu 1004688Sgblack@eecs.umich.edu if(%(cond_check)s) 1014688Sgblack@eecs.umich.edu { 1024688Sgblack@eecs.umich.edu %(code)s; 1034688Sgblack@eecs.umich.edu %(flag_code)s; 1044688Sgblack@eecs.umich.edu } 1054708Sgblack@eecs.umich.edu else 1064708Sgblack@eecs.umich.edu { 1074708Sgblack@eecs.umich.edu %(else_code)s; 1084708Sgblack@eecs.umich.edu } 1094519Sgblack@eecs.umich.edu 1104519Sgblack@eecs.umich.edu //Write the resulting state to the execution context 1114519Sgblack@eecs.umich.edu if(fault == NoFault) 1124519Sgblack@eecs.umich.edu { 1134519Sgblack@eecs.umich.edu %(op_wb)s; 1144519Sgblack@eecs.umich.edu } 1154519Sgblack@eecs.umich.edu return fault; 1164519Sgblack@eecs.umich.edu } 1174519Sgblack@eecs.umich.edu}}; 1184519Sgblack@eecs.umich.edu 1194519Sgblack@eecs.umich.edudef template MicroRegOpDeclare {{ 1204519Sgblack@eecs.umich.edu class %(class_name)s : public %(base_class)s 1214519Sgblack@eecs.umich.edu { 1224519Sgblack@eecs.umich.edu protected: 1234519Sgblack@eecs.umich.edu void buildMe(); 1244519Sgblack@eecs.umich.edu 1254519Sgblack@eecs.umich.edu public: 1264519Sgblack@eecs.umich.edu %(class_name)s(ExtMachInst _machInst, 1274519Sgblack@eecs.umich.edu const char * instMnem, 1284519Sgblack@eecs.umich.edu bool isMicro, bool isDelayed, bool isFirst, bool isLast, 1294519Sgblack@eecs.umich.edu RegIndex _src1, RegIndex _src2, RegIndex _dest, 1304712Sgblack@eecs.umich.edu uint8_t _dataSize, uint16_t _ext); 1314519Sgblack@eecs.umich.edu 1324519Sgblack@eecs.umich.edu %(class_name)s(ExtMachInst _machInst, 1334519Sgblack@eecs.umich.edu const char * instMnem, 1344519Sgblack@eecs.umich.edu RegIndex _src1, RegIndex _src2, RegIndex _dest, 1354712Sgblack@eecs.umich.edu uint8_t _dataSize, uint16_t _ext); 1364519Sgblack@eecs.umich.edu 1374519Sgblack@eecs.umich.edu %(BasicExecDeclare)s 1384519Sgblack@eecs.umich.edu }; 1394519Sgblack@eecs.umich.edu}}; 1404519Sgblack@eecs.umich.edu 1414519Sgblack@eecs.umich.edudef template MicroRegOpImmDeclare {{ 1424519Sgblack@eecs.umich.edu 1434951Sgblack@eecs.umich.edu class %(class_name)s : public %(base_class)s 1444519Sgblack@eecs.umich.edu { 1454519Sgblack@eecs.umich.edu protected: 1464519Sgblack@eecs.umich.edu void buildMe(); 1474519Sgblack@eecs.umich.edu 1484519Sgblack@eecs.umich.edu public: 1494951Sgblack@eecs.umich.edu %(class_name)s(ExtMachInst _machInst, 1504519Sgblack@eecs.umich.edu const char * instMnem, 1514519Sgblack@eecs.umich.edu bool isMicro, bool isDelayed, bool isFirst, bool isLast, 1524951Sgblack@eecs.umich.edu RegIndex _src1, uint16_t _imm8, RegIndex _dest, 1534712Sgblack@eecs.umich.edu uint8_t _dataSize, uint16_t _ext); 1544519Sgblack@eecs.umich.edu 1554951Sgblack@eecs.umich.edu %(class_name)s(ExtMachInst _machInst, 1564519Sgblack@eecs.umich.edu const char * instMnem, 1574951Sgblack@eecs.umich.edu RegIndex _src1, uint16_t _imm8, RegIndex _dest, 1584712Sgblack@eecs.umich.edu uint8_t _dataSize, uint16_t _ext); 1594519Sgblack@eecs.umich.edu 1604519Sgblack@eecs.umich.edu %(BasicExecDeclare)s 1614519Sgblack@eecs.umich.edu }; 1624519Sgblack@eecs.umich.edu}}; 1634519Sgblack@eecs.umich.edu 1644519Sgblack@eecs.umich.edudef template MicroRegOpConstructor {{ 1654519Sgblack@eecs.umich.edu 1664519Sgblack@eecs.umich.edu inline void %(class_name)s::buildMe() 1674519Sgblack@eecs.umich.edu { 1684519Sgblack@eecs.umich.edu %(constructor)s; 1694519Sgblack@eecs.umich.edu } 1704519Sgblack@eecs.umich.edu 1714519Sgblack@eecs.umich.edu inline %(class_name)s::%(class_name)s( 1724519Sgblack@eecs.umich.edu ExtMachInst machInst, const char * instMnem, 1734519Sgblack@eecs.umich.edu RegIndex _src1, RegIndex _src2, RegIndex _dest, 1744712Sgblack@eecs.umich.edu uint8_t _dataSize, uint16_t _ext) : 1754519Sgblack@eecs.umich.edu %(base_class)s(machInst, "%(mnemonic)s", instMnem, 1764581Sgblack@eecs.umich.edu false, false, false, false, 1774688Sgblack@eecs.umich.edu _src1, _src2, _dest, _dataSize, _ext, 1784581Sgblack@eecs.umich.edu %(op_class)s) 1794519Sgblack@eecs.umich.edu { 1804519Sgblack@eecs.umich.edu buildMe(); 1814519Sgblack@eecs.umich.edu } 1824519Sgblack@eecs.umich.edu 1834519Sgblack@eecs.umich.edu inline %(class_name)s::%(class_name)s( 1844519Sgblack@eecs.umich.edu ExtMachInst machInst, const char * instMnem, 1854519Sgblack@eecs.umich.edu bool isMicro, bool isDelayed, bool isFirst, bool isLast, 1864519Sgblack@eecs.umich.edu RegIndex _src1, RegIndex _src2, RegIndex _dest, 1874712Sgblack@eecs.umich.edu uint8_t _dataSize, uint16_t _ext) : 1884519Sgblack@eecs.umich.edu %(base_class)s(machInst, "%(mnemonic)s", instMnem, 1894581Sgblack@eecs.umich.edu isMicro, isDelayed, isFirst, isLast, 1904688Sgblack@eecs.umich.edu _src1, _src2, _dest, _dataSize, _ext, 1914581Sgblack@eecs.umich.edu %(op_class)s) 1924519Sgblack@eecs.umich.edu { 1934519Sgblack@eecs.umich.edu buildMe(); 1944519Sgblack@eecs.umich.edu } 1954519Sgblack@eecs.umich.edu}}; 1964519Sgblack@eecs.umich.edu 1974519Sgblack@eecs.umich.edudef template MicroRegOpImmConstructor {{ 1984519Sgblack@eecs.umich.edu 1994951Sgblack@eecs.umich.edu inline void %(class_name)s::buildMe() 2004519Sgblack@eecs.umich.edu { 2014519Sgblack@eecs.umich.edu %(constructor)s; 2024519Sgblack@eecs.umich.edu } 2034519Sgblack@eecs.umich.edu 2044951Sgblack@eecs.umich.edu inline %(class_name)s::%(class_name)s( 2054519Sgblack@eecs.umich.edu ExtMachInst machInst, const char * instMnem, 2064951Sgblack@eecs.umich.edu RegIndex _src1, uint16_t _imm8, RegIndex _dest, 2074712Sgblack@eecs.umich.edu uint8_t _dataSize, uint16_t _ext) : 2084519Sgblack@eecs.umich.edu %(base_class)s(machInst, "%(mnemonic)s", instMnem, 2094581Sgblack@eecs.umich.edu false, false, false, false, 2104688Sgblack@eecs.umich.edu _src1, _imm8, _dest, _dataSize, _ext, 2114581Sgblack@eecs.umich.edu %(op_class)s) 2124519Sgblack@eecs.umich.edu { 2134519Sgblack@eecs.umich.edu buildMe(); 2144519Sgblack@eecs.umich.edu } 2154519Sgblack@eecs.umich.edu 2164951Sgblack@eecs.umich.edu inline %(class_name)s::%(class_name)s( 2174519Sgblack@eecs.umich.edu ExtMachInst machInst, const char * instMnem, 2184519Sgblack@eecs.umich.edu bool isMicro, bool isDelayed, bool isFirst, bool isLast, 2194951Sgblack@eecs.umich.edu RegIndex _src1, uint16_t _imm8, RegIndex _dest, 2204712Sgblack@eecs.umich.edu uint8_t _dataSize, uint16_t _ext) : 2214519Sgblack@eecs.umich.edu %(base_class)s(machInst, "%(mnemonic)s", instMnem, 2224581Sgblack@eecs.umich.edu isMicro, isDelayed, isFirst, isLast, 2234688Sgblack@eecs.umich.edu _src1, _imm8, _dest, _dataSize, _ext, 2244581Sgblack@eecs.umich.edu %(op_class)s) 2254519Sgblack@eecs.umich.edu { 2264519Sgblack@eecs.umich.edu buildMe(); 2274519Sgblack@eecs.umich.edu } 2284519Sgblack@eecs.umich.edu}}; 2294519Sgblack@eecs.umich.edu 2305075Sgblack@eecs.umich.eduoutput header {{ 2315075Sgblack@eecs.umich.edu void 2325075Sgblack@eecs.umich.edu divide(uint64_t dividend, uint64_t divisor, 2335075Sgblack@eecs.umich.edu uint64_t "ient, uint64_t &remainder); 2345075Sgblack@eecs.umich.edu}}; 2355075Sgblack@eecs.umich.edu 2365075Sgblack@eecs.umich.eduoutput decoder {{ 2375075Sgblack@eecs.umich.edu void 2385075Sgblack@eecs.umich.edu divide(uint64_t dividend, uint64_t divisor, 2395075Sgblack@eecs.umich.edu uint64_t "ient, uint64_t &remainder) 2405075Sgblack@eecs.umich.edu { 2415075Sgblack@eecs.umich.edu //Check for divide by zero. 2425075Sgblack@eecs.umich.edu if (divisor == 0) 2435075Sgblack@eecs.umich.edu panic("Divide by zero!\\n"); 2445075Sgblack@eecs.umich.edu //If the divisor is bigger than the dividend, don't do anything. 2455075Sgblack@eecs.umich.edu if (divisor <= dividend) { 2465075Sgblack@eecs.umich.edu //Shift the divisor so it's msb lines up with the dividend. 2475075Sgblack@eecs.umich.edu int dividendMsb = findMsbSet(dividend); 2485075Sgblack@eecs.umich.edu int divisorMsb = findMsbSet(divisor); 2495075Sgblack@eecs.umich.edu int shift = dividendMsb - divisorMsb; 2505075Sgblack@eecs.umich.edu divisor <<= shift; 2515075Sgblack@eecs.umich.edu //Compute what we'll add to the quotient if the divisor isn't 2525075Sgblack@eecs.umich.edu //now larger than the dividend. 2535075Sgblack@eecs.umich.edu uint64_t quotientBit = 1; 2545075Sgblack@eecs.umich.edu quotientBit <<= shift; 2555075Sgblack@eecs.umich.edu //If we need to step back a bit (no pun intended) because the 2565075Sgblack@eecs.umich.edu //divisor got too to large, do that here. This is the "or two" 2575075Sgblack@eecs.umich.edu //part of one or two bit division. 2585075Sgblack@eecs.umich.edu if (divisor > dividend) { 2595075Sgblack@eecs.umich.edu quotientBit >>= 1; 2605075Sgblack@eecs.umich.edu divisor >>= 1; 2615075Sgblack@eecs.umich.edu } 2625075Sgblack@eecs.umich.edu //Decrement the remainder and increment the quotient. 2635075Sgblack@eecs.umich.edu quotient += quotientBit; 2645075Sgblack@eecs.umich.edu remainder -= divisor; 2655075Sgblack@eecs.umich.edu } 2665075Sgblack@eecs.umich.edu } 2675075Sgblack@eecs.umich.edu}}; 2685075Sgblack@eecs.umich.edu 2694519Sgblack@eecs.umich.edulet {{ 2705040Sgblack@eecs.umich.edu # Make these empty strings so that concatenating onto 2715040Sgblack@eecs.umich.edu # them will always work. 2725040Sgblack@eecs.umich.edu header_output = "" 2735040Sgblack@eecs.umich.edu decoder_output = "" 2745040Sgblack@eecs.umich.edu exec_output = "" 2755040Sgblack@eecs.umich.edu 2765040Sgblack@eecs.umich.edu immTemplates = ( 2775040Sgblack@eecs.umich.edu MicroRegOpImmDeclare, 2785040Sgblack@eecs.umich.edu MicroRegOpImmConstructor, 2795040Sgblack@eecs.umich.edu MicroRegOpImmExecute) 2805040Sgblack@eecs.umich.edu 2815040Sgblack@eecs.umich.edu regTemplates = ( 2825040Sgblack@eecs.umich.edu MicroRegOpDeclare, 2835040Sgblack@eecs.umich.edu MicroRegOpConstructor, 2845040Sgblack@eecs.umich.edu MicroRegOpExecute) 2855040Sgblack@eecs.umich.edu 2865040Sgblack@eecs.umich.edu class RegOpMeta(type): 2875040Sgblack@eecs.umich.edu def buildCppClasses(self, name, Name, suffix, \ 2885040Sgblack@eecs.umich.edu code, flag_code, cond_check, else_code): 2895040Sgblack@eecs.umich.edu 2905040Sgblack@eecs.umich.edu # Globals to stick the output in 2915040Sgblack@eecs.umich.edu global header_output 2925040Sgblack@eecs.umich.edu global decoder_output 2935040Sgblack@eecs.umich.edu global exec_output 2945040Sgblack@eecs.umich.edu 2955040Sgblack@eecs.umich.edu # Stick all the code together so it can be searched at once 2965040Sgblack@eecs.umich.edu allCode = "|".join((code, flag_code, cond_check, else_code)) 2975040Sgblack@eecs.umich.edu 2985040Sgblack@eecs.umich.edu # If op2 is used anywhere, make register and immediate versions 2995040Sgblack@eecs.umich.edu # of this code. 3005062Sgblack@eecs.umich.edu matcher = re.compile("(?<!\\w)(?P<prefix>s?)op2(?P<typeQual>\\.\\w+)?") 3015062Sgblack@eecs.umich.edu match = matcher.search(allCode) 3025062Sgblack@eecs.umich.edu if match: 3035062Sgblack@eecs.umich.edu typeQual = "" 3045062Sgblack@eecs.umich.edu if match.group("typeQual"): 3055062Sgblack@eecs.umich.edu typeQual = match.group("typeQual") 3065062Sgblack@eecs.umich.edu src2_name = "%spsrc2%s" % (match.group("prefix"), typeQual) 3075040Sgblack@eecs.umich.edu self.buildCppClasses(name, Name, suffix, 3085062Sgblack@eecs.umich.edu matcher.sub(src2_name, code), 3095062Sgblack@eecs.umich.edu matcher.sub(src2_name, flag_code), 3105062Sgblack@eecs.umich.edu matcher.sub(src2_name, cond_check), 3115062Sgblack@eecs.umich.edu matcher.sub(src2_name, else_code)) 3125040Sgblack@eecs.umich.edu self.buildCppClasses(name + "i", Name, suffix + "Imm", 3135040Sgblack@eecs.umich.edu matcher.sub("imm8", code), 3145040Sgblack@eecs.umich.edu matcher.sub("imm8", flag_code), 3155040Sgblack@eecs.umich.edu matcher.sub("imm8", cond_check), 3165040Sgblack@eecs.umich.edu matcher.sub("imm8", else_code)) 3175040Sgblack@eecs.umich.edu return 3185040Sgblack@eecs.umich.edu 3195040Sgblack@eecs.umich.edu # If there's something optional to do with flags, generate 3205040Sgblack@eecs.umich.edu # a version without it and fix up this version to use it. 3215239Sgblack@eecs.umich.edu if flag_code != "" or cond_check != "true": 3225040Sgblack@eecs.umich.edu self.buildCppClasses(name, Name, suffix, 3235040Sgblack@eecs.umich.edu code, "", "true", else_code) 3245040Sgblack@eecs.umich.edu suffix = "Flags" + suffix 3255040Sgblack@eecs.umich.edu 3265040Sgblack@eecs.umich.edu # If psrc1 or psrc2 is used, we need to actually insert code to 3275040Sgblack@eecs.umich.edu # compute it. 3285040Sgblack@eecs.umich.edu matcher = re.compile("(?<!\w)psrc1(?!\w)") 3295040Sgblack@eecs.umich.edu if matcher.search(allCode): 3305061Sgblack@eecs.umich.edu code = "uint64_t psrc1 = pick(SrcReg1, 0, dataSize);" + code 3315040Sgblack@eecs.umich.edu matcher = re.compile("(?<!\w)psrc2(?!\w)") 3325040Sgblack@eecs.umich.edu if matcher.search(allCode): 3335061Sgblack@eecs.umich.edu code = "uint64_t psrc2 = pick(SrcReg2, 1, dataSize);" + code 3345061Sgblack@eecs.umich.edu # Also make available versions which do sign extension 3355061Sgblack@eecs.umich.edu matcher = re.compile("(?<!\w)spsrc1(?!\w)") 3365061Sgblack@eecs.umich.edu if matcher.search(allCode): 3375061Sgblack@eecs.umich.edu code = "int64_t spsrc1 = signedPick(SrcReg1, 0, dataSize);" + code 3385061Sgblack@eecs.umich.edu matcher = re.compile("(?<!\w)spsrc2(?!\w)") 3395061Sgblack@eecs.umich.edu if matcher.search(allCode): 3405061Sgblack@eecs.umich.edu code = "int64_t spsrc2 = signedPick(SrcReg2, 1, dataSize);" + code 3415040Sgblack@eecs.umich.edu 3425040Sgblack@eecs.umich.edu base = "X86ISA::RegOp" 3435040Sgblack@eecs.umich.edu 3445040Sgblack@eecs.umich.edu # If imm8 shows up in the code, use the immediate templates, if 3455040Sgblack@eecs.umich.edu # not, hopefully the register ones will be correct. 3465040Sgblack@eecs.umich.edu templates = regTemplates 3475040Sgblack@eecs.umich.edu matcher = re.compile("(?<!\w)imm8(?!\w)") 3485040Sgblack@eecs.umich.edu if matcher.search(allCode): 3495040Sgblack@eecs.umich.edu base += "Imm" 3505040Sgblack@eecs.umich.edu templates = immTemplates 3515040Sgblack@eecs.umich.edu 3525040Sgblack@eecs.umich.edu # Get everything ready for the substitution 3535040Sgblack@eecs.umich.edu iop = InstObjParams(name, Name + suffix, base, 3545040Sgblack@eecs.umich.edu {"code" : code, 3555040Sgblack@eecs.umich.edu "flag_code" : flag_code, 3565040Sgblack@eecs.umich.edu "cond_check" : cond_check, 3575040Sgblack@eecs.umich.edu "else_code" : else_code}) 3585040Sgblack@eecs.umich.edu 3595040Sgblack@eecs.umich.edu # Generate the actual code (finally!) 3605040Sgblack@eecs.umich.edu header_output += templates[0].subst(iop) 3615040Sgblack@eecs.umich.edu decoder_output += templates[1].subst(iop) 3625040Sgblack@eecs.umich.edu exec_output += templates[2].subst(iop) 3635040Sgblack@eecs.umich.edu 3645040Sgblack@eecs.umich.edu 3655040Sgblack@eecs.umich.edu def __new__(mcls, Name, bases, dict): 3664688Sgblack@eecs.umich.edu abstract = False 3675040Sgblack@eecs.umich.edu name = Name.lower() 3684688Sgblack@eecs.umich.edu if "abstract" in dict: 3694688Sgblack@eecs.umich.edu abstract = dict['abstract'] 3704688Sgblack@eecs.umich.edu del dict['abstract'] 3714688Sgblack@eecs.umich.edu 3725040Sgblack@eecs.umich.edu cls = super(RegOpMeta, mcls).__new__(mcls, Name, bases, dict) 3734688Sgblack@eecs.umich.edu if not abstract: 3745040Sgblack@eecs.umich.edu cls.className = Name 3755040Sgblack@eecs.umich.edu cls.base_mnemonic = name 3765040Sgblack@eecs.umich.edu code = cls.code 3775040Sgblack@eecs.umich.edu flag_code = cls.flag_code 3785040Sgblack@eecs.umich.edu cond_check = cls.cond_check 3795040Sgblack@eecs.umich.edu else_code = cls.else_code 3805040Sgblack@eecs.umich.edu 3815040Sgblack@eecs.umich.edu # Set up the C++ classes 3825040Sgblack@eecs.umich.edu mcls.buildCppClasses(cls, name, Name, "", 3835040Sgblack@eecs.umich.edu code, flag_code, cond_check, else_code) 3845040Sgblack@eecs.umich.edu 3855040Sgblack@eecs.umich.edu # Hook into the microassembler dict 3865040Sgblack@eecs.umich.edu global microopClasses 3875040Sgblack@eecs.umich.edu microopClasses[name] = cls 3885040Sgblack@eecs.umich.edu 3895040Sgblack@eecs.umich.edu allCode = "|".join((code, flag_code, cond_check, else_code)) 3905040Sgblack@eecs.umich.edu 3915040Sgblack@eecs.umich.edu # If op2 is used anywhere, make register and immediate versions 3925040Sgblack@eecs.umich.edu # of this code. 3935040Sgblack@eecs.umich.edu matcher = re.compile("op2(?P<typeQual>\\.\\w+)?") 3945040Sgblack@eecs.umich.edu if matcher.search(allCode): 3955040Sgblack@eecs.umich.edu microopClasses[name + 'i'] = cls 3964688Sgblack@eecs.umich.edu return cls 3974688Sgblack@eecs.umich.edu 3985040Sgblack@eecs.umich.edu 3995040Sgblack@eecs.umich.edu class RegOp(X86Microop): 4005040Sgblack@eecs.umich.edu __metaclass__ = RegOpMeta 4015040Sgblack@eecs.umich.edu # This class itself doesn't act as a microop 4024688Sgblack@eecs.umich.edu abstract = True 4034688Sgblack@eecs.umich.edu 4045040Sgblack@eecs.umich.edu # Default template parameter values 4055040Sgblack@eecs.umich.edu flag_code = "" 4065040Sgblack@eecs.umich.edu cond_check = "true" 4075040Sgblack@eecs.umich.edu else_code = ";" 4085040Sgblack@eecs.umich.edu 4095040Sgblack@eecs.umich.edu def __init__(self, dest, src1, op2, flags = None, dataSize = "env.dataSize"): 4104519Sgblack@eecs.umich.edu self.dest = dest 4114519Sgblack@eecs.umich.edu self.src1 = src1 4125040Sgblack@eecs.umich.edu self.op2 = op2 4134688Sgblack@eecs.umich.edu self.flags = flags 4144701Sgblack@eecs.umich.edu self.dataSize = dataSize 4154688Sgblack@eecs.umich.edu if flags is None: 4164688Sgblack@eecs.umich.edu self.ext = 0 4174688Sgblack@eecs.umich.edu else: 4184688Sgblack@eecs.umich.edu if not isinstance(flags, (list, tuple)): 4194688Sgblack@eecs.umich.edu raise Exception, "flags must be a list or tuple of flags" 4204688Sgblack@eecs.umich.edu self.ext = " | ".join(flags) 4214688Sgblack@eecs.umich.edu self.className += "Flags" 4224519Sgblack@eecs.umich.edu 4234519Sgblack@eecs.umich.edu def getAllocator(self, *microFlags): 4245040Sgblack@eecs.umich.edu className = self.className 4255040Sgblack@eecs.umich.edu if self.mnemonic == self.base_mnemonic + 'i': 4265040Sgblack@eecs.umich.edu className += "Imm" 4274560Sgblack@eecs.umich.edu allocator = '''new %(class_name)s(machInst, mnemonic 4285040Sgblack@eecs.umich.edu %(flags)s, %(src1)s, %(op2)s, %(dest)s, 4294688Sgblack@eecs.umich.edu %(dataSize)s, %(ext)s)''' % { 4305040Sgblack@eecs.umich.edu "class_name" : className, 4314519Sgblack@eecs.umich.edu "flags" : self.microFlagsText(microFlags), 4325040Sgblack@eecs.umich.edu "src1" : self.src1, "op2" : self.op2, 4334519Sgblack@eecs.umich.edu "dest" : self.dest, 4344519Sgblack@eecs.umich.edu "dataSize" : self.dataSize, 4354519Sgblack@eecs.umich.edu "ext" : self.ext} 4364539Sgblack@eecs.umich.edu return allocator 4374519Sgblack@eecs.umich.edu 4385040Sgblack@eecs.umich.edu class LogicRegOp(RegOp): 4394688Sgblack@eecs.umich.edu abstract = True 4405040Sgblack@eecs.umich.edu flag_code = ''' 4415040Sgblack@eecs.umich.edu //Don't have genFlags handle the OF or CF bits 4425115Sgblack@eecs.umich.edu uint64_t mask = CFBit | ECFBit | OFBit; 4435040Sgblack@eecs.umich.edu ccFlagBits = genFlags(ccFlagBits, ext & ~mask, DestReg, psrc1, op2); 4445040Sgblack@eecs.umich.edu //If a logic microop wants to set these, it wants to set them to 0. 4455040Sgblack@eecs.umich.edu ccFlagBits &= ~(CFBit & ext); 4465115Sgblack@eecs.umich.edu ccFlagBits &= ~(ECFBit & ext); 4475040Sgblack@eecs.umich.edu ccFlagBits &= ~(OFBit & ext); 4485040Sgblack@eecs.umich.edu ''' 4494519Sgblack@eecs.umich.edu 4505040Sgblack@eecs.umich.edu class FlagRegOp(RegOp): 4515040Sgblack@eecs.umich.edu abstract = True 4525040Sgblack@eecs.umich.edu flag_code = \ 4535040Sgblack@eecs.umich.edu "ccFlagBits = genFlags(ccFlagBits, ext, DestReg, psrc1, op2);" 4544519Sgblack@eecs.umich.edu 4555040Sgblack@eecs.umich.edu class SubRegOp(RegOp): 4565040Sgblack@eecs.umich.edu abstract = True 4575040Sgblack@eecs.umich.edu flag_code = \ 4585040Sgblack@eecs.umich.edu "ccFlagBits = genFlags(ccFlagBits, ext, DestReg, psrc1, ~op2, true);" 4594519Sgblack@eecs.umich.edu 4605040Sgblack@eecs.umich.edu class CondRegOp(RegOp): 4615040Sgblack@eecs.umich.edu abstract = True 4625083Sgblack@eecs.umich.edu cond_check = "checkCondition(ccFlagBits, ext)" 4634519Sgblack@eecs.umich.edu 4645063Sgblack@eecs.umich.edu class RdRegOp(RegOp): 4655063Sgblack@eecs.umich.edu abstract = True 4665063Sgblack@eecs.umich.edu def __init__(self, dest, src1=None, dataSize="env.dataSize"): 4675063Sgblack@eecs.umich.edu if not src1: 4685063Sgblack@eecs.umich.edu src1 = dest 4695063Sgblack@eecs.umich.edu super(RdRegOp, self).__init__(dest, src1, "NUM_INTREGS", None, dataSize) 4705063Sgblack@eecs.umich.edu 4715063Sgblack@eecs.umich.edu class WrRegOp(RegOp): 4725063Sgblack@eecs.umich.edu abstract = True 4735063Sgblack@eecs.umich.edu def __init__(self, src1, src2, flags=None, dataSize="env.dataSize"): 4745063Sgblack@eecs.umich.edu super(WrRegOp, self).__init__("NUM_INTREGS", src1, src2, flags, dataSize) 4755063Sgblack@eecs.umich.edu 4765040Sgblack@eecs.umich.edu class Add(FlagRegOp): 4775040Sgblack@eecs.umich.edu code = 'DestReg = merge(DestReg, psrc1 + op2, dataSize);' 4784595Sgblack@eecs.umich.edu 4795040Sgblack@eecs.umich.edu class Or(LogicRegOp): 4805040Sgblack@eecs.umich.edu code = 'DestReg = merge(DestReg, psrc1 | op2, dataSize);' 4814595Sgblack@eecs.umich.edu 4825040Sgblack@eecs.umich.edu class Adc(FlagRegOp): 4835040Sgblack@eecs.umich.edu code = ''' 4844732Sgblack@eecs.umich.edu CCFlagBits flags = ccFlagBits; 4855138Sgblack@eecs.umich.edu DestReg = merge(DestReg, psrc1 + op2 + flags.cf, dataSize); 4865040Sgblack@eecs.umich.edu ''' 4875040Sgblack@eecs.umich.edu 4885040Sgblack@eecs.umich.edu class Sbb(SubRegOp): 4895040Sgblack@eecs.umich.edu code = ''' 4904732Sgblack@eecs.umich.edu CCFlagBits flags = ccFlagBits; 4915138Sgblack@eecs.umich.edu DestReg = merge(DestReg, psrc1 - op2 - flags.cf, dataSize); 4925040Sgblack@eecs.umich.edu ''' 4935040Sgblack@eecs.umich.edu 4945040Sgblack@eecs.umich.edu class And(LogicRegOp): 4955040Sgblack@eecs.umich.edu code = 'DestReg = merge(DestReg, psrc1 & op2, dataSize)' 4965040Sgblack@eecs.umich.edu 4975040Sgblack@eecs.umich.edu class Sub(SubRegOp): 4985040Sgblack@eecs.umich.edu code = 'DestReg = merge(DestReg, psrc1 - op2, dataSize)' 4995040Sgblack@eecs.umich.edu 5005040Sgblack@eecs.umich.edu class Xor(LogicRegOp): 5015040Sgblack@eecs.umich.edu code = 'DestReg = merge(DestReg, psrc1 ^ op2, dataSize)' 5025040Sgblack@eecs.umich.edu 5035065Sgblack@eecs.umich.edu # Neither of these is quite correct because it assumes that right shifting 5045065Sgblack@eecs.umich.edu # a signed or unsigned value does sign or zero extension respectively. 5055065Sgblack@eecs.umich.edu # The C standard says that what happens on a right shift with a 1 in the 5065065Sgblack@eecs.umich.edu # MSB position is undefined. On x86 and under likely most compilers the 5075065Sgblack@eecs.umich.edu # "right thing" happens, but this isn't a guarantee. 5085063Sgblack@eecs.umich.edu class Mul1s(WrRegOp): 5095040Sgblack@eecs.umich.edu code = ''' 5105063Sgblack@eecs.umich.edu ProdLow = psrc1 * op2; 5115063Sgblack@eecs.umich.edu int halfSize = (dataSize * 8) / 2; 5125063Sgblack@eecs.umich.edu int64_t spsrc1_h = spsrc1 >> halfSize; 5135063Sgblack@eecs.umich.edu int64_t spsrc1_l = spsrc1 & mask(halfSize); 5145063Sgblack@eecs.umich.edu int64_t spsrc2_h = sop2 >> halfSize; 5155063Sgblack@eecs.umich.edu int64_t spsrc2_l = sop2 & mask(halfSize); 5165063Sgblack@eecs.umich.edu ProdHi = ((spsrc1_l * spsrc2_h + spsrc1_h * spsrc2_l + 5175063Sgblack@eecs.umich.edu ((spsrc1_l * spsrc2_l) >> halfSize)) >> halfSize) + 5185063Sgblack@eecs.umich.edu spsrc1_h * spsrc2_h; 5195040Sgblack@eecs.umich.edu ''' 5205040Sgblack@eecs.umich.edu 5215063Sgblack@eecs.umich.edu class Mul1u(WrRegOp): 5225040Sgblack@eecs.umich.edu code = ''' 5235063Sgblack@eecs.umich.edu ProdLow = psrc1 * op2; 5244809Sgblack@eecs.umich.edu int halfSize = (dataSize * 8) / 2; 5255063Sgblack@eecs.umich.edu uint64_t psrc1_h = psrc1 >> halfSize; 5265063Sgblack@eecs.umich.edu uint64_t psrc1_l = psrc1 & mask(halfSize); 5275063Sgblack@eecs.umich.edu uint64_t psrc2_h = op2 >> halfSize; 5285063Sgblack@eecs.umich.edu uint64_t psrc2_l = op2 & mask(halfSize); 5295063Sgblack@eecs.umich.edu ProdHi = ((psrc1_l * psrc2_h + psrc1_h * psrc2_l + 5305063Sgblack@eecs.umich.edu ((psrc1_l * psrc2_l) >> halfSize)) >> halfSize) + 5315063Sgblack@eecs.umich.edu psrc1_h * psrc2_h; 5325040Sgblack@eecs.umich.edu ''' 5335040Sgblack@eecs.umich.edu 5345063Sgblack@eecs.umich.edu class Mulel(RdRegOp): 5355063Sgblack@eecs.umich.edu code = 'DestReg = merge(SrcReg1, ProdLow, dataSize);' 5365040Sgblack@eecs.umich.edu 5375063Sgblack@eecs.umich.edu class Muleh(RdRegOp): 5385063Sgblack@eecs.umich.edu def __init__(self, dest, src1=None, flags=None, dataSize="env.dataSize"): 5395063Sgblack@eecs.umich.edu if not src1: 5405063Sgblack@eecs.umich.edu src1 = dest 5415063Sgblack@eecs.umich.edu super(RdRegOp, self).__init__(dest, src1, "NUM_INTREGS", flags, dataSize) 5425063Sgblack@eecs.umich.edu code = 'DestReg = merge(SrcReg1, ProdHi, dataSize);' 5435063Sgblack@eecs.umich.edu flag_code = ''' 5445063Sgblack@eecs.umich.edu if (ProdHi) 5455063Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | (ext & (CFBit | OFBit | ECFBit)); 5465063Sgblack@eecs.umich.edu else 5475063Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits & ~(ext & (CFBit | OFBit | ECFBit)); 5485063Sgblack@eecs.umich.edu ''' 5495062Sgblack@eecs.umich.edu 5505075Sgblack@eecs.umich.edu # One or two bit divide 5515075Sgblack@eecs.umich.edu class Div1(WrRegOp): 5525040Sgblack@eecs.umich.edu code = ''' 5535075Sgblack@eecs.umich.edu //These are temporaries so that modifying them later won't make 5545075Sgblack@eecs.umich.edu //the ISA parser think they're also sources. 5555075Sgblack@eecs.umich.edu uint64_t quotient = 0; 5565075Sgblack@eecs.umich.edu uint64_t remainder = psrc1; 5575075Sgblack@eecs.umich.edu //Similarly, this is a temporary so changing it doesn't make it 5585075Sgblack@eecs.umich.edu //a source. 5595075Sgblack@eecs.umich.edu uint64_t divisor = op2; 5605075Sgblack@eecs.umich.edu //This is a temporary just for consistency and clarity. 5615075Sgblack@eecs.umich.edu uint64_t dividend = remainder; 5625075Sgblack@eecs.umich.edu //Do the division. 5635075Sgblack@eecs.umich.edu divide(dividend, divisor, quotient, remainder); 5645075Sgblack@eecs.umich.edu //Record the final results. 5655075Sgblack@eecs.umich.edu Remainder = remainder; 5665075Sgblack@eecs.umich.edu Quotient = quotient; 5675075Sgblack@eecs.umich.edu Divisor = divisor; 5685040Sgblack@eecs.umich.edu ''' 5694823Sgblack@eecs.umich.edu 5705075Sgblack@eecs.umich.edu # Step divide 5715075Sgblack@eecs.umich.edu class Div2(RegOp): 5725075Sgblack@eecs.umich.edu code = ''' 5735075Sgblack@eecs.umich.edu uint64_t dividend = Remainder; 5745075Sgblack@eecs.umich.edu uint64_t divisor = Divisor; 5755075Sgblack@eecs.umich.edu uint64_t quotient = Quotient; 5765075Sgblack@eecs.umich.edu uint64_t remainder = dividend; 5775075Sgblack@eecs.umich.edu int remaining = op2; 5785075Sgblack@eecs.umich.edu //If we overshot, do nothing. This lets us unrool division loops a 5795075Sgblack@eecs.umich.edu //little. 5805075Sgblack@eecs.umich.edu if (remaining) { 5815075Sgblack@eecs.umich.edu //Shift in bits from the low order portion of the dividend 5825075Sgblack@eecs.umich.edu while(dividend < divisor && remaining) { 5835075Sgblack@eecs.umich.edu dividend = (dividend << 1) | bits(SrcReg1, remaining - 1); 5845075Sgblack@eecs.umich.edu quotient <<= 1; 5855075Sgblack@eecs.umich.edu remaining--; 5865075Sgblack@eecs.umich.edu } 5875075Sgblack@eecs.umich.edu remainder = dividend; 5885075Sgblack@eecs.umich.edu //Do the division. 5895075Sgblack@eecs.umich.edu divide(dividend, divisor, quotient, remainder); 5905075Sgblack@eecs.umich.edu } 5915075Sgblack@eecs.umich.edu //Keep track of how many bits there are still to pull in. 5925075Sgblack@eecs.umich.edu DestReg = merge(DestReg, remaining, dataSize); 5935075Sgblack@eecs.umich.edu //Record the final results 5945075Sgblack@eecs.umich.edu Remainder = remainder; 5955075Sgblack@eecs.umich.edu Quotient = quotient; 5965075Sgblack@eecs.umich.edu ''' 5975075Sgblack@eecs.umich.edu flag_code = ''' 5985075Sgblack@eecs.umich.edu if (DestReg == 0) 5995075Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | (ext & EZFBit); 6005075Sgblack@eecs.umich.edu else 6015075Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits & ~(ext & EZFBit); 6025075Sgblack@eecs.umich.edu ''' 6034732Sgblack@eecs.umich.edu 6045075Sgblack@eecs.umich.edu class Divq(RdRegOp): 6055075Sgblack@eecs.umich.edu code = 'DestReg = merge(SrcReg1, Quotient, dataSize);' 6065075Sgblack@eecs.umich.edu 6075075Sgblack@eecs.umich.edu class Divr(RdRegOp): 6085075Sgblack@eecs.umich.edu code = 'DestReg = merge(SrcReg1, Remainder, dataSize);' 6095040Sgblack@eecs.umich.edu 6105040Sgblack@eecs.umich.edu class Mov(CondRegOp): 6115040Sgblack@eecs.umich.edu code = 'DestReg = merge(SrcReg1, op2, dataSize)' 6125040Sgblack@eecs.umich.edu else_code = 'DestReg=DestReg;' 6135040Sgblack@eecs.umich.edu 6144732Sgblack@eecs.umich.edu # Shift instructions 6155040Sgblack@eecs.umich.edu 6165076Sgblack@eecs.umich.edu class Sll(RegOp): 6175040Sgblack@eecs.umich.edu code = ''' 6184756Sgblack@eecs.umich.edu uint8_t shiftAmt = (op2 & ((dataSize == 8) ? mask(6) : mask(5))); 6194823Sgblack@eecs.umich.edu DestReg = merge(DestReg, psrc1 << shiftAmt, dataSize); 6205040Sgblack@eecs.umich.edu ''' 6215076Sgblack@eecs.umich.edu flag_code = ''' 6225076Sgblack@eecs.umich.edu // If the shift amount is zero, no flags should be modified. 6235076Sgblack@eecs.umich.edu if (shiftAmt) { 6245076Sgblack@eecs.umich.edu //Zero out any flags we might modify. This way we only have to 6255076Sgblack@eecs.umich.edu //worry about setting them. 6265076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits & ~(ext & (CFBit | ECFBit | OFBit)); 6275076Sgblack@eecs.umich.edu int CFBits = 0; 6285076Sgblack@eecs.umich.edu //Figure out if we -would- set the CF bits if requested. 6295076Sgblack@eecs.umich.edu if (bits(SrcReg1, dataSize * 8 - shiftAmt)) 6305076Sgblack@eecs.umich.edu CFBits = 1; 6315076Sgblack@eecs.umich.edu //If some combination of the CF bits need to be set, set them. 6325076Sgblack@eecs.umich.edu if ((ext & (CFBit | ECFBit)) && CFBits) 6335076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | (ext & (CFBit | ECFBit)); 6345076Sgblack@eecs.umich.edu //Figure out what the OF bit should be. 6355076Sgblack@eecs.umich.edu if ((ext & OFBit) && (CFBits ^ bits(DestReg, dataSize * 8 - 1))) 6365076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | OFBit; 6375076Sgblack@eecs.umich.edu //Use the regular mechanisms to calculate the other flags. 6385076Sgblack@eecs.umich.edu ccFlagBits = genFlags(ccFlagBits, ext & ~(CFBit | ECFBit | OFBit), 6395076Sgblack@eecs.umich.edu DestReg, psrc1, op2); 6405076Sgblack@eecs.umich.edu } 6415076Sgblack@eecs.umich.edu ''' 6425040Sgblack@eecs.umich.edu 6435076Sgblack@eecs.umich.edu class Srl(RegOp): 6445040Sgblack@eecs.umich.edu code = ''' 6454756Sgblack@eecs.umich.edu uint8_t shiftAmt = (op2 & ((dataSize == 8) ? mask(6) : mask(5))); 6464732Sgblack@eecs.umich.edu // Because what happens to the bits shift -in- on a right shift 6474732Sgblack@eecs.umich.edu // is not defined in the C/C++ standard, we have to mask them out 6484732Sgblack@eecs.umich.edu // to be sure they're zero. 6494732Sgblack@eecs.umich.edu uint64_t logicalMask = mask(dataSize * 8 - shiftAmt); 6504823Sgblack@eecs.umich.edu DestReg = merge(DestReg, (psrc1 >> shiftAmt) & logicalMask, dataSize); 6515040Sgblack@eecs.umich.edu ''' 6525076Sgblack@eecs.umich.edu flag_code = ''' 6535076Sgblack@eecs.umich.edu // If the shift amount is zero, no flags should be modified. 6545076Sgblack@eecs.umich.edu if (shiftAmt) { 6555076Sgblack@eecs.umich.edu //Zero out any flags we might modify. This way we only have to 6565076Sgblack@eecs.umich.edu //worry about setting them. 6575076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits & ~(ext & (CFBit | ECFBit | OFBit)); 6585076Sgblack@eecs.umich.edu //If some combination of the CF bits need to be set, set them. 6595076Sgblack@eecs.umich.edu if ((ext & (CFBit | ECFBit)) && bits(SrcReg1, shiftAmt - 1)) 6605076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | (ext & (CFBit | ECFBit)); 6615076Sgblack@eecs.umich.edu //Figure out what the OF bit should be. 6625076Sgblack@eecs.umich.edu if ((ext & OFBit) && bits(SrcReg1, dataSize * 8 - 1)) 6635076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | OFBit; 6645076Sgblack@eecs.umich.edu //Use the regular mechanisms to calculate the other flags. 6655076Sgblack@eecs.umich.edu ccFlagBits = genFlags(ccFlagBits, ext & ~(CFBit | ECFBit | OFBit), 6665076Sgblack@eecs.umich.edu DestReg, psrc1, op2); 6675076Sgblack@eecs.umich.edu } 6685076Sgblack@eecs.umich.edu ''' 6695040Sgblack@eecs.umich.edu 6705076Sgblack@eecs.umich.edu class Sra(RegOp): 6715040Sgblack@eecs.umich.edu code = ''' 6724756Sgblack@eecs.umich.edu uint8_t shiftAmt = (op2 & ((dataSize == 8) ? mask(6) : mask(5))); 6734732Sgblack@eecs.umich.edu // Because what happens to the bits shift -in- on a right shift 6744732Sgblack@eecs.umich.edu // is not defined in the C/C++ standard, we have to sign extend 6754732Sgblack@eecs.umich.edu // them manually to be sure. 6764732Sgblack@eecs.umich.edu uint64_t arithMask = 6775032Sgblack@eecs.umich.edu -bits(psrc1, dataSize * 8 - 1) << (dataSize * 8 - shiftAmt); 6784823Sgblack@eecs.umich.edu DestReg = merge(DestReg, (psrc1 >> shiftAmt) | arithMask, dataSize); 6795040Sgblack@eecs.umich.edu ''' 6805076Sgblack@eecs.umich.edu flag_code = ''' 6815076Sgblack@eecs.umich.edu // If the shift amount is zero, no flags should be modified. 6825076Sgblack@eecs.umich.edu if (shiftAmt) { 6835076Sgblack@eecs.umich.edu //Zero out any flags we might modify. This way we only have to 6845076Sgblack@eecs.umich.edu //worry about setting them. 6855076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits & ~(ext & (CFBit | ECFBit | OFBit)); 6865076Sgblack@eecs.umich.edu //If some combination of the CF bits need to be set, set them. 6875076Sgblack@eecs.umich.edu if ((ext & (CFBit | ECFBit)) && bits(SrcReg1, shiftAmt - 1)) 6885076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | (ext & (CFBit | ECFBit)); 6895076Sgblack@eecs.umich.edu //Use the regular mechanisms to calculate the other flags. 6905076Sgblack@eecs.umich.edu ccFlagBits = genFlags(ccFlagBits, ext & ~(CFBit | ECFBit | OFBit), 6915076Sgblack@eecs.umich.edu DestReg, psrc1, op2); 6925076Sgblack@eecs.umich.edu } 6935076Sgblack@eecs.umich.edu ''' 6945040Sgblack@eecs.umich.edu 6955076Sgblack@eecs.umich.edu class Ror(RegOp): 6965040Sgblack@eecs.umich.edu code = ''' 6974732Sgblack@eecs.umich.edu uint8_t shiftAmt = 6984756Sgblack@eecs.umich.edu (op2 & ((dataSize == 8) ? mask(6) : mask(5))); 6994732Sgblack@eecs.umich.edu if(shiftAmt) 7004732Sgblack@eecs.umich.edu { 7014823Sgblack@eecs.umich.edu uint64_t top = psrc1 << (dataSize * 8 - shiftAmt); 7024823Sgblack@eecs.umich.edu uint64_t bottom = bits(psrc1, dataSize * 8, shiftAmt); 7034732Sgblack@eecs.umich.edu DestReg = merge(DestReg, top | bottom, dataSize); 7044732Sgblack@eecs.umich.edu } 7054732Sgblack@eecs.umich.edu else 7064732Sgblack@eecs.umich.edu DestReg = DestReg; 7075040Sgblack@eecs.umich.edu ''' 7085076Sgblack@eecs.umich.edu flag_code = ''' 7095076Sgblack@eecs.umich.edu // If the shift amount is zero, no flags should be modified. 7105076Sgblack@eecs.umich.edu if (shiftAmt) { 7115076Sgblack@eecs.umich.edu //Zero out any flags we might modify. This way we only have to 7125076Sgblack@eecs.umich.edu //worry about setting them. 7135076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits & ~(ext & (CFBit | ECFBit | OFBit)); 7145076Sgblack@eecs.umich.edu //Find the most and second most significant bits of the result. 7155076Sgblack@eecs.umich.edu int msb = bits(DestReg, dataSize * 8 - 1); 7165076Sgblack@eecs.umich.edu int smsb = bits(DestReg, dataSize * 8 - 2); 7175076Sgblack@eecs.umich.edu //If some combination of the CF bits need to be set, set them. 7185076Sgblack@eecs.umich.edu if ((ext & (CFBit | ECFBit)) && msb) 7195076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | (ext & (CFBit | ECFBit)); 7205076Sgblack@eecs.umich.edu //Figure out what the OF bit should be. 7215076Sgblack@eecs.umich.edu if ((ext & OFBit) && (msb ^ smsb)) 7225076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | OFBit; 7235076Sgblack@eecs.umich.edu //Use the regular mechanisms to calculate the other flags. 7245076Sgblack@eecs.umich.edu ccFlagBits = genFlags(ccFlagBits, ext & ~(CFBit | ECFBit | OFBit), 7255076Sgblack@eecs.umich.edu DestReg, psrc1, op2); 7265076Sgblack@eecs.umich.edu } 7275076Sgblack@eecs.umich.edu ''' 7285040Sgblack@eecs.umich.edu 7295076Sgblack@eecs.umich.edu class Rcr(RegOp): 7305040Sgblack@eecs.umich.edu code = ''' 7314733Sgblack@eecs.umich.edu uint8_t shiftAmt = 7324756Sgblack@eecs.umich.edu (op2 & ((dataSize == 8) ? mask(6) : mask(5))); 7334733Sgblack@eecs.umich.edu if(shiftAmt) 7344733Sgblack@eecs.umich.edu { 7354733Sgblack@eecs.umich.edu CCFlagBits flags = ccFlagBits; 7365138Sgblack@eecs.umich.edu uint64_t top = flags.cf << (dataSize * 8 - shiftAmt); 7374733Sgblack@eecs.umich.edu if(shiftAmt > 1) 7384823Sgblack@eecs.umich.edu top |= psrc1 << (dataSize * 8 - shiftAmt - 1); 7394823Sgblack@eecs.umich.edu uint64_t bottom = bits(psrc1, dataSize * 8, shiftAmt); 7404733Sgblack@eecs.umich.edu DestReg = merge(DestReg, top | bottom, dataSize); 7414733Sgblack@eecs.umich.edu } 7424733Sgblack@eecs.umich.edu else 7434733Sgblack@eecs.umich.edu DestReg = DestReg; 7445040Sgblack@eecs.umich.edu ''' 7455076Sgblack@eecs.umich.edu flag_code = ''' 7465076Sgblack@eecs.umich.edu // If the shift amount is zero, no flags should be modified. 7475076Sgblack@eecs.umich.edu if (shiftAmt) { 7485076Sgblack@eecs.umich.edu //Zero out any flags we might modify. This way we only have to 7495076Sgblack@eecs.umich.edu //worry about setting them. 7505076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits & ~(ext & (CFBit | ECFBit | OFBit)); 7515076Sgblack@eecs.umich.edu //Figure out what the OF bit should be. 7525076Sgblack@eecs.umich.edu if ((ext & OFBit) && ((ccFlagBits & CFBit) ^ 7535076Sgblack@eecs.umich.edu bits(SrcReg1, dataSize * 8 - 1))) 7545076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | OFBit; 7555076Sgblack@eecs.umich.edu //If some combination of the CF bits need to be set, set them. 7565076Sgblack@eecs.umich.edu if ((ext & (CFBit | ECFBit)) && bits(SrcReg1, shiftAmt - 1)) 7575076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | (ext & (CFBit | ECFBit)); 7585076Sgblack@eecs.umich.edu //Use the regular mechanisms to calculate the other flags. 7595076Sgblack@eecs.umich.edu ccFlagBits = genFlags(ccFlagBits, ext & ~(CFBit | ECFBit | OFBit), 7605076Sgblack@eecs.umich.edu DestReg, psrc1, op2); 7615076Sgblack@eecs.umich.edu } 7625076Sgblack@eecs.umich.edu ''' 7635040Sgblack@eecs.umich.edu 7645076Sgblack@eecs.umich.edu class Rol(RegOp): 7655040Sgblack@eecs.umich.edu code = ''' 7664732Sgblack@eecs.umich.edu uint8_t shiftAmt = 7674756Sgblack@eecs.umich.edu (op2 & ((dataSize == 8) ? mask(6) : mask(5))); 7684732Sgblack@eecs.umich.edu if(shiftAmt) 7694732Sgblack@eecs.umich.edu { 7704823Sgblack@eecs.umich.edu uint64_t top = psrc1 << shiftAmt; 7714732Sgblack@eecs.umich.edu uint64_t bottom = 7724823Sgblack@eecs.umich.edu bits(psrc1, dataSize * 8 - 1, dataSize * 8 - shiftAmt); 7734732Sgblack@eecs.umich.edu DestReg = merge(DestReg, top | bottom, dataSize); 7744732Sgblack@eecs.umich.edu } 7754732Sgblack@eecs.umich.edu else 7764732Sgblack@eecs.umich.edu DestReg = DestReg; 7775040Sgblack@eecs.umich.edu ''' 7785076Sgblack@eecs.umich.edu flag_code = ''' 7795076Sgblack@eecs.umich.edu // If the shift amount is zero, no flags should be modified. 7805076Sgblack@eecs.umich.edu if (shiftAmt) { 7815076Sgblack@eecs.umich.edu //Zero out any flags we might modify. This way we only have to 7825076Sgblack@eecs.umich.edu //worry about setting them. 7835076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits & ~(ext & (CFBit | ECFBit | OFBit)); 7845076Sgblack@eecs.umich.edu //The CF bits, if set, would be set to the lsb of the result. 7855076Sgblack@eecs.umich.edu int lsb = DestReg & 0x1; 7865076Sgblack@eecs.umich.edu int msb = bits(DestReg, dataSize * 8 - 1); 7875076Sgblack@eecs.umich.edu //If some combination of the CF bits need to be set, set them. 7885076Sgblack@eecs.umich.edu if ((ext & (CFBit | ECFBit)) && lsb) 7895076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | (ext & (CFBit | ECFBit)); 7905076Sgblack@eecs.umich.edu //Figure out what the OF bit should be. 7915076Sgblack@eecs.umich.edu if ((ext & OFBit) && (msb ^ lsb)) 7925076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | OFBit; 7935076Sgblack@eecs.umich.edu //Use the regular mechanisms to calculate the other flags. 7945076Sgblack@eecs.umich.edu ccFlagBits = genFlags(ccFlagBits, ext & ~(CFBit | ECFBit | OFBit), 7955076Sgblack@eecs.umich.edu DestReg, psrc1, op2); 7965076Sgblack@eecs.umich.edu } 7975076Sgblack@eecs.umich.edu ''' 7985040Sgblack@eecs.umich.edu 7995076Sgblack@eecs.umich.edu class Rcl(RegOp): 8005040Sgblack@eecs.umich.edu code = ''' 8014733Sgblack@eecs.umich.edu uint8_t shiftAmt = 8024756Sgblack@eecs.umich.edu (op2 & ((dataSize == 8) ? mask(6) : mask(5))); 8034733Sgblack@eecs.umich.edu if(shiftAmt) 8044733Sgblack@eecs.umich.edu { 8054733Sgblack@eecs.umich.edu CCFlagBits flags = ccFlagBits; 8064823Sgblack@eecs.umich.edu uint64_t top = psrc1 << shiftAmt; 8075138Sgblack@eecs.umich.edu uint64_t bottom = flags.cf << (shiftAmt - 1); 8084733Sgblack@eecs.umich.edu if(shiftAmt > 1) 8094733Sgblack@eecs.umich.edu bottom |= 8104823Sgblack@eecs.umich.edu bits(psrc1, dataSize * 8 - 1, 8114809Sgblack@eecs.umich.edu dataSize * 8 - shiftAmt + 1); 8124733Sgblack@eecs.umich.edu DestReg = merge(DestReg, top | bottom, dataSize); 8134733Sgblack@eecs.umich.edu } 8144733Sgblack@eecs.umich.edu else 8154733Sgblack@eecs.umich.edu DestReg = DestReg; 8165040Sgblack@eecs.umich.edu ''' 8175076Sgblack@eecs.umich.edu flag_code = ''' 8185076Sgblack@eecs.umich.edu // If the shift amount is zero, no flags should be modified. 8195076Sgblack@eecs.umich.edu if (shiftAmt) { 8205076Sgblack@eecs.umich.edu //Zero out any flags we might modify. This way we only have to 8215076Sgblack@eecs.umich.edu //worry about setting them. 8225076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits & ~(ext & (CFBit | ECFBit | OFBit)); 8235076Sgblack@eecs.umich.edu int msb = bits(DestReg, dataSize * 8 - 1); 8245076Sgblack@eecs.umich.edu int CFBits = bits(SrcReg1, dataSize * 8 - shiftAmt); 8255076Sgblack@eecs.umich.edu //If some combination of the CF bits need to be set, set them. 8265076Sgblack@eecs.umich.edu if ((ext & (CFBit | ECFBit)) && CFBits) 8275076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | (ext & (CFBit | ECFBit)); 8285076Sgblack@eecs.umich.edu //Figure out what the OF bit should be. 8295076Sgblack@eecs.umich.edu if ((ext & OFBit) && (msb ^ CFBits)) 8305076Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | OFBit; 8315076Sgblack@eecs.umich.edu //Use the regular mechanisms to calculate the other flags. 8325076Sgblack@eecs.umich.edu ccFlagBits = genFlags(ccFlagBits, ext & ~(CFBit | ECFBit | OFBit), 8335076Sgblack@eecs.umich.edu DestReg, psrc1, op2); 8345076Sgblack@eecs.umich.edu } 8355076Sgblack@eecs.umich.edu ''' 8364732Sgblack@eecs.umich.edu 8375040Sgblack@eecs.umich.edu class Wrip(WrRegOp, CondRegOp): 8385246Sgblack@eecs.umich.edu code = 'RIP = psrc1 + sop2 + CSBase' 8395040Sgblack@eecs.umich.edu else_code="RIP = RIP;" 8405040Sgblack@eecs.umich.edu 8415040Sgblack@eecs.umich.edu class Br(WrRegOp, CondRegOp): 8425040Sgblack@eecs.umich.edu code = 'nuIP = psrc1 + op2;' 8435040Sgblack@eecs.umich.edu else_code='nuIP = nuIP;' 8445040Sgblack@eecs.umich.edu 8455040Sgblack@eecs.umich.edu class Wruflags(WrRegOp): 8465040Sgblack@eecs.umich.edu code = 'ccFlagBits = psrc1 ^ op2' 8475040Sgblack@eecs.umich.edu 8485040Sgblack@eecs.umich.edu class Rdip(RdRegOp): 8495246Sgblack@eecs.umich.edu code = 'DestReg = RIP - CSBase' 8505040Sgblack@eecs.umich.edu 8515040Sgblack@eecs.umich.edu class Ruflags(RdRegOp): 8525040Sgblack@eecs.umich.edu code = 'DestReg = ccFlagBits' 8535040Sgblack@eecs.umich.edu 8545040Sgblack@eecs.umich.edu class Ruflag(RegOp): 8555040Sgblack@eecs.umich.edu code = ''' 8565116Sgblack@eecs.umich.edu int flag = bits(ccFlagBits, imm8); 8574951Sgblack@eecs.umich.edu DestReg = merge(DestReg, flag, dataSize); 8585011Sgblack@eecs.umich.edu ccFlagBits = (flag == 0) ? (ccFlagBits | EZFBit) : 8595011Sgblack@eecs.umich.edu (ccFlagBits & ~EZFBit); 8605040Sgblack@eecs.umich.edu ''' 8615040Sgblack@eecs.umich.edu def __init__(self, dest, imm, flags=None, \ 8625040Sgblack@eecs.umich.edu dataSize="env.dataSize"): 8635040Sgblack@eecs.umich.edu super(Ruflag, self).__init__(dest, \ 8645040Sgblack@eecs.umich.edu "NUM_INTREGS", imm, flags, dataSize) 8654732Sgblack@eecs.umich.edu 8665040Sgblack@eecs.umich.edu class Sext(RegOp): 8675040Sgblack@eecs.umich.edu code = ''' 8684823Sgblack@eecs.umich.edu IntReg val = psrc1; 8695239Sgblack@eecs.umich.edu // Mask the bit position so that it wraps. 8705239Sgblack@eecs.umich.edu int bitPos = op2 & (dataSize * 8 - 1); 8715239Sgblack@eecs.umich.edu int sign_bit = bits(val, bitPos, bitPos); 8725239Sgblack@eecs.umich.edu uint64_t maskVal = mask(bitPos+1); 8735007Sgblack@eecs.umich.edu val = sign_bit ? (val | ~maskVal) : (val & maskVal); 8745007Sgblack@eecs.umich.edu DestReg = merge(DestReg, val, dataSize); 8755040Sgblack@eecs.umich.edu ''' 8765239Sgblack@eecs.umich.edu flag_code = ''' 8775239Sgblack@eecs.umich.edu if (!sign_bit) 8785239Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits & 8795239Sgblack@eecs.umich.edu ~(ext & (CFBit | ECFBit | ZFBit | EZFBit)); 8805239Sgblack@eecs.umich.edu else 8815239Sgblack@eecs.umich.edu ccFlagBits = ccFlagBits | 8825239Sgblack@eecs.umich.edu (ext & (CFBit | ECFBit | ZFBit | EZFBit)); 8835239Sgblack@eecs.umich.edu ''' 8844714Sgblack@eecs.umich.edu 8855040Sgblack@eecs.umich.edu class Zext(RegOp): 8865239Sgblack@eecs.umich.edu code = 'DestReg = bits(psrc1, op2, 0);' 8875241Sgblack@eecs.umich.edu 8885241Sgblack@eecs.umich.edu class Wrcr(RegOp): 8895241Sgblack@eecs.umich.edu def __init__(self, dest, src1, flags=None, dataSize="env.dataSize"): 8905241Sgblack@eecs.umich.edu super(Wrcr, self).__init__(dest, \ 8915241Sgblack@eecs.umich.edu src1, "NUM_INTREGS", flags, dataSize) 8925241Sgblack@eecs.umich.edu code = ''' 8935241Sgblack@eecs.umich.edu if (dest == 1 || (dest > 4 && dest < 8) || (dest > 8)) { 8945241Sgblack@eecs.umich.edu fault = new InvalidOpcode(); 8955241Sgblack@eecs.umich.edu } else { 8965241Sgblack@eecs.umich.edu // There are *s in the line below so it doesn't confuse the 8975241Sgblack@eecs.umich.edu // parser. They may be unnecessary. 8985241Sgblack@eecs.umich.edu //Mis*cReg old*Val = pick(Cont*rolDest, 0, dat*aSize); 8995241Sgblack@eecs.umich.edu MiscReg newVal = psrc1; 9005241Sgblack@eecs.umich.edu 9015241Sgblack@eecs.umich.edu // Check for any modifications that would cause a fault. 9025241Sgblack@eecs.umich.edu switch(dest) { 9035241Sgblack@eecs.umich.edu case 0: 9045241Sgblack@eecs.umich.edu { 9055241Sgblack@eecs.umich.edu Efer efer = EferOp; 9065241Sgblack@eecs.umich.edu CR0 cr0 = newVal; 9075241Sgblack@eecs.umich.edu CR4 oldCr4 = CR4Op; 9085241Sgblack@eecs.umich.edu if (bits(newVal, 63, 32) || 9095241Sgblack@eecs.umich.edu (!cr0.pe && cr0.pg) || 9105241Sgblack@eecs.umich.edu (!cr0.cd && cr0.nw) || 9115241Sgblack@eecs.umich.edu (cr0.pg && efer.lme && !oldCr4.pae)) 9125241Sgblack@eecs.umich.edu fault = new GeneralProtection(0); 9135241Sgblack@eecs.umich.edu } 9145241Sgblack@eecs.umich.edu break; 9155241Sgblack@eecs.umich.edu case 2: 9165241Sgblack@eecs.umich.edu break; 9175241Sgblack@eecs.umich.edu case 3: 9185241Sgblack@eecs.umich.edu break; 9195241Sgblack@eecs.umich.edu case 4: 9205241Sgblack@eecs.umich.edu { 9215241Sgblack@eecs.umich.edu CR4 cr4 = newVal; 9225241Sgblack@eecs.umich.edu // PAE can't be disabled in long mode. 9235241Sgblack@eecs.umich.edu if (bits(newVal, 63, 11) || 9245241Sgblack@eecs.umich.edu (machInst.mode.mode == LongMode && !cr4.pae)) 9255241Sgblack@eecs.umich.edu fault = new GeneralProtection(0); 9265241Sgblack@eecs.umich.edu } 9275241Sgblack@eecs.umich.edu break; 9285241Sgblack@eecs.umich.edu case 8: 9295241Sgblack@eecs.umich.edu { 9305241Sgblack@eecs.umich.edu if (bits(newVal, 63, 4)) 9315241Sgblack@eecs.umich.edu fault = new GeneralProtection(0); 9325241Sgblack@eecs.umich.edu } 9335241Sgblack@eecs.umich.edu default: 9345241Sgblack@eecs.umich.edu panic("Unrecognized control register %d.\\n", dest); 9355241Sgblack@eecs.umich.edu } 9365241Sgblack@eecs.umich.edu ControlDest = newVal; 9375241Sgblack@eecs.umich.edu } 9385241Sgblack@eecs.umich.edu ''' 9394519Sgblack@eecs.umich.edu}}; 940