1// -*- mode:c++ -*-
2
3// Copyright (c) 2010-2012 ARM Limited
4// All rights reserved
5//
6// The license below extends only to copyright in the software and shall
7// not be construed as granting a license to any other intellectual
8// property including but not limited to intellectual property relating
9// to a hardware implementation of the functionality of the software
10// licensed hereunder.  You may use the software subject to the license
11// terms below provided that you ensure that this notice is replicated
12// unmodified and in its entirety in all distributions of the software,
13// modified or unmodified, in source code or in binary form.
14//
15// Redistribution and use in source and binary forms, with or without
16// modification, are permitted provided that the following conditions are
17// met: redistributions of source code must retain the above copyright
18// notice, this list of conditions and the following disclaimer;
19// redistributions in binary form must reproduce the above copyright
20// notice, this list of conditions and the following disclaimer in the
21// documentation and/or other materials provided with the distribution;
22// neither the name of the copyright holders nor the names of its
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Authors: Gabe Black
39
40let {{
41
42    class LoadStoreInst(object):
43        def __init__(self):
44            self.fullExecTemplate = eval(self.execBase + 'Execute')
45            self.initiateAccTemplate = eval(self.execBase + 'InitiateAcc')
46            self.completeAccTemplate = eval(self.execBase + 'CompleteAcc')
47            self.declareTemplate = eval(self.decConstBase + 'Declare')
48            self.constructTemplate = eval(self.decConstBase + 'Constructor')
49
50        def fillTemplates(self, name, Name, codeBlobs, memFlags, instFlags,
51                          base='Memory', wbDecl=None, pcDecl=None,
52                          rasPop=False, size=4, sign=False, faCode=None):
53            # Make sure flags are in lists (convert to lists if not).
54            memFlags = makeList(memFlags)
55            instFlags = makeList(instFlags)
56
57            eaCode = codeBlobs["ea_code"]
58
59            # This shouldn't be part of the eaCode, but until the exec templates
60            # are converted over it's the easiest place to put it.
61            eaCode += '\n    unsigned memAccessFlags = '
62            eaCode += (string.join(memFlags, '|') + ';')
63
64            codeBlobs["ea_code"] = eaCode
65
66            if faCode:
67                # For AArch64 the fa_code snippet comes already assembled here
68                codeBlobs["fa_code"] = faCode
69            elif wbDecl == None:
70                codeBlobs["fa_code"] = '''
71                    if (dest != INTREG_PC) {
72                        fault->annotate(ArmFault::SAS, %s);
73                        fault->annotate(ArmFault::SSE, %s);
74                        fault->annotate(ArmFault::SRT, dest);
75                    }
76                ''' %("0"    if size == 1 else
77                      "1"    if size == 2 else "2",
78                      "true" if sign      else "false")
79            else:
80                codeBlobs["fa_code"] = ''
81
82            macroName = Name
83            instFlagsCopy = list(instFlags)
84            codeBlobsCopy = dict(codeBlobs)
85
86            use_uops = 0
87            if wbDecl is not None or pcDecl is not None:
88                instFlagsCopy.append('IsMicroop')
89                Name = Name + 'Acc'
90                use_uops = 1
91
92            use_wb = 0
93            use_pc = 0
94            if wbDecl is not None:
95                use_wb = 1
96            if pcDecl is not None:
97                use_pc = 1
98
99            codeBlobsCopy['acc_name'] = Name
100            codeBlobsCopy['wb_decl'] = wbDecl
101            codeBlobsCopy['pc_decl'] = pcDecl
102            codeBlobsCopy['use_uops'] = 0
103            codeBlobsCopy['use_wb'] = 0
104            codeBlobsCopy['use_pc'] = 0
105            is_ras_pop = "0"
106            if rasPop:
107                is_ras_pop = "1"
108            codeBlobsCopy['is_ras_pop'] = is_ras_pop
109            if 'memacc_epilog_code' in codeBlobsCopy:
110                del codeBlobsCopy['memacc_epilog_code']
111
112            iop = InstObjParams(name, Name, base,
113                                codeBlobsCopy, instFlagsCopy)
114            if 'memacc_epilog_code' in codeBlobs:
115                iop.snippets['memacc_code'] += codeBlobs['memacc_epilog_code']
116            header_output = self.declareTemplate.subst(iop)
117            decoder_output = self.constructTemplate.subst(iop)
118            exec_output = self.fullExecTemplate.subst(iop) + \
119                          self.initiateAccTemplate.subst(iop) + \
120                          self.completeAccTemplate.subst(iop)
121
122            if wbDecl is not None or pcDecl is not None:
123                iop = InstObjParams(name, macroName, base,
124                                    { "wb_decl" : wbDecl,
125                                      "pc_decl" : pcDecl,
126                                      "acc_name" : Name,
127                                      "use_uops" : use_uops,
128                                      "use_pc" : use_pc,
129                                      "use_wb" : use_wb,
130                                      "fa_code" : '',
131                                      "is_ras_pop" : is_ras_pop },
132                                    ['IsMacroop'])
133                header_output += self.declareTemplate.subst(iop)
134                decoder_output += self.constructTemplate.subst(iop)
135                exec_output += PanicExecute.subst(iop) + \
136                               PanicInitiateAcc.subst(iop) + \
137                               PanicCompleteAcc.subst(iop)
138
139            return (header_output, decoder_output, exec_output)
140
141    def pickPredicate(blobs):
142        opt_nz = True
143        opt_c = 'opt'
144        opt_v = True
145
146        if not isinstance(blobs, dict):
147            vals = [blobs]
148        else:
149            vals = blobs.values()
150        for val in vals:
151            if re.search('(?<!Opt)CondCodesNZ(?!.*=)', val):
152                opt_nz = False
153            if re.search('OptShiftRmCondCodesC(?!.*=)', val):
154                opt_c = 'opt_shift_rm'
155            elif re.search('(?<!Opt)CondCodesC(?!.*=)', val):
156                opt_c = 'none'
157            if re.search('(?<!Opt)CondCodesV(?!.*=)', val):
158                opt_v = False
159
160        # Build up the predicate piece by piece depending on which
161        # flags the instruction needs
162        predicate = 'testPredicate('
163        if opt_nz:
164            predicate += 'OptCondCodesNZ, '
165        else:
166            predicate += 'CondCodesNZ, '
167        if opt_c == 'opt':
168            predicate += 'OptCondCodesC, '
169        elif opt_c == 'opt_shift_rm':
170            predicate += 'OptShiftRmCondCodesC, '
171        else:
172            predicate += 'CondCodesC, '
173        if opt_v:
174            predicate += 'OptCondCodesV, '
175        else:
176            predicate += 'CondCodesV, '
177        predicate += 'condCode)'
178        predicate += '/*auto*/'
179        return predicate
180
181    def memClassName(base, post, add, writeback, \
182                     size=4, sign=False, user=False):
183        Name = base
184
185        parts = { "P" : post, "A" : add, "W" : writeback,
186                  "S" : sign, "U" : user }
187
188        for (letter, val) in parts.items():
189            if val:
190                Name += "_%sY" % letter
191            else:
192                Name += "_%sN" % letter
193
194        Name += ('_SZ%d' % size)
195
196        return Name
197
198    def buildMemSuffix(sign, size):
199        if size == 16:
200            memSuffix = '_tud'
201        elif size == 8:
202            memSuffix = '_ud'
203        elif size == 4:
204            if sign:
205                memSuffix = '_sw'
206            else:
207                memSuffix = '_uw'
208        elif size == 2:
209            if sign:
210                memSuffix = '_sh'
211            else:
212                memSuffix = '_uh'
213        elif size == 1:
214            if sign:
215                memSuffix = '_sb'
216            else:
217                memSuffix = '_ub'
218        else:
219            raise Exception, "Unrecognized size for access %d" % size
220
221        return memSuffix
222
223    def buildMemBase(base, post, writeback):
224        if post and writeback:
225            base = "MemoryPostIndex<%s>" % base
226        elif not post and writeback:
227            base = "MemoryPreIndex<%s>" % base
228        elif not post and not writeback:
229            base = "MemoryOffset<%s>" % base
230        else:
231            raise Exception, "Illegal combination of post and writeback"
232        return base
233}};
234
235