mem.isa revision 2107
16657Snate@binkert.org// -*- mode:c++ -*-
26657Snate@binkert.org
36657Snate@binkert.org// Copyright (c) 2003-2005 The Regents of The University of Michigan
46657Snate@binkert.org// All rights reserved.
56657Snate@binkert.org//
66657Snate@binkert.org// Redistribution and use in source and binary forms, with or without
76657Snate@binkert.org// modification, are permitted provided that the following conditions are
86657Snate@binkert.org// met: redistributions of source code must retain the above copyright
96657Snate@binkert.org// notice, this list of conditions and the following disclaimer;
106657Snate@binkert.org// redistributions in binary form must reproduce the above copyright
116657Snate@binkert.org// notice, this list of conditions and the following disclaimer in the
126657Snate@binkert.org// documentation and/or other materials provided with the distribution;
136657Snate@binkert.org// neither the name of the copyright holders nor the names of its
146657Snate@binkert.org// contributors may be used to endorse or promote products derived from
156657Snate@binkert.org// this software without specific prior written permission.
166657Snate@binkert.org//
176657Snate@binkert.org// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186657Snate@binkert.org// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
196657Snate@binkert.org// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
206657Snate@binkert.org// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216657Snate@binkert.org// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
226657Snate@binkert.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
236657Snate@binkert.org// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246657Snate@binkert.org// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256657Snate@binkert.org// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266657Snate@binkert.org// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
276657Snate@binkert.org// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286657Snate@binkert.org
296657Snate@binkert.orgoutput header {{
306657Snate@binkert.org    /**
316657Snate@binkert.org     * Base class for general Alpha memory-format instructions.
326657Snate@binkert.org     */
336657Snate@binkert.org    class Memory : public AlphaStaticInst
346657Snate@binkert.org    {
356657Snate@binkert.org      protected:
366657Snate@binkert.org
376657Snate@binkert.org        /// Memory request flags.  See mem_req_base.hh.
386657Snate@binkert.org        unsigned memAccessFlags;
396657Snate@binkert.org        /// Pointer to EAComp object.
406657Snate@binkert.org        const StaticInstPtr eaCompPtr;
416657Snate@binkert.org        /// Pointer to MemAcc object.
426657Snate@binkert.org        const StaticInstPtr memAccPtr;
436657Snate@binkert.org
446657Snate@binkert.org        /// Constructor
456657Snate@binkert.org        Memory(const char *mnem, MachInst _machInst, OpClass __opClass,
466657Snate@binkert.org               StaticInstPtr _eaCompPtr = nullStaticInstPtr,
476657Snate@binkert.org               StaticInstPtr _memAccPtr = nullStaticInstPtr)
486657Snate@binkert.org            : AlphaStaticInst(mnem, _machInst, __opClass),
496657Snate@binkert.org              memAccessFlags(0), eaCompPtr(_eaCompPtr), memAccPtr(_memAccPtr)
506657Snate@binkert.org        {
516657Snate@binkert.org        }
526657Snate@binkert.org
536657Snate@binkert.org        std::string
546657Snate@binkert.org        generateDisassembly(Addr pc, const SymbolTable *symtab) const;
556657Snate@binkert.org
566657Snate@binkert.org      public:
576657Snate@binkert.org
586657Snate@binkert.org        const StaticInstPtr &eaCompInst() const { return eaCompPtr; }
596657Snate@binkert.org        const StaticInstPtr &memAccInst() const { return memAccPtr; }
606657Snate@binkert.org    };
616657Snate@binkert.org
626657Snate@binkert.org    /**
636657Snate@binkert.org     * Base class for memory-format instructions using a 32-bit
646657Snate@binkert.org     * displacement (i.e. most of them).
656657Snate@binkert.org     */
666657Snate@binkert.org    class MemoryDisp32 : public Memory
676657Snate@binkert.org    {
686657Snate@binkert.org      protected:
696657Snate@binkert.org        /// Displacement for EA calculation (signed).
706657Snate@binkert.org        int32_t disp;
716657Snate@binkert.org
726657Snate@binkert.org        /// Constructor.
736657Snate@binkert.org        MemoryDisp32(const char *mnem, MachInst _machInst, OpClass __opClass,
746657Snate@binkert.org                     StaticInstPtr _eaCompPtr = nullStaticInstPtr,
756657Snate@binkert.org                     StaticInstPtr _memAccPtr = nullStaticInstPtr)
766657Snate@binkert.org            : Memory(mnem, _machInst, __opClass, _eaCompPtr, _memAccPtr),
776657Snate@binkert.org              disp(MEMDISP)
786657Snate@binkert.org        {
796657Snate@binkert.org        }
806657Snate@binkert.org    };
816657Snate@binkert.org
82
83    /**
84     * Base class for a few miscellaneous memory-format insts
85     * that don't interpret the disp field: wh64, fetch, fetch_m, ecb.
86     * None of these instructions has a destination register either.
87     */
88    class MemoryNoDisp : public Memory
89    {
90      protected:
91        /// Constructor
92        MemoryNoDisp(const char *mnem, MachInst _machInst, OpClass __opClass,
93                     StaticInstPtr _eaCompPtr = nullStaticInstPtr,
94                     StaticInstPtr _memAccPtr = nullStaticInstPtr)
95            : Memory(mnem, _machInst, __opClass, _eaCompPtr, _memAccPtr)
96        {
97        }
98
99        std::string
100        generateDisassembly(Addr pc, const SymbolTable *symtab) const;
101    };
102}};
103
104
105output decoder {{
106    std::string
107    Memory::generateDisassembly(Addr pc, const SymbolTable *symtab) const
108    {
109        return csprintf("%-10s %c%d,%d(r%d)", mnemonic,
110                        flags[IsFloating] ? 'f' : 'r', RA, MEMDISP, RB);
111    }
112
113    std::string
114    MemoryNoDisp::generateDisassembly(Addr pc, const SymbolTable *symtab) const
115    {
116        return csprintf("%-10s (r%d)", mnemonic, RB);
117    }
118}};
119
120def format LoadAddress(code) {{
121    iop = InstObjParams(name, Name, 'MemoryDisp32', CodeBlock(code))
122    header_output = BasicDeclare.subst(iop)
123    decoder_output = BasicConstructor.subst(iop)
124    decode_block = BasicDecode.subst(iop)
125    exec_output = BasicExecute.subst(iop)
126}};
127
128
129def template LoadStoreDeclare {{
130    /**
131     * Static instruction class for "%(mnemonic)s".
132     */
133    class %(class_name)s : public %(base_class)s
134    {
135      protected:
136
137        /**
138         * "Fake" effective address computation class for "%(mnemonic)s".
139         */
140        class EAComp : public %(base_class)s
141        {
142          public:
143            /// Constructor
144            EAComp(MachInst machInst);
145
146            %(BasicExecDeclare)s
147        };
148
149        /**
150         * "Fake" memory access instruction class for "%(mnemonic)s".
151         */
152        class MemAcc : public %(base_class)s
153        {
154          public:
155            /// Constructor
156            MemAcc(MachInst machInst);
157
158            %(BasicExecDeclare)s
159        };
160
161      public:
162
163        /// Constructor.
164        %(class_name)s(MachInst machInst);
165
166        %(BasicExecDeclare)s
167
168        %(InitiateAccDeclare)s
169
170        %(CompleteAccDeclare)s
171    };
172}};
173
174
175def template InitiateAccDeclare {{
176    Fault * initiateAcc(%(CPU_exec_context)s *, Trace::InstRecord *) const;
177}};
178
179
180def template CompleteAccDeclare {{
181    Fault * completeAcc(uint8_t *, %(CPU_exec_context)s *, Trace::InstRecord *) const;
182}};
183
184
185def template LoadStoreConstructor {{
186    /** TODO: change op_class to AddrGenOp or something (requires
187     * creating new member of OpClass enum in op_class.hh, updating
188     * config files, etc.). */
189    inline %(class_name)s::EAComp::EAComp(MachInst machInst)
190        : %(base_class)s("%(mnemonic)s (EAComp)", machInst, IntAluOp)
191    {
192        %(ea_constructor)s;
193    }
194
195    inline %(class_name)s::MemAcc::MemAcc(MachInst machInst)
196        : %(base_class)s("%(mnemonic)s (MemAcc)", machInst, %(op_class)s)
197    {
198        %(memacc_constructor)s;
199    }
200
201    inline %(class_name)s::%(class_name)s(MachInst machInst)
202         : %(base_class)s("%(mnemonic)s", machInst, %(op_class)s,
203                          new EAComp(machInst), new MemAcc(machInst))
204    {
205        %(constructor)s;
206    }
207}};
208
209
210def template EACompExecute {{
211    Fault *
212    %(class_name)s::EAComp::execute(%(CPU_exec_context)s *xc,
213                                   Trace::InstRecord *traceData) const
214    {
215        Addr EA;
216        Fault * fault = NoFault;
217
218        %(fp_enable_check)s;
219        %(op_decl)s;
220        %(op_rd)s;
221        %(code)s;
222
223        if (fault == NoFault) {
224            %(op_wb)s;
225            xc->setEA(EA);
226        }
227
228        return fault;
229    }
230}};
231
232def template LoadMemAccExecute {{
233    Fault *
234    %(class_name)s::MemAcc::execute(%(CPU_exec_context)s *xc,
235                                   Trace::InstRecord *traceData) const
236    {
237        Addr EA;
238        Fault * fault = NoFault;
239
240        %(fp_enable_check)s;
241        %(op_decl)s;
242        %(op_rd)s;
243        EA = xc->getEA();
244
245        if (fault == NoFault) {
246            fault = xc->read(EA, (uint%(mem_acc_size)d_t&)Mem, memAccessFlags);
247            %(code)s;
248        }
249
250        if (fault == NoFault) {
251            %(op_wb)s;
252        }
253
254        return fault;
255    }
256}};
257
258
259def template LoadExecute {{
260    Fault * %(class_name)s::execute(%(CPU_exec_context)s *xc,
261                                  Trace::InstRecord *traceData) const
262    {
263        Addr EA;
264        Fault * fault = NoFault;
265
266        %(fp_enable_check)s;
267        %(op_decl)s;
268        %(op_rd)s;
269        %(ea_code)s;
270
271        if (fault == NoFault) {
272            fault = xc->read(EA, (uint%(mem_acc_size)d_t&)Mem, memAccessFlags);
273            %(memacc_code)s;
274        }
275
276        if (fault == NoFault) {
277            %(op_wb)s;
278        }
279
280        return fault;
281    }
282}};
283
284
285def template LoadInitiateAcc {{
286    Fault * %(class_name)s::initiateAcc(%(CPU_exec_context)s *xc,
287                                      Trace::InstRecord *traceData) const
288    {
289        Addr EA;
290        Fault * fault = NoFault;
291        %(mem_acc_type)s Mem = 0;
292
293        %(fp_enable_check)s;
294        %(op_src_decl)s;
295        %(op_rd)s;
296        %(ea_code)s;
297
298        if (fault == NoFault) {
299            fault = xc->read(EA, (uint%(mem_acc_size)d_t &)Mem, memAccessFlags);
300        }
301
302        return fault;
303    }
304}};
305
306
307def template LoadCompleteAcc {{
308    Fault * %(class_name)s::completeAcc(uint8_t *data,
309                                      %(CPU_exec_context)s *xc,
310                                      Trace::InstRecord *traceData) const
311    {
312        Fault * fault = NoFault;
313        %(mem_acc_type)s Mem = 0;
314
315        %(fp_enable_check)s;
316        %(op_dest_decl)s;
317
318        memcpy(&Mem, data, sizeof(Mem));
319
320        if (fault == NoFault) {
321            %(memacc_code)s;
322        }
323
324        if (fault == NoFault) {
325            %(op_wb)s;
326        }
327
328        return fault;
329    }
330}};
331
332
333def template StoreMemAccExecute {{
334    Fault *
335    %(class_name)s::MemAcc::execute(%(CPU_exec_context)s *xc,
336                                   Trace::InstRecord *traceData) const
337    {
338        Addr EA;
339        Fault * fault = NoFault;
340        uint64_t write_result = 0;
341
342        %(fp_enable_check)s;
343        %(op_decl)s;
344        %(op_rd)s;
345        EA = xc->getEA();
346
347        if (fault == NoFault) {
348            %(code)s;
349        }
350
351        if (fault == NoFault) {
352            fault = xc->write((uint%(mem_acc_size)d_t&)Mem, EA,
353                              memAccessFlags, &write_result);
354            if (traceData) { traceData->setData(Mem); }
355        }
356
357        if (fault == NoFault) {
358            %(postacc_code)s;
359        }
360
361        if (fault == NoFault) {
362            %(op_wb)s;
363        }
364
365        return fault;
366    }
367}};
368
369
370def template StoreExecute {{
371    Fault * %(class_name)s::execute(%(CPU_exec_context)s *xc,
372                                  Trace::InstRecord *traceData) const
373    {
374        Addr EA;
375        Fault * fault = NoFault;
376        uint64_t write_result = 0;
377
378        %(fp_enable_check)s;
379        %(op_decl)s;
380        %(op_rd)s;
381        %(ea_code)s;
382
383        if (fault == NoFault) {
384            %(memacc_code)s;
385        }
386
387        if (fault == NoFault) {
388            fault = xc->write((uint%(mem_acc_size)d_t&)Mem, EA,
389                              memAccessFlags, &write_result);
390            if (traceData) { traceData->setData(Mem); }
391        }
392
393        if (fault == NoFault) {
394            %(postacc_code)s;
395        }
396
397        if (fault == NoFault) {
398            %(op_wb)s;
399        }
400
401        return fault;
402    }
403}};
404
405def template StoreInitiateAcc {{
406    Fault * %(class_name)s::initiateAcc(%(CPU_exec_context)s *xc,
407                                      Trace::InstRecord *traceData) const
408    {
409        Addr EA;
410        Fault * fault = NoFault;
411        uint64_t write_result = 0;
412        %(mem_acc_type)s Mem = 0;
413
414        %(fp_enable_check)s;
415        %(op_src_decl)s;
416        %(op_rd)s;
417        %(ea_code)s;
418
419        if (fault == NoFault) {
420            %(memacc_code)s;
421        }
422
423        if (fault == NoFault) {
424            fault = xc->write((uint%(mem_acc_size)d_t&)Mem, EA,
425                              memAccessFlags, &write_result);
426            if (traceData) { traceData->setData(Mem); }
427        }
428
429        return fault;
430    }
431}};
432
433
434def template StoreCompleteAcc {{
435    Fault * %(class_name)s::completeAcc(uint8_t *data,
436                                      %(CPU_exec_context)s *xc,
437                                      Trace::InstRecord *traceData) const
438    {
439        Fault * fault = NoFault;
440        uint64_t write_result = 0;
441
442        %(fp_enable_check)s;
443        %(op_dest_decl)s;
444
445        memcpy(&write_result, data, sizeof(write_result));
446
447        if (fault == NoFault) {
448            %(postacc_code)s;
449        }
450
451        if (fault == NoFault) {
452            %(op_wb)s;
453        }
454
455        return fault;
456    }
457}};
458
459
460def template MiscMemAccExecute {{
461    Fault * %(class_name)s::MemAcc::execute(%(CPU_exec_context)s *xc,
462                                          Trace::InstRecord *traceData) const
463    {
464        Addr EA;
465        Fault * fault = NoFault;
466
467        %(fp_enable_check)s;
468        %(op_decl)s;
469        %(op_rd)s;
470        EA = xc->getEA();
471
472        if (fault == NoFault) {
473            %(code)s;
474        }
475
476        return NoFault;
477    }
478}};
479
480def template MiscExecute {{
481    Fault * %(class_name)s::execute(%(CPU_exec_context)s *xc,
482                                  Trace::InstRecord *traceData) const
483    {
484        Addr EA;
485        Fault * fault = NoFault;
486
487        %(fp_enable_check)s;
488        %(op_decl)s;
489        %(op_rd)s;
490        %(ea_code)s;
491
492        if (fault == NoFault) {
493            %(memacc_code)s;
494        }
495
496        return NoFault;
497    }
498}};
499
500def template MiscInitiateAcc {{
501    Fault * %(class_name)s::initiateAcc(%(CPU_exec_context)s *xc,
502                                      Trace::InstRecord *traceData) const
503    {
504        Addr EA;
505        Fault * fault = NoFault;
506
507        %(fp_enable_check)s;
508        %(op_decl)s;
509        %(op_rd)s;
510        %(ea_code)s;
511
512        if (fault == NoFault) {
513            %(memacc_code)s;
514        }
515
516        return NoFault;
517    }
518}};
519
520
521def template MiscCompleteAcc {{
522    Fault * %(class_name)s::completeAcc(uint8_t *data,
523                                      %(CPU_exec_context)s *xc,
524                                      Trace::InstRecord *traceData) const
525    {
526        return NoFault;
527    }
528}};
529
530// load instructions use Ra as dest, so check for
531// Ra == 31 to detect nops
532def template LoadNopCheckDecode {{
533 {
534     AlphaStaticInst *i = new %(class_name)s(machInst);
535     if (RA == 31) {
536         i = makeNop(i);
537     }
538     return i;
539 }
540}};
541
542
543// for some load instructions, Ra == 31 indicates a prefetch (not a nop)
544def template LoadPrefetchCheckDecode {{
545 {
546     if (RA != 31) {
547         return new %(class_name)s(machInst);
548     }
549     else {
550         return new %(class_name)sPrefetch(machInst);
551     }
552 }
553}};
554
555
556let {{
557def LoadStoreBase(name, Name, ea_code, memacc_code, mem_flags, inst_flags,
558                  postacc_code = '', base_class = 'MemoryDisp32',
559                  decode_template = BasicDecode, exec_template_base = ''):
560    # Make sure flags are in lists (convert to lists if not).
561    mem_flags = makeList(mem_flags)
562    inst_flags = makeList(inst_flags)
563
564    # add hook to get effective addresses into execution trace output.
565    ea_code += '\nif (traceData) { traceData->setAddr(EA); }\n'
566
567    # generate code block objects
568    ea_cblk = CodeBlock(ea_code)
569    memacc_cblk = CodeBlock(memacc_code)
570    postacc_cblk = CodeBlock(postacc_code)
571
572    # Some CPU models execute the memory operation as an atomic unit,
573    # while others want to separate them into an effective address
574    # computation and a memory access operation.  As a result, we need
575    # to generate three StaticInst objects.  Note that the latter two
576    # are nested inside the larger "atomic" one.
577
578    # generate InstObjParams for EAComp object
579    ea_iop = InstObjParams(name, Name, base_class, ea_cblk, inst_flags)
580
581    # generate InstObjParams for MemAcc object
582    memacc_iop = InstObjParams(name, Name, base_class, memacc_cblk, inst_flags)
583    # in the split execution model, the MemAcc portion is responsible
584    # for the post-access code.
585    memacc_iop.postacc_code = postacc_cblk.code
586
587    # generate InstObjParams for unified execution
588    cblk = CodeBlock(ea_code + memacc_code + postacc_code)
589    iop = InstObjParams(name, Name, base_class, cblk, inst_flags)
590
591    iop.ea_constructor = ea_cblk.constructor
592    iop.ea_code = ea_cblk.code
593    iop.memacc_constructor = memacc_cblk.constructor
594    iop.memacc_code = memacc_cblk.code
595    iop.postacc_code = postacc_cblk.code
596
597    if mem_flags:
598        s = '\n\tmemAccessFlags = ' + string.join(mem_flags, '|') + ';'
599        iop.constructor += s
600        memacc_iop.constructor += s
601
602    # select templates
603    memAccExecTemplate = eval(exec_template_base + 'MemAccExecute')
604    fullExecTemplate = eval(exec_template_base + 'Execute')
605    initiateAccTemplate = eval(exec_template_base + 'InitiateAcc')
606    completeAccTemplate = eval(exec_template_base + 'CompleteAcc')
607
608    # (header_output, decoder_output, decode_block, exec_output)
609    return (LoadStoreDeclare.subst(iop), LoadStoreConstructor.subst(iop),
610            decode_template.subst(iop),
611            EACompExecute.subst(ea_iop)
612            + memAccExecTemplate.subst(memacc_iop)
613            + fullExecTemplate.subst(iop)
614            + initiateAccTemplate.subst(iop)
615            + completeAccTemplate.subst(iop))
616}};
617
618
619def format LoadOrNop(memacc_code, ea_code = {{ EA = Rb + disp; }},
620                     mem_flags = [], inst_flags = []) {{
621    (header_output, decoder_output, decode_block, exec_output) = \
622        LoadStoreBase(name, Name, ea_code, memacc_code, mem_flags, inst_flags,
623                      decode_template = LoadNopCheckDecode,
624                      exec_template_base = 'Load')
625}};
626
627
628// Note that the flags passed in apply only to the prefetch version
629def format LoadOrPrefetch(memacc_code, ea_code = {{ EA = Rb + disp; }},
630                          mem_flags = [], pf_flags = [], inst_flags = []) {{
631    # declare the load instruction object and generate the decode block
632    (header_output, decoder_output, decode_block, exec_output) = \
633        LoadStoreBase(name, Name, ea_code, memacc_code, mem_flags, inst_flags,
634                      decode_template = LoadPrefetchCheckDecode,
635                      exec_template_base = 'Load')
636
637    # Declare the prefetch instruction object.
638
639    # Make sure flag args are lists so we can mess with them.
640    mem_flags = makeList(mem_flags)
641    pf_flags = makeList(pf_flags)
642    inst_flags = makeList(inst_flags)
643
644    pf_mem_flags = mem_flags + pf_flags + ['NO_FAULT']
645    pf_inst_flags = inst_flags + ['IsMemRef', 'IsLoad',
646                                  'IsDataPrefetch', 'MemReadOp']
647
648    (pf_header_output, pf_decoder_output, _, pf_exec_output) = \
649        LoadStoreBase(name, Name + 'Prefetch', ea_code,
650                      'xc->prefetch(EA, memAccessFlags);',
651                      pf_mem_flags, pf_inst_flags, exec_template_base = 'Misc')
652
653    header_output += pf_header_output
654    decoder_output += pf_decoder_output
655    exec_output += pf_exec_output
656}};
657
658
659def format Store(memacc_code, ea_code = {{ EA = Rb + disp; }},
660                 mem_flags = [], inst_flags = []) {{
661    (header_output, decoder_output, decode_block, exec_output) = \
662        LoadStoreBase(name, Name, ea_code, memacc_code, mem_flags, inst_flags,
663                      exec_template_base = 'Store')
664}};
665
666
667def format StoreCond(memacc_code, postacc_code,
668                     ea_code = {{ EA = Rb + disp; }},
669                     mem_flags = [], inst_flags = []) {{
670    (header_output, decoder_output, decode_block, exec_output) = \
671        LoadStoreBase(name, Name, ea_code, memacc_code, mem_flags, inst_flags,
672                      postacc_code, exec_template_base = 'Store')
673}};
674
675
676// Use 'MemoryNoDisp' as base: for wh64, fetch, ecb
677def format MiscPrefetch(ea_code, memacc_code,
678                        mem_flags = [], inst_flags = []) {{
679    (header_output, decoder_output, decode_block, exec_output) = \
680        LoadStoreBase(name, Name, ea_code, memacc_code, mem_flags, inst_flags,
681                      base_class = 'MemoryNoDisp', exec_template_base = 'Misc')
682}};
683
684
685