]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/internal/obj/ppc64/asm9.go
cmd/internal/asm/ppc64: support 34b ADD/MOVD $const, Rx
[gostls13.git] / src / cmd / internal / obj / ppc64 / asm9.go
1 // cmd/9l/optab.c, cmd/9l/asmout.c from Vita Nuova.
2 //
3 //      Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
4 //      Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
5 //      Portions Copyright © 1997-1999 Vita Nuova Limited
6 //      Portions Copyright © 2000-2008 Vita Nuova Holdings Limited (www.vitanuova.com)
7 //      Portions Copyright © 2004,2006 Bruce Ellis
8 //      Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
9 //      Revisions Copyright © 2000-2008 Lucent Technologies Inc. and others
10 //      Portions Copyright © 2009 The Go Authors. All rights reserved.
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy
13 // of this software and associated documentation files (the "Software"), to deal
14 // in the Software without restriction, including without limitation the rights
15 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 // copies of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be included in
20 // all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 // THE SOFTWARE.
29
30 package ppc64
31
32 import (
33         "cmd/internal/obj"
34         "cmd/internal/objabi"
35         "encoding/binary"
36         "fmt"
37         "internal/buildcfg"
38         "log"
39         "math"
40         "math/bits"
41         "sort"
42 )
43
44 // ctxt9 holds state while assembling a single function.
45 // Each function gets a fresh ctxt9.
46 // This allows for multiple functions to be safely concurrently assembled.
47 type ctxt9 struct {
48         ctxt       *obj.Link
49         newprog    obj.ProgAlloc
50         cursym     *obj.LSym
51         autosize   int32
52         instoffset int64
53         pc         int64
54 }
55
56 // Instruction layout.
57
58 const (
59         r0iszero = 1
60 )
61
62 const (
63         // R bit option in prefixed load/store/add D-form operations
64         PFX_R_ABS   = 0 // Offset is absolute
65         PFX_R_PCREL = 1 // Offset is relative to PC, RA should be 0
66 )
67
68 type Optab struct {
69         as    obj.As // Opcode
70         a1    uint8  // p.From argument (obj.Addr). p is of type obj.Prog.
71         a2    uint8  // p.Reg argument (int16 Register)
72         a3    uint8  // p.RestArgs[0]  (obj.AddrPos)
73         a4    uint8  // p.RestArgs[1]
74         a5    uint8  // p.RestARgs[2]
75         a6    uint8  // p.To (obj.Addr)
76         type_ int8   // cases in asmout below. E.g., 44 = st r,(ra+rb); 45 = ld (ra+rb), r
77         size  int8   // Text space in bytes to lay operation
78
79         // A prefixed instruction is generated by this opcode. This cannot be placed
80         // across a 64B PC address. Opcodes should not translate to more than one
81         // prefixed instruction. The prefixed instruction should be written first
82         // (e.g when Optab.size > 8).
83         ispfx bool
84
85         asmout func(*ctxt9, *obj.Prog, *Optab, *[5]uint32)
86 }
87
88 // optab contains an array to be sliced of accepted operand combinations for an
89 // instruction. Unused arguments and fields are not explicitly enumerated, and
90 // should not be listed for clarity. Unused arguments and values should always
91 // assume the default value for the given type.
92 //
93 // optab does not list every valid ppc64 opcode, it enumerates representative
94 // operand combinations for a class of instruction.  The variable oprange indexes
95 // all valid ppc64 opcodes.
96 //
97 // oprange is initialized to point a slice within optab which contains the valid
98 // operand combinations for a given instruction.  This is initialized from buildop.
99 //
100 // Likewise, each slice of optab is dynamically sorted using the ocmp Sort interface
101 // to arrange entries to minimize text size of each opcode.
102 //
103 // optab is the sorted result of combining optabBase, optabGen, and prefixableOptab.
104 var optab []Optab
105
106 var optabBase = []Optab{
107         {as: obj.ATEXT, a1: C_LOREG, a6: C_TEXTSIZE, type_: 0, size: 0},
108         {as: obj.ATEXT, a1: C_LOREG, a3: C_LCON, a6: C_TEXTSIZE, type_: 0, size: 0},
109         {as: obj.ATEXT, a1: C_ADDR, a6: C_TEXTSIZE, type_: 0, size: 0},
110         {as: obj.ATEXT, a1: C_ADDR, a3: C_LCON, a6: C_TEXTSIZE, type_: 0, size: 0},
111         /* move register */
112         {as: AADD, a1: C_REG, a2: C_REG, a6: C_REG, type_: 2, size: 4},
113         {as: AADD, a1: C_REG, a6: C_REG, type_: 2, size: 4},
114         {as: AADD, a1: C_SCON, a2: C_REG, a6: C_REG, type_: 4, size: 4},
115         {as: AADD, a1: C_SCON, a6: C_REG, type_: 4, size: 4},
116         {as: AADD, a1: C_ADDCON, a2: C_REG, a6: C_REG, type_: 4, size: 4},
117         {as: AADD, a1: C_ADDCON, a6: C_REG, type_: 4, size: 4},
118         {as: AADD, a1: C_UCON, a2: C_REG, a6: C_REG, type_: 20, size: 4},
119         {as: AADD, a1: C_UCON, a6: C_REG, type_: 20, size: 4},
120         {as: AADD, a1: C_ANDCON, a2: C_REG, a6: C_REG, type_: 22, size: 8},
121         {as: AADD, a1: C_ANDCON, a6: C_REG, type_: 22, size: 8},
122         {as: AADDIS, a1: C_ADDCON, a2: C_REG, a6: C_REG, type_: 20, size: 4},
123         {as: AADDIS, a1: C_ADDCON, a6: C_REG, type_: 20, size: 4},
124         {as: AADDC, a1: C_REG, a2: C_REG, a6: C_REG, type_: 2, size: 4},
125         {as: AADDC, a1: C_REG, a6: C_REG, type_: 2, size: 4},
126         {as: AADDC, a1: C_ADDCON, a2: C_REG, a6: C_REG, type_: 4, size: 4},
127         {as: AADDC, a1: C_ADDCON, a6: C_REG, type_: 4, size: 4},
128         {as: AADDC, a1: C_LCON, a2: C_REG, a6: C_REG, type_: 22, size: 12},
129         {as: AADDC, a1: C_LCON, a6: C_REG, type_: 22, size: 12},
130         {as: AAND, a1: C_REG, a2: C_REG, a6: C_REG, type_: 6, size: 4}, /* logical, no literal */
131         {as: AAND, a1: C_REG, a6: C_REG, type_: 6, size: 4},
132         {as: AANDCC, a1: C_REG, a2: C_REG, a6: C_REG, type_: 6, size: 4},
133         {as: AANDCC, a1: C_REG, a6: C_REG, type_: 6, size: 4},
134         {as: AANDCC, a1: C_ANDCON, a6: C_REG, type_: 58, size: 4},
135         {as: AANDCC, a1: C_ANDCON, a2: C_REG, a6: C_REG, type_: 58, size: 4},
136         {as: AANDCC, a1: C_UCON, a6: C_REG, type_: 59, size: 4},
137         {as: AANDCC, a1: C_UCON, a2: C_REG, a6: C_REG, type_: 59, size: 4},
138         {as: AANDCC, a1: C_ADDCON, a6: C_REG, type_: 23, size: 8},
139         {as: AANDCC, a1: C_ADDCON, a2: C_REG, a6: C_REG, type_: 23, size: 8},
140         {as: AANDCC, a1: C_LCON, a6: C_REG, type_: 23, size: 12},
141         {as: AANDCC, a1: C_LCON, a2: C_REG, a6: C_REG, type_: 23, size: 12},
142         {as: AANDISCC, a1: C_ANDCON, a6: C_REG, type_: 59, size: 4},
143         {as: AANDISCC, a1: C_ANDCON, a2: C_REG, a6: C_REG, type_: 59, size: 4},
144         {as: AMULLW, a1: C_REG, a2: C_REG, a6: C_REG, type_: 2, size: 4},
145         {as: AMULLW, a1: C_REG, a6: C_REG, type_: 2, size: 4},
146         {as: AMULLW, a1: C_ADDCON, a2: C_REG, a6: C_REG, type_: 4, size: 4},
147         {as: AMULLW, a1: C_ADDCON, a6: C_REG, type_: 4, size: 4},
148         {as: AMULLW, a1: C_ANDCON, a2: C_REG, a6: C_REG, type_: 4, size: 4},
149         {as: AMULLW, a1: C_ANDCON, a6: C_REG, type_: 4, size: 4},
150         {as: AMULLW, a1: C_LCON, a2: C_REG, a6: C_REG, type_: 22, size: 12},
151         {as: AMULLW, a1: C_LCON, a6: C_REG, type_: 22, size: 12},
152         {as: ASUBC, a1: C_REG, a2: C_REG, a6: C_REG, type_: 10, size: 4},
153         {as: ASUBC, a1: C_REG, a6: C_REG, type_: 10, size: 4},
154         {as: ASUBC, a1: C_REG, a3: C_ADDCON, a6: C_REG, type_: 27, size: 4},
155         {as: ASUBC, a1: C_REG, a3: C_LCON, a6: C_REG, type_: 28, size: 12},
156         {as: AOR, a1: C_REG, a2: C_REG, a6: C_REG, type_: 6, size: 4}, /* logical, literal not cc (or/xor) */
157         {as: AOR, a1: C_REG, a6: C_REG, type_: 6, size: 4},
158         {as: AOR, a1: C_ANDCON, a6: C_REG, type_: 58, size: 4},
159         {as: AOR, a1: C_ANDCON, a2: C_REG, a6: C_REG, type_: 58, size: 4},
160         {as: AOR, a1: C_UCON, a6: C_REG, type_: 59, size: 4},
161         {as: AOR, a1: C_UCON, a2: C_REG, a6: C_REG, type_: 59, size: 4},
162         {as: AOR, a1: C_ADDCON, a6: C_REG, type_: 23, size: 8},
163         {as: AOR, a1: C_ADDCON, a2: C_REG, a6: C_REG, type_: 23, size: 8},
164         {as: AOR, a1: C_LCON, a6: C_REG, type_: 23, size: 12},
165         {as: AOR, a1: C_LCON, a2: C_REG, a6: C_REG, type_: 23, size: 12},
166         {as: AORIS, a1: C_ANDCON, a6: C_REG, type_: 59, size: 4},
167         {as: AORIS, a1: C_ANDCON, a2: C_REG, a6: C_REG, type_: 59, size: 4},
168         {as: ADIVW, a1: C_REG, a2: C_REG, a6: C_REG, type_: 2, size: 4}, /* op r1[,r2],r3 */
169         {as: ADIVW, a1: C_REG, a6: C_REG, type_: 2, size: 4},
170         {as: ASUB, a1: C_REG, a2: C_REG, a6: C_REG, type_: 10, size: 4}, /* op r2[,r1],r3 */
171         {as: ASUB, a1: C_REG, a6: C_REG, type_: 10, size: 4},
172         {as: ASLW, a1: C_REG, a6: C_REG, type_: 6, size: 4},
173         {as: ASLW, a1: C_REG, a2: C_REG, a6: C_REG, type_: 6, size: 4},
174         {as: ASLD, a1: C_REG, a6: C_REG, type_: 6, size: 4},
175         {as: ASLD, a1: C_REG, a2: C_REG, a6: C_REG, type_: 6, size: 4},
176         {as: ASLD, a1: C_SCON, a2: C_REG, a6: C_REG, type_: 25, size: 4},
177         {as: ASLD, a1: C_SCON, a6: C_REG, type_: 25, size: 4},
178         {as: AEXTSWSLI, a1: C_SCON, a6: C_REG, type_: 25, size: 4},
179         {as: AEXTSWSLI, a1: C_SCON, a2: C_REG, a6: C_REG, type_: 25, size: 4},
180         {as: ASLW, a1: C_SCON, a2: C_REG, a6: C_REG, type_: 57, size: 4},
181         {as: ASLW, a1: C_SCON, a6: C_REG, type_: 57, size: 4},
182         {as: ASRAW, a1: C_REG, a6: C_REG, type_: 6, size: 4},
183         {as: ASRAW, a1: C_REG, a2: C_REG, a6: C_REG, type_: 6, size: 4},
184         {as: ASRAW, a1: C_SCON, a2: C_REG, a6: C_REG, type_: 56, size: 4},
185         {as: ASRAW, a1: C_SCON, a6: C_REG, type_: 56, size: 4},
186         {as: ASRAD, a1: C_REG, a6: C_REG, type_: 6, size: 4},
187         {as: ASRAD, a1: C_REG, a2: C_REG, a6: C_REG, type_: 6, size: 4},
188         {as: ASRAD, a1: C_SCON, a2: C_REG, a6: C_REG, type_: 56, size: 4},
189         {as: ASRAD, a1: C_SCON, a6: C_REG, type_: 56, size: 4},
190         {as: ARLWNM, a1: C_SCON, a2: C_REG, a3: C_LCON, a6: C_REG, type_: 63, size: 4},
191         {as: ARLWNM, a1: C_SCON, a2: C_REG, a3: C_SCON, a4: C_SCON, a6: C_REG, type_: 63, size: 4},
192         {as: ARLWNM, a1: C_REG, a2: C_REG, a3: C_LCON, a6: C_REG, type_: 63, size: 4},
193         {as: ARLWNM, a1: C_REG, a2: C_REG, a3: C_SCON, a4: C_SCON, a6: C_REG, type_: 63, size: 4},
194         {as: ACLRLSLWI, a1: C_SCON, a2: C_REG, a3: C_LCON, a6: C_REG, type_: 62, size: 4},
195         {as: ARLDMI, a1: C_SCON, a2: C_REG, a3: C_LCON, a6: C_REG, type_: 30, size: 4},
196         {as: ARLDC, a1: C_SCON, a2: C_REG, a3: C_LCON, a6: C_REG, type_: 29, size: 4},
197         {as: ARLDCL, a1: C_SCON, a2: C_REG, a3: C_LCON, a6: C_REG, type_: 29, size: 4},
198         {as: ARLDCL, a1: C_REG, a2: C_REG, a3: C_LCON, a6: C_REG, type_: 14, size: 4},
199         {as: ARLDICL, a1: C_REG, a2: C_REG, a3: C_LCON, a6: C_REG, type_: 14, size: 4},
200         {as: ARLDICL, a1: C_SCON, a2: C_REG, a3: C_LCON, a6: C_REG, type_: 14, size: 4},
201         {as: ARLDCL, a1: C_REG, a3: C_LCON, a6: C_REG, type_: 14, size: 4},
202         {as: AFADD, a1: C_FREG, a6: C_FREG, type_: 2, size: 4},
203         {as: AFADD, a1: C_FREG, a2: C_FREG, a6: C_FREG, type_: 2, size: 4},
204         {as: AFABS, a1: C_FREG, a6: C_FREG, type_: 33, size: 4},
205         {as: AFABS, a6: C_FREG, type_: 33, size: 4},
206         {as: AFMADD, a1: C_FREG, a2: C_FREG, a3: C_FREG, a6: C_FREG, type_: 34, size: 4},
207         {as: AFMUL, a1: C_FREG, a6: C_FREG, type_: 32, size: 4},
208         {as: AFMUL, a1: C_FREG, a2: C_FREG, a6: C_FREG, type_: 32, size: 4},
209
210         {as: AMOVBU, a1: C_REG, a6: C_SOREG, type_: 7, size: 4},
211         {as: AMOVBU, a1: C_REG, a6: C_XOREG, type_: 108, size: 4},
212         {as: AMOVBU, a1: C_SOREG, a6: C_REG, type_: 8, size: 8},
213         {as: AMOVBU, a1: C_XOREG, a6: C_REG, type_: 109, size: 8},
214
215         {as: AMOVBZU, a1: C_REG, a6: C_SOREG, type_: 7, size: 4},
216         {as: AMOVBZU, a1: C_REG, a6: C_XOREG, type_: 108, size: 4},
217         {as: AMOVBZU, a1: C_SOREG, a6: C_REG, type_: 8, size: 4},
218         {as: AMOVBZU, a1: C_XOREG, a6: C_REG, type_: 109, size: 4},
219
220         {as: AMOVHBR, a1: C_REG, a6: C_XOREG, type_: 44, size: 4},
221         {as: AMOVHBR, a1: C_XOREG, a6: C_REG, type_: 45, size: 4},
222
223         {as: AMOVB, a1: C_SOREG, a6: C_REG, type_: 8, size: 8},
224         {as: AMOVB, a1: C_XOREG, a6: C_REG, type_: 109, size: 8},
225         {as: AMOVB, a1: C_REG, a6: C_SOREG, type_: 7, size: 4},
226         {as: AMOVB, a1: C_REG, a6: C_XOREG, type_: 108, size: 4},
227         {as: AMOVB, a1: C_REG, a6: C_REG, type_: 13, size: 4},
228
229         {as: AMOVBZ, a1: C_SOREG, a6: C_REG, type_: 8, size: 4},
230         {as: AMOVBZ, a1: C_XOREG, a6: C_REG, type_: 109, size: 4},
231         {as: AMOVBZ, a1: C_REG, a6: C_SOREG, type_: 7, size: 4},
232         {as: AMOVBZ, a1: C_REG, a6: C_XOREG, type_: 108, size: 4},
233         {as: AMOVBZ, a1: C_REG, a6: C_REG, type_: 13, size: 4},
234
235         {as: AMOVD, a1: C_ADDCON, a6: C_REG, type_: 3, size: 4},
236         {as: AMOVD, a1: C_ANDCON, a6: C_REG, type_: 3, size: 4},
237         {as: AMOVD, a1: C_UCON, a6: C_REG, type_: 3, size: 4},
238         {as: AMOVD, a1: C_SACON, a6: C_REG, type_: 3, size: 4},
239         {as: AMOVD, a1: C_SOREG, a6: C_REG, type_: 8, size: 4},
240         {as: AMOVD, a1: C_XOREG, a6: C_REG, type_: 109, size: 4},
241         {as: AMOVD, a1: C_SOREG, a6: C_SPR, type_: 107, size: 8},
242         {as: AMOVD, a1: C_SPR, a6: C_REG, type_: 66, size: 4},
243         {as: AMOVD, a1: C_REG, a6: C_SOREG, type_: 7, size: 4},
244         {as: AMOVD, a1: C_REG, a6: C_XOREG, type_: 108, size: 4},
245         {as: AMOVD, a1: C_SPR, a6: C_SOREG, type_: 106, size: 8},
246         {as: AMOVD, a1: C_REG, a6: C_SPR, type_: 66, size: 4},
247         {as: AMOVD, a1: C_REG, a6: C_REG, type_: 13, size: 4},
248
249         {as: AMOVW, a1: C_ADDCON, a6: C_REG, type_: 3, size: 4},
250         {as: AMOVW, a1: C_ANDCON, a6: C_REG, type_: 3, size: 4},
251         {as: AMOVW, a1: C_UCON, a6: C_REG, type_: 3, size: 4},
252         {as: AMOVW, a1: C_SACON, a6: C_REG, type_: 3, size: 4},
253         {as: AMOVW, a1: C_CREG, a6: C_REG, type_: 68, size: 4},
254         {as: AMOVW, a1: C_SOREG, a6: C_REG, type_: 8, size: 4},
255         {as: AMOVW, a1: C_XOREG, a6: C_REG, type_: 109, size: 4},
256         {as: AMOVW, a1: C_SPR, a6: C_REG, type_: 66, size: 4},
257         {as: AMOVW, a1: C_REG, a6: C_CREG, type_: 69, size: 4},
258         {as: AMOVW, a1: C_REG, a6: C_SOREG, type_: 7, size: 4},
259         {as: AMOVW, a1: C_REG, a6: C_XOREG, type_: 108, size: 4},
260         {as: AMOVW, a1: C_REG, a6: C_SPR, type_: 66, size: 4},
261         {as: AMOVW, a1: C_REG, a6: C_REG, type_: 13, size: 4},
262
263         {as: AFMOVD, a1: C_ADDCON, a6: C_FREG, type_: 24, size: 8},
264         {as: AFMOVD, a1: C_SOREG, a6: C_FREG, type_: 8, size: 4},
265         {as: AFMOVD, a1: C_XOREG, a6: C_FREG, type_: 109, size: 4},
266         {as: AFMOVD, a1: C_ZCON, a6: C_FREG, type_: 24, size: 4},
267         {as: AFMOVD, a1: C_FREG, a6: C_FREG, type_: 33, size: 4},
268         {as: AFMOVD, a1: C_FREG, a6: C_SOREG, type_: 7, size: 4},
269         {as: AFMOVD, a1: C_FREG, a6: C_XOREG, type_: 108, size: 4},
270
271         {as: AFMOVSX, a1: C_XOREG, a6: C_FREG, type_: 45, size: 4},
272         {as: AFMOVSX, a1: C_FREG, a6: C_XOREG, type_: 44, size: 4},
273
274         {as: AFMOVSZ, a1: C_ZOREG, a6: C_FREG, type_: 45, size: 4},
275         {as: AFMOVSZ, a1: C_XOREG, a6: C_FREG, type_: 45, size: 4},
276
277         {as: AMOVFL, a1: C_CREG, a6: C_CREG, type_: 67, size: 4},
278         {as: AMOVFL, a1: C_FPSCR, a6: C_CREG, type_: 73, size: 4},
279         {as: AMOVFL, a1: C_FPSCR, a6: C_FREG, type_: 53, size: 4},
280         {as: AMOVFL, a1: C_FREG, a3: C_LCON, a6: C_FPSCR, type_: 64, size: 4},
281         {as: AMOVFL, a1: C_FREG, a6: C_FPSCR, type_: 64, size: 4},
282         {as: AMOVFL, a1: C_LCON, a6: C_FPSCR, type_: 65, size: 4},
283         {as: AMOVFL, a1: C_REG, a6: C_CREG, type_: 69, size: 4},
284         {as: AMOVFL, a1: C_REG, a6: C_LCON, type_: 69, size: 4},
285
286         {as: ASYSCALL, type_: 5, size: 4},
287         {as: ASYSCALL, a1: C_REG, type_: 77, size: 12},
288         {as: ASYSCALL, a1: C_SCON, type_: 77, size: 12},
289         {as: ABEQ, a6: C_SBRA, type_: 16, size: 4},
290         {as: ABEQ, a1: C_CREG, a6: C_SBRA, type_: 16, size: 4},
291         {as: ABR, a6: C_LBRA, type_: 11, size: 4},                                    // b label
292         {as: ABR, a6: C_LBRAPIC, type_: 11, size: 8},                                 // b label; nop
293         {as: ABR, a6: C_LR, type_: 18, size: 4},                                      // blr
294         {as: ABR, a6: C_CTR, type_: 18, size: 4},                                     // bctr
295         {as: ABC, a1: C_SCON, a2: C_CRBIT, a6: C_SBRA, type_: 16, size: 4},           // bc bo, bi, label
296         {as: ABC, a1: C_SCON, a2: C_CRBIT, a6: C_LBRA, type_: 17, size: 4},           // bc bo, bi, label
297         {as: ABC, a1: C_SCON, a2: C_CRBIT, a6: C_LR, type_: 18, size: 4},             // bclr bo, bi
298         {as: ABC, a1: C_SCON, a2: C_CRBIT, a3: C_SCON, a6: C_LR, type_: 18, size: 4}, // bclr bo, bi, bh
299         {as: ABC, a1: C_SCON, a2: C_CRBIT, a6: C_CTR, type_: 18, size: 4},            // bcctr bo, bi
300         {as: ABDNZ, a6: C_SBRA, type_: 16, size: 4},
301         {as: ASYNC, type_: 46, size: 4},
302         {as: AWORD, a1: C_LCON, type_: 40, size: 4},
303         {as: ADWORD, a1: C_64CON, type_: 31, size: 8},
304         {as: ADWORD, a1: C_LACON, type_: 31, size: 8},
305         {as: AADDME, a1: C_REG, a6: C_REG, type_: 47, size: 4},
306         {as: AEXTSB, a1: C_REG, a6: C_REG, type_: 48, size: 4},
307         {as: AEXTSB, a6: C_REG, type_: 48, size: 4},
308         {as: AISEL, a1: C_U5CON, a2: C_REG, a3: C_REG, a6: C_REG, type_: 84, size: 4},
309         {as: AISEL, a1: C_CRBIT, a2: C_REG, a3: C_REG, a6: C_REG, type_: 84, size: 4},
310         {as: ANEG, a1: C_REG, a6: C_REG, type_: 47, size: 4},
311         {as: ANEG, a6: C_REG, type_: 47, size: 4},
312         {as: AREM, a1: C_REG, a6: C_REG, type_: 50, size: 12},
313         {as: AREM, a1: C_REG, a2: C_REG, a6: C_REG, type_: 50, size: 12},
314         {as: AREMU, a1: C_REG, a6: C_REG, type_: 50, size: 16},
315         {as: AREMU, a1: C_REG, a2: C_REG, a6: C_REG, type_: 50, size: 16},
316         {as: AREMD, a1: C_REG, a6: C_REG, type_: 51, size: 12},
317         {as: AREMD, a1: C_REG, a2: C_REG, a6: C_REG, type_: 51, size: 12},
318         {as: AMTFSB0, a1: C_SCON, type_: 52, size: 4},
319         /* Other ISA 2.05+ instructions */
320         {as: APOPCNTD, a1: C_REG, a6: C_REG, type_: 93, size: 4},            /* population count, x-form */
321         {as: ACMPB, a1: C_REG, a2: C_REG, a6: C_REG, type_: 92, size: 4},    /* compare byte, x-form */
322         {as: ACMPEQB, a1: C_REG, a2: C_REG, a6: C_CREG, type_: 92, size: 4}, /* compare equal byte, x-form, ISA 3.0 */
323         {as: ACMPEQB, a1: C_REG, a6: C_REG, type_: 70, size: 4},
324         {as: AFTDIV, a1: C_FREG, a2: C_FREG, a6: C_SCON, type_: 92, size: 4},          /* floating test for sw divide, x-form */
325         {as: AFTSQRT, a1: C_FREG, a6: C_SCON, type_: 93, size: 4},                     /* floating test for sw square root, x-form */
326         {as: ACOPY, a1: C_REG, a6: C_REG, type_: 92, size: 4},                         /* copy/paste facility, x-form */
327         {as: ADARN, a1: C_SCON, a6: C_REG, type_: 92, size: 4},                        /* deliver random number, x-form */
328         {as: AMADDHD, a1: C_REG, a2: C_REG, a3: C_REG, a6: C_REG, type_: 83, size: 4}, /* multiply-add high/low doubleword, va-form */
329         {as: AADDEX, a1: C_REG, a2: C_REG, a3: C_SCON, a6: C_REG, type_: 94, size: 4}, /* add extended using alternate carry, z23-form */
330         {as: ACRAND, a1: C_CRBIT, a2: C_CRBIT, a6: C_CRBIT, type_: 2, size: 4},        /* logical ops for condition register bits xl-form */
331
332         /* Misc ISA 3.0 instructions */
333         {as: ASETB, a1: C_CREG, a6: C_REG, type_: 110, size: 4},
334         {as: AVCLZLSBB, a1: C_VREG, a6: C_REG, type_: 85, size: 4},
335
336         /* Vector instructions */
337
338         /* Vector load */
339         {as: ALVEBX, a1: C_XOREG, a6: C_VREG, type_: 45, size: 4}, /* vector load, x-form */
340
341         /* Vector store */
342         {as: ASTVEBX, a1: C_VREG, a6: C_XOREG, type_: 44, size: 4}, /* vector store, x-form */
343
344         /* Vector logical */
345         {as: AVAND, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4}, /* vector and, vx-form */
346         {as: AVOR, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},  /* vector or, vx-form */
347
348         /* Vector add */
349         {as: AVADDUM, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},            /* vector add unsigned modulo, vx-form */
350         {as: AVADDCU, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},            /* vector add & write carry unsigned, vx-form */
351         {as: AVADDUS, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},            /* vector add unsigned saturate, vx-form */
352         {as: AVADDSS, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},            /* vector add signed saturate, vx-form */
353         {as: AVADDE, a1: C_VREG, a2: C_VREG, a3: C_VREG, a6: C_VREG, type_: 83, size: 4}, /* vector add extended, va-form */
354
355         /* Vector subtract */
356         {as: AVSUBUM, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},            /* vector subtract unsigned modulo, vx-form */
357         {as: AVSUBCU, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},            /* vector subtract & write carry unsigned, vx-form */
358         {as: AVSUBUS, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},            /* vector subtract unsigned saturate, vx-form */
359         {as: AVSUBSS, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},            /* vector subtract signed saturate, vx-form */
360         {as: AVSUBE, a1: C_VREG, a2: C_VREG, a3: C_VREG, a6: C_VREG, type_: 83, size: 4}, /* vector subtract extended, va-form */
361
362         /* Vector multiply */
363         {as: AVMULESB, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},              /* vector multiply, vx-form */
364         {as: AVPMSUM, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},               /* vector polynomial multiply & sum, vx-form */
365         {as: AVMSUMUDM, a1: C_VREG, a2: C_VREG, a3: C_VREG, a6: C_VREG, type_: 83, size: 4}, /* vector multiply-sum, va-form */
366
367         /* Vector rotate */
368         {as: AVR, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4}, /* vector rotate, vx-form */
369
370         /* Vector shift */
371         {as: AVS, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},                 /* vector shift, vx-form */
372         {as: AVSA, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},                /* vector shift algebraic, vx-form */
373         {as: AVSOI, a1: C_ANDCON, a2: C_VREG, a3: C_VREG, a6: C_VREG, type_: 83, size: 4}, /* vector shift by octet immediate, va-form */
374
375         /* Vector count */
376         {as: AVCLZ, a1: C_VREG, a6: C_VREG, type_: 85, size: 4},    /* vector count leading zeros, vx-form */
377         {as: AVPOPCNT, a1: C_VREG, a6: C_VREG, type_: 85, size: 4}, /* vector population count, vx-form */
378
379         /* Vector compare */
380         {as: AVCMPEQ, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},   /* vector compare equal, vc-form */
381         {as: AVCMPGT, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},   /* vector compare greater than, vc-form */
382         {as: AVCMPNEZB, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4}, /* vector compare not equal, vx-form */
383
384         /* Vector merge */
385         {as: AVMRGOW, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4}, /* vector merge odd word, vx-form */
386
387         /* Vector permute */
388         {as: AVPERM, a1: C_VREG, a2: C_VREG, a3: C_VREG, a6: C_VREG, type_: 83, size: 4}, /* vector permute, va-form */
389
390         /* Vector bit permute */
391         {as: AVBPERMQ, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4}, /* vector bit permute, vx-form */
392
393         /* Vector select */
394         {as: AVSEL, a1: C_VREG, a2: C_VREG, a3: C_VREG, a6: C_VREG, type_: 83, size: 4}, /* vector select, va-form */
395
396         /* Vector splat */
397         {as: AVSPLTB, a1: C_SCON, a2: C_VREG, a6: C_VREG, type_: 82, size: 4}, /* vector splat, vx-form */
398         {as: AVSPLTB, a1: C_ADDCON, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},
399         {as: AVSPLTISB, a1: C_SCON, a6: C_VREG, type_: 82, size: 4}, /* vector splat immediate, vx-form */
400         {as: AVSPLTISB, a1: C_ADDCON, a6: C_VREG, type_: 82, size: 4},
401
402         /* Vector AES */
403         {as: AVCIPH, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4},  /* vector AES cipher, vx-form */
404         {as: AVNCIPH, a1: C_VREG, a2: C_VREG, a6: C_VREG, type_: 82, size: 4}, /* vector AES inverse cipher, vx-form */
405         {as: AVSBOX, a1: C_VREG, a6: C_VREG, type_: 82, size: 4},              /* vector AES subbytes, vx-form */
406
407         /* Vector SHA */
408         {as: AVSHASIGMA, a1: C_ANDCON, a2: C_VREG, a3: C_ANDCON, a6: C_VREG, type_: 82, size: 4}, /* vector SHA sigma, vx-form */
409
410         /* VSX vector load */
411         {as: ALXVD2X, a1: C_XOREG, a6: C_VSREG, type_: 87, size: 4},        /* vsx vector load, xx1-form */
412         {as: ALXV, a1: C_SOREG, a6: C_VSREG, type_: 96, size: 4},           /* vsx vector load, dq-form */
413         {as: ALXVL, a1: C_REG, a2: C_REG, a6: C_VSREG, type_: 98, size: 4}, /* vsx vector load length */
414
415         /* VSX vector store */
416         {as: ASTXVD2X, a1: C_VSREG, a6: C_XOREG, type_: 86, size: 4},        /* vsx vector store, xx1-form */
417         {as: ASTXV, a1: C_VSREG, a6: C_SOREG, type_: 97, size: 4},           /* vsx vector store, dq-form */
418         {as: ASTXVL, a1: C_VSREG, a2: C_REG, a6: C_REG, type_: 99, size: 4}, /* vsx vector store with length x-form */
419
420         /* VSX scalar load */
421         {as: ALXSDX, a1: C_XOREG, a6: C_VSREG, type_: 87, size: 4}, /* vsx scalar load, xx1-form */
422
423         /* VSX scalar store */
424         {as: ASTXSDX, a1: C_VSREG, a6: C_XOREG, type_: 86, size: 4}, /* vsx scalar store, xx1-form */
425
426         /* VSX scalar as integer load */
427         {as: ALXSIWAX, a1: C_XOREG, a6: C_VSREG, type_: 87, size: 4}, /* vsx scalar as integer load, xx1-form */
428
429         /* VSX scalar store as integer */
430         {as: ASTXSIWX, a1: C_VSREG, a6: C_XOREG, type_: 86, size: 4}, /* vsx scalar as integer store, xx1-form */
431
432         /* VSX move from VSR */
433         {as: AMFVSRD, a1: C_VSREG, a6: C_REG, type_: 88, size: 4},
434         {as: AMFVSRD, a1: C_FREG, a6: C_REG, type_: 88, size: 4},
435
436         /* VSX move to VSR */
437         {as: AMTVSRD, a1: C_REG, a6: C_VSREG, type_: 104, size: 4},
438         {as: AMTVSRD, a1: C_REG, a6: C_FREG, type_: 104, size: 4},
439         {as: AMTVSRDD, a1: C_REG, a2: C_REG, a6: C_VSREG, type_: 104, size: 4},
440
441         /* VSX logical */
442         {as: AXXLAND, a1: C_VSREG, a2: C_VSREG, a6: C_VSREG, type_: 90, size: 4}, /* vsx and, xx3-form */
443         {as: AXXLOR, a1: C_VSREG, a2: C_VSREG, a6: C_VSREG, type_: 90, size: 4},  /* vsx or, xx3-form */
444
445         /* VSX select */
446         {as: AXXSEL, a1: C_VSREG, a2: C_VSREG, a3: C_VSREG, a6: C_VSREG, type_: 91, size: 4}, /* vsx select, xx4-form */
447
448         /* VSX merge */
449         {as: AXXMRGHW, a1: C_VSREG, a2: C_VSREG, a6: C_VSREG, type_: 90, size: 4}, /* vsx merge, xx3-form */
450
451         /* VSX splat */
452         {as: AXXSPLTW, a1: C_VSREG, a3: C_SCON, a6: C_VSREG, type_: 89, size: 4}, /* vsx splat, xx2-form */
453         {as: AXXSPLTIB, a1: C_SCON, a6: C_VSREG, type_: 100, size: 4},            /* vsx splat, xx2-form */
454
455         /* VSX permute */
456         {as: AXXPERM, a1: C_VSREG, a2: C_VSREG, a6: C_VSREG, type_: 90, size: 4}, /* vsx permute, xx3-form */
457
458         /* VSX shift */
459         {as: AXXSLDWI, a1: C_VSREG, a2: C_VSREG, a3: C_SCON, a6: C_VSREG, type_: 90, size: 4}, /* vsx shift immediate, xx3-form */
460
461         /* VSX reverse bytes */
462         {as: AXXBRQ, a1: C_VSREG, a6: C_VSREG, type_: 101, size: 4}, /* vsx reverse bytes */
463
464         /* VSX scalar FP-FP conversion */
465         {as: AXSCVDPSP, a1: C_VSREG, a6: C_VSREG, type_: 89, size: 4}, /* vsx scalar fp-fp conversion, xx2-form */
466
467         /* VSX vector FP-FP conversion */
468         {as: AXVCVDPSP, a1: C_VSREG, a6: C_VSREG, type_: 89, size: 4}, /* vsx vector fp-fp conversion, xx2-form */
469
470         /* VSX scalar FP-integer conversion */
471         {as: AXSCVDPSXDS, a1: C_VSREG, a6: C_VSREG, type_: 89, size: 4}, /* vsx scalar fp-integer conversion, xx2-form */
472
473         /* VSX scalar integer-FP conversion */
474         {as: AXSCVSXDDP, a1: C_VSREG, a6: C_VSREG, type_: 89, size: 4}, /* vsx scalar integer-fp conversion, xx2-form */
475
476         /* VSX vector FP-integer conversion */
477         {as: AXVCVDPSXDS, a1: C_VSREG, a6: C_VSREG, type_: 89, size: 4}, /* vsx vector fp-integer conversion, xx2-form */
478
479         /* VSX vector integer-FP conversion */
480         {as: AXVCVSXDDP, a1: C_VSREG, a6: C_VSREG, type_: 89, size: 4}, /* vsx vector integer-fp conversion, xx2-form */
481
482         {as: ACMP, a1: C_REG, a6: C_REG, type_: 70, size: 4},
483         {as: ACMP, a1: C_REG, a2: C_CREG, a6: C_REG, type_: 70, size: 4},
484         {as: ACMP, a1: C_REG, a6: C_ADDCON, type_: 71, size: 4},
485         {as: ACMP, a1: C_REG, a2: C_CREG, a6: C_ADDCON, type_: 71, size: 4},
486         {as: ACMPU, a1: C_REG, a6: C_REG, type_: 70, size: 4},
487         {as: ACMPU, a1: C_REG, a2: C_CREG, a6: C_REG, type_: 70, size: 4},
488         {as: ACMPU, a1: C_REG, a6: C_ANDCON, type_: 71, size: 4},
489         {as: ACMPU, a1: C_REG, a2: C_CREG, a6: C_ANDCON, type_: 71, size: 4},
490         {as: AFCMPO, a1: C_FREG, a6: C_FREG, type_: 70, size: 4},
491         {as: AFCMPO, a1: C_FREG, a2: C_CREG, a6: C_FREG, type_: 70, size: 4},
492         {as: ATW, a1: C_LCON, a2: C_REG, a6: C_REG, type_: 60, size: 4},
493         {as: ATW, a1: C_LCON, a2: C_REG, a6: C_ADDCON, type_: 61, size: 4},
494         {as: ADCBF, a1: C_SOREG, type_: 43, size: 4},
495         {as: ADCBF, a1: C_XOREG, type_: 43, size: 4},
496         {as: ADCBF, a1: C_XOREG, a2: C_REG, a6: C_SCON, type_: 43, size: 4},
497         {as: ADCBF, a1: C_SOREG, a6: C_SCON, type_: 43, size: 4},
498         {as: ADCBF, a1: C_XOREG, a6: C_SCON, type_: 43, size: 4},
499         {as: ASTDCCC, a1: C_REG, a2: C_REG, a6: C_XOREG, type_: 44, size: 4},
500         {as: ASTDCCC, a1: C_REG, a6: C_XOREG, type_: 44, size: 4},
501         {as: ALDAR, a1: C_XOREG, a6: C_REG, type_: 45, size: 4},
502         {as: ALDAR, a1: C_XOREG, a3: C_ANDCON, a6: C_REG, type_: 45, size: 4},
503         {as: AEIEIO, type_: 46, size: 4},
504         {as: ATLBIE, a1: C_REG, type_: 49, size: 4},
505         {as: ATLBIE, a1: C_SCON, a6: C_REG, type_: 49, size: 4},
506         {as: ASLBMFEE, a1: C_REG, a6: C_REG, type_: 55, size: 4},
507         {as: ASLBMTE, a1: C_REG, a6: C_REG, type_: 55, size: 4},
508         {as: ASTSW, a1: C_REG, a6: C_XOREG, type_: 44, size: 4},
509         {as: ASTSW, a1: C_REG, a3: C_LCON, a6: C_ZOREG, type_: 41, size: 4},
510         {as: ALSW, a1: C_XOREG, a6: C_REG, type_: 45, size: 4},
511         {as: ALSW, a1: C_ZOREG, a3: C_LCON, a6: C_REG, type_: 42, size: 4},
512
513         {as: obj.AUNDEF, type_: 78, size: 4},
514         {as: obj.APCDATA, a1: C_LCON, a6: C_LCON, type_: 0, size: 0},
515         {as: obj.AFUNCDATA, a1: C_SCON, a6: C_ADDR, type_: 0, size: 0},
516         {as: obj.ANOP, type_: 0, size: 0},
517         {as: obj.ANOP, a1: C_LCON, type_: 0, size: 0}, // NOP operand variations added for #40689
518         {as: obj.ANOP, a1: C_REG, type_: 0, size: 0},  // to preserve previous behavior
519         {as: obj.ANOP, a1: C_FREG, type_: 0, size: 0},
520         {as: obj.ADUFFZERO, a6: C_LBRA, type_: 11, size: 4}, // same as ABR/ABL
521         {as: obj.ADUFFCOPY, a6: C_LBRA, type_: 11, size: 4}, // same as ABR/ABL
522         {as: obj.APCALIGN, a1: C_LCON, type_: 0, size: 0},   // align code
523 }
524
525 // These are opcodes above which may generate different sequences depending on whether prefix opcode support
526 // is available
527 type PrefixableOptab struct {
528         Optab
529         minGOPPC64 int  // Minimum GOPPC64 required to support this.
530         pfxsize    int8 // Instruction sequence size when prefixed opcodes are used
531 }
532
533 // The prefixable optab entry contains the pseudo-opcodes which generate relocations, or may generate
534 // a more efficient sequence of instructions if a prefixed version exists (ex. paddi instead of oris/ori/add).
535 //
536 // This table is meant to transform all sequences which might be TOC-relative into an equivalent PC-relative
537 // sequence. It also encompasses several transformations which do not involve relocations, those could be
538 // separated and applied to AIX and other non-ELF targets. Likewise, the prefixed forms do not have encoding
539 // restrictions on the offset, so they are also used for static binary to allow better code generation. e.x
540 //
541 //      MOVD something-byte-aligned(Rx), Ry
542 //      MOVD 3(Rx), Ry
543 //
544 // is allowed when the prefixed forms are used.
545 //
546 // This requires an ISA 3.1 compatible cpu (e.g Power10), and when linking externally an ELFv2 1.5 compliant.
547 var prefixableOptab = []PrefixableOptab{
548         {Optab: Optab{as: AMOVD, a1: C_S34CON, a6: C_REG, type_: 19, size: 8}, minGOPPC64: 10, pfxsize: 8},
549         {Optab: Optab{as: AMOVD, a1: C_ADDR, a6: C_REG, type_: 75, size: 8}, minGOPPC64: 10, pfxsize: 8},
550         {Optab: Optab{as: AMOVD, a1: C_TLS_LE, a6: C_REG, type_: 79, size: 8}, minGOPPC64: 10, pfxsize: 8},
551         {Optab: Optab{as: AMOVD, a1: C_TLS_IE, a6: C_REG, type_: 80, size: 12}, minGOPPC64: 10, pfxsize: 12},
552         {Optab: Optab{as: AMOVD, a1: C_LACON, a6: C_REG, type_: 26, size: 8}, minGOPPC64: 10, pfxsize: 8},
553         {Optab: Optab{as: AMOVD, a1: C_LOREG, a6: C_REG, type_: 36, size: 8}, minGOPPC64: 10, pfxsize: 8},
554         {Optab: Optab{as: AMOVD, a1: C_REG, a6: C_LOREG, type_: 35, size: 8}, minGOPPC64: 10, pfxsize: 8},
555         {Optab: Optab{as: AMOVD, a1: C_REG, a6: C_ADDR, type_: 74, size: 8}, minGOPPC64: 10, pfxsize: 8},
556
557         {Optab: Optab{as: AMOVW, a1: C_LCON, a6: C_REG, type_: 19, size: 8}, minGOPPC64: 10, pfxsize: 8},
558         {Optab: Optab{as: AMOVW, a1: C_LACON, a6: C_REG, type_: 26, size: 8}, minGOPPC64: 10, pfxsize: 8},
559         {Optab: Optab{as: AMOVW, a1: C_LOREG, a6: C_REG, type_: 36, size: 8}, minGOPPC64: 10, pfxsize: 8},
560         {Optab: Optab{as: AMOVW, a1: C_ADDR, a6: C_REG, type_: 75, size: 8}, minGOPPC64: 10, pfxsize: 8},
561         {Optab: Optab{as: AMOVW, a1: C_REG, a6: C_LOREG, type_: 35, size: 8}, minGOPPC64: 10, pfxsize: 8},
562         {Optab: Optab{as: AMOVW, a1: C_REG, a6: C_ADDR, type_: 74, size: 8}, minGOPPC64: 10, pfxsize: 8},
563
564         {Optab: Optab{as: AMOVB, a1: C_REG, a6: C_LOREG, type_: 35, size: 8}, minGOPPC64: 10, pfxsize: 8},
565         {Optab: Optab{as: AMOVB, a1: C_LOREG, a6: C_REG, type_: 36, size: 12}, minGOPPC64: 10, pfxsize: 12},
566         {Optab: Optab{as: AMOVB, a1: C_ADDR, a6: C_REG, type_: 75, size: 12}, minGOPPC64: 10, pfxsize: 12},
567         {Optab: Optab{as: AMOVB, a1: C_REG, a6: C_ADDR, type_: 74, size: 8}, minGOPPC64: 10, pfxsize: 8},
568
569         {Optab: Optab{as: AMOVBZ, a1: C_LOREG, a6: C_REG, type_: 36, size: 8}, minGOPPC64: 10, pfxsize: 8},
570         {Optab: Optab{as: AMOVBZ, a1: C_ADDR, a6: C_REG, type_: 75, size: 8}, minGOPPC64: 10, pfxsize: 8},
571         {Optab: Optab{as: AMOVBZ, a1: C_REG, a6: C_LOREG, type_: 35, size: 8}, minGOPPC64: 10, pfxsize: 8},
572         {Optab: Optab{as: AMOVBZ, a1: C_REG, a6: C_ADDR, type_: 74, size: 8}, minGOPPC64: 10, pfxsize: 8},
573
574         {Optab: Optab{as: AFMOVD, a1: C_LOREG, a6: C_FREG, type_: 36, size: 8}, minGOPPC64: 10, pfxsize: 8},
575         {Optab: Optab{as: AFMOVD, a1: C_ADDR, a6: C_FREG, type_: 75, size: 8}, minGOPPC64: 10, pfxsize: 8},
576         {Optab: Optab{as: AFMOVD, a1: C_FREG, a6: C_LOREG, type_: 35, size: 8}, minGOPPC64: 10, pfxsize: 8},
577         {Optab: Optab{as: AFMOVD, a1: C_FREG, a6: C_ADDR, type_: 74, size: 8}, minGOPPC64: 10, pfxsize: 8},
578
579         {Optab: Optab{as: AADD, a1: C_LCON, a2: C_REG, a6: C_REG, type_: 22, size: 12}, minGOPPC64: 10, pfxsize: 8},
580         {Optab: Optab{as: AADD, a1: C_LCON, a6: C_REG, type_: 22, size: 12}, minGOPPC64: 10, pfxsize: 8},
581         {Optab: Optab{as: AADD, a1: C_S34CON, a2: C_REG, a6: C_REG, type_: 22, size: 20}, minGOPPC64: 10, pfxsize: 8},
582         {Optab: Optab{as: AADD, a1: C_S34CON, a6: C_REG, type_: 22, size: 20}, minGOPPC64: 10, pfxsize: 8},
583 }
584
585 var oprange [ALAST & obj.AMask][]Optab
586
587 var xcmp [C_NCLASS][C_NCLASS]bool
588
589 var pfxEnabled = false // ISA 3.1 prefixed instructions are supported.
590 var buildOpCfg = ""    // Save the os/cpu/arch tuple used to configure the assembler in buildop
591
592 // padding bytes to add to align code as requested.
593 func addpad(pc, a int64, ctxt *obj.Link, cursym *obj.LSym) int {
594         switch a {
595         case 8, 16, 32, 64:
596                 // By default function alignment is 16. If an alignment > 16 is
597                 // requested then the function alignment must also be promoted.
598                 // The function alignment is not promoted on AIX at this time.
599                 // TODO: Investigate AIX function alignment.
600                 if ctxt.Headtype != objabi.Haix && cursym.Func().Align < int32(a) {
601                         cursym.Func().Align = int32(a)
602                 }
603                 if pc&(a-1) != 0 {
604                         return int(a - (pc & (a - 1)))
605                 }
606         default:
607                 ctxt.Diag("Unexpected alignment: %d for PCALIGN directive\n", a)
608         }
609         return 0
610 }
611
612 // Get the implied register of an operand which doesn't specify one.  These show up
613 // in handwritten asm like "MOVD R5, foosymbol" where a base register is not supplied,
614 // or "MOVD R5, foo+10(SP) or pseudo-register is used.  The other common case is when
615 // generating constants in register like "MOVD $constant, Rx".
616 func (c *ctxt9) getimpliedreg(a *obj.Addr, p *obj.Prog) int {
617         class := oclass(a)
618         if class >= C_ZCON && class <= C_64CON {
619                 return REGZERO
620         }
621         switch class {
622         case C_SACON, C_LACON:
623                 return REGSP
624         case C_LOREG, C_SOREG, C_ZOREG, C_XOREG:
625                 switch a.Name {
626                 case obj.NAME_EXTERN, obj.NAME_STATIC:
627                         return REGSB
628                 case obj.NAME_AUTO, obj.NAME_PARAM:
629                         return REGSP
630                 case obj.NAME_NONE:
631                         return REGZERO
632                 }
633         }
634         c.ctxt.Diag("failed to determine implied reg for class %v (%v)", DRconv(oclass(a)), p)
635         return 0
636 }
637
638 func span9(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
639         p := cursym.Func().Text
640         if p == nil || p.Link == nil { // handle external functions and ELF section symbols
641                 return
642         }
643
644         if oprange[AANDN&obj.AMask] == nil {
645                 ctxt.Diag("ppc64 ops not initialized, call ppc64.buildop first")
646         }
647
648         c := ctxt9{ctxt: ctxt, newprog: newprog, cursym: cursym, autosize: int32(p.To.Offset)}
649
650         pc := int64(0)
651         p.Pc = pc
652
653         var m int
654         var o *Optab
655         for p = p.Link; p != nil; p = p.Link {
656                 p.Pc = pc
657                 o = c.oplook(p)
658                 m = int(o.size)
659                 if m == 0 {
660                         if p.As == obj.APCALIGN {
661                                 a := c.vregoff(&p.From)
662                                 m = addpad(pc, a, ctxt, cursym)
663                         } else {
664                                 if p.As != obj.ANOP && p.As != obj.AFUNCDATA && p.As != obj.APCDATA {
665                                         ctxt.Diag("zero-width instruction\n%v", p)
666                                 }
667                                 continue
668                         }
669                 }
670                 pc += int64(m)
671         }
672
673         c.cursym.Size = pc
674
675         /*
676          * if any procedure is large enough to
677          * generate a large SBRA branch, then
678          * generate extra passes putting branches
679          * around jmps to fix. this is rare.
680          */
681         bflag := 1
682
683         var otxt int64
684         var q *obj.Prog
685         var out [5]uint32
686         var falign int32 // Track increased alignment requirements for prefix.
687         for bflag != 0 {
688                 bflag = 0
689                 pc = 0
690                 falign = 0 // Note, linker bumps function symbols to funcAlign.
691                 for p = c.cursym.Func().Text.Link; p != nil; p = p.Link {
692                         p.Pc = pc
693                         o = c.oplook(p)
694
695                         // very large conditional branches
696                         if (o.type_ == 16 || o.type_ == 17) && p.To.Target() != nil {
697                                 otxt = p.To.Target().Pc - pc
698                                 if otxt < -(1<<15)+10 || otxt >= (1<<15)-10 {
699                                         // Assemble the instruction with a target not too far to figure out BI and BO fields.
700                                         // If only the CTR or BI (the CR bit) are tested, the conditional branch can be inverted,
701                                         // and only one extra branch is needed to reach the target.
702                                         tgt := p.To.Target()
703                                         p.To.SetTarget(p.Link)
704                                         o.asmout(&c, p, o, &out)
705                                         p.To.SetTarget(tgt)
706
707                                         bo := int64(out[0]>>21) & 31
708                                         bi := int16((out[0] >> 16) & 31)
709                                         invertible := false
710
711                                         if bo&0x14 == 0x14 {
712                                                 // A conditional branch that is unconditionally taken. This cannot be inverted.
713                                         } else if bo&0x10 == 0x10 {
714                                                 // A branch based on the value of CTR. Invert the CTR comparison against zero bit.
715                                                 bo ^= 0x2
716                                                 invertible = true
717                                         } else if bo&0x04 == 0x04 {
718                                                 // A branch based on CR bit. Invert the BI comparison bit.
719                                                 bo ^= 0x8
720                                                 invertible = true
721                                         }
722
723                                         if invertible {
724                                                 // Rewrite
725                                                 //     BC bo,...,far_away_target
726                                                 //     NEXT_INSN
727                                                 // to:
728                                                 //     BC invert(bo),next_insn
729                                                 //     JMP far_away_target
730                                                 //   next_insn:
731                                                 //     NEXT_INSN
732                                                 p.As = ABC
733                                                 p.From = obj.Addr{Type: obj.TYPE_CONST, Name: obj.NAME_NONE, Offset: bo}
734                                                 q = c.newprog()
735                                                 q.As = ABR
736                                                 q.To.Type = obj.TYPE_BRANCH
737                                                 q.To.SetTarget(p.To.Target())
738                                                 q.Link = p.Link
739                                                 p.To.SetTarget(p.Link)
740                                                 p.Link = q
741                                                 p.Reg = REG_CRBIT0 + bi
742                                         } else {
743                                                 // Rewrite
744                                                 //     BC ...,far_away_target
745                                                 //     NEXT_INSN
746                                                 // to
747                                                 //     BC ...,tmp
748                                                 //     JMP next_insn
749                                                 //   tmp:
750                                                 //     JMP far_away_target
751                                                 //   next_insn:
752                                                 //     NEXT_INSN
753                                                 q = c.newprog()
754                                                 q.Link = p.Link
755                                                 p.Link = q
756                                                 q.As = ABR
757                                                 q.To.Type = obj.TYPE_BRANCH
758                                                 q.To.SetTarget(p.To.Target())
759                                                 p.To.SetTarget(q)
760                                                 q = c.newprog()
761                                                 q.Link = p.Link
762                                                 p.Link = q
763                                                 q.As = ABR
764                                                 q.To.Type = obj.TYPE_BRANCH
765                                                 q.To.SetTarget(q.Link.Link)
766                                         }
767                                         bflag = 1
768                                 }
769                         }
770
771                         m = int(o.size)
772                         if m == 0 {
773                                 if p.As == obj.APCALIGN {
774                                         a := c.vregoff(&p.From)
775                                         m = addpad(pc, a, ctxt, cursym)
776                                 } else {
777                                         if p.As != obj.ANOP && p.As != obj.AFUNCDATA && p.As != obj.APCDATA {
778                                                 ctxt.Diag("zero-width instruction\n%v", p)
779                                         }
780                                         continue
781                                 }
782                         }
783
784                         // Prefixed instructions cannot be placed across a 64B boundary.
785                         // Mark and adjust the PC of those which do. A nop will be
786                         // inserted during final assembly.
787                         if o.ispfx {
788                                 mark := p.Mark &^ PFX_X64B
789                                 if pc&63 == 60 {
790                                         p.Pc += 4
791                                         m += 4
792                                         mark |= PFX_X64B
793                                 }
794
795                                 // Marks may be adjusted if a too-far conditional branch is
796                                 // fixed up above. Likewise, inserting a NOP may cause a
797                                 // branch target to become too far away.  We need to run
798                                 // another iteration and verify no additional changes
799                                 // are needed.
800                                 if mark != p.Mark {
801                                         bflag = 1
802                                         p.Mark = mark
803                                 }
804
805                                 // Check for 16 or 32B crossing of this prefixed insn.
806                                 // These do no require padding, but do require increasing
807                                 // the function alignment to prevent them from potentially
808                                 // crossing a 64B boundary when the linker assigns the final
809                                 // PC.
810                                 switch p.Pc & 31 {
811                                 case 28: // 32B crossing
812                                         falign = 64
813                                 case 12: // 16B crossing
814                                         if falign < 64 {
815                                                 falign = 32
816                                         }
817                                 }
818                         }
819
820                         pc += int64(m)
821                 }
822
823                 c.cursym.Size = pc
824         }
825
826         c.cursym.Size = pc
827         c.cursym.Func().Align = falign
828         c.cursym.Grow(c.cursym.Size)
829
830         // lay out the code, emitting code and data relocations.
831
832         bp := c.cursym.P
833         nop := LOP_IRR(OP_ORI, REGZERO, REGZERO, 0)
834         var i int32
835         for p := c.cursym.Func().Text.Link; p != nil; p = p.Link {
836                 c.pc = p.Pc
837                 o = c.oplook(p)
838                 if int(o.size) > 4*len(out) {
839                         log.Fatalf("out array in span9 is too small, need at least %d for %v", o.size/4, p)
840                 }
841                 // asmout is not set up to add large amounts of padding
842                 if o.type_ == 0 && p.As == obj.APCALIGN {
843                         aln := c.vregoff(&p.From)
844                         v := addpad(p.Pc, aln, c.ctxt, c.cursym)
845                         if v > 0 {
846                                 // Same padding instruction for all
847                                 for i = 0; i < int32(v/4); i++ {
848                                         c.ctxt.Arch.ByteOrder.PutUint32(bp, nop)
849                                         bp = bp[4:]
850                                 }
851                         }
852                 } else {
853                         if p.Mark&PFX_X64B != 0 {
854                                 c.ctxt.Arch.ByteOrder.PutUint32(bp, nop)
855                                 bp = bp[4:]
856                         }
857                         o.asmout(&c, p, o, &out)
858                         for i = 0; i < int32(o.size/4); i++ {
859                                 c.ctxt.Arch.ByteOrder.PutUint32(bp, out[i])
860                                 bp = bp[4:]
861                         }
862                 }
863         }
864 }
865
866 func isint32(v int64) bool {
867         return int64(int32(v)) == v
868 }
869
870 func isuint32(v uint64) bool {
871         return uint64(uint32(v)) == v
872 }
873
874 func (c *ctxt9) aclassreg(reg int16) int {
875         if REG_R0 <= reg && reg <= REG_R31 {
876                 return C_REGP + int(reg&1)
877         }
878         if REG_F0 <= reg && reg <= REG_F31 {
879                 return C_FREGP + int(reg&1)
880         }
881         if REG_V0 <= reg && reg <= REG_V31 {
882                 return C_VREG
883         }
884         if REG_VS0 <= reg && reg <= REG_VS63 {
885                 return C_VSREGP + int(reg&1)
886         }
887         if REG_CR0 <= reg && reg <= REG_CR7 || reg == REG_CR {
888                 return C_CREG
889         }
890         if REG_CR0LT <= reg && reg <= REG_CR7SO {
891                 return C_CRBIT
892         }
893         if REG_SPR0 <= reg && reg <= REG_SPR0+1023 {
894                 switch reg {
895                 case REG_LR:
896                         return C_LR
897
898                 case REG_XER:
899                         return C_XER
900
901                 case REG_CTR:
902                         return C_CTR
903                 }
904
905                 return C_SPR
906         }
907         if REG_A0 <= reg && reg <= REG_A7 {
908                 return C_AREG
909         }
910         if reg == REG_FPSCR {
911                 return C_FPSCR
912         }
913         return C_GOK
914 }
915
916 func (c *ctxt9) aclass(a *obj.Addr) int {
917         switch a.Type {
918         case obj.TYPE_NONE:
919                 return C_NONE
920
921         case obj.TYPE_REG:
922                 return c.aclassreg(a.Reg)
923
924         case obj.TYPE_MEM:
925                 if a.Index != 0 {
926                         if a.Name != obj.NAME_NONE || a.Offset != 0 {
927                                 c.ctxt.Logf("Unexpected Instruction operand index %d offset %d class %d \n", a.Index, a.Offset, a.Class)
928
929                         }
930                         return C_XOREG
931                 }
932                 switch a.Name {
933                 case obj.NAME_GOTREF, obj.NAME_TOCREF:
934                         return C_ADDR
935
936                 case obj.NAME_EXTERN,
937                         obj.NAME_STATIC:
938                         c.instoffset = a.Offset
939                         if a.Sym == nil {
940                                 break
941                         } else if a.Sym.Type == objabi.STLSBSS {
942                                 // For PIC builds, use 12 byte got initial-exec TLS accesses.
943                                 if c.ctxt.Flag_shared {
944                                         return C_TLS_IE
945                                 }
946                                 // Otherwise, use 8 byte local-exec TLS accesses.
947                                 return C_TLS_LE
948                         } else {
949                                 return C_ADDR
950                         }
951
952                 case obj.NAME_AUTO:
953                         c.instoffset = int64(c.autosize) + a.Offset
954
955                         if c.instoffset >= -BIG && c.instoffset < BIG {
956                                 return C_SOREG
957                         }
958                         return C_LOREG
959
960                 case obj.NAME_PARAM:
961                         c.instoffset = int64(c.autosize) + a.Offset + c.ctxt.Arch.FixedFrameSize
962                         if c.instoffset >= -BIG && c.instoffset < BIG {
963                                 return C_SOREG
964                         }
965                         return C_LOREG
966
967                 case obj.NAME_NONE:
968                         c.instoffset = a.Offset
969                         if a.Offset == 0 && a.Index == 0 {
970                                 return C_ZOREG
971                         } else if c.instoffset >= -BIG && c.instoffset < BIG {
972                                 return C_SOREG
973                         } else {
974                                 return C_LOREG
975                         }
976                 }
977
978                 return C_GOK
979
980         case obj.TYPE_TEXTSIZE:
981                 return C_TEXTSIZE
982
983         case obj.TYPE_FCONST:
984                 // The only cases where FCONST will occur are with float64 +/- 0.
985                 // All other float constants are generated in memory.
986                 f64 := a.Val.(float64)
987                 if f64 == 0 {
988                         if math.Signbit(f64) {
989                                 return C_ADDCON
990                         }
991                         return C_ZCON
992                 }
993                 log.Fatalf("Unexpected nonzero FCONST operand %v", a)
994
995         case obj.TYPE_CONST,
996                 obj.TYPE_ADDR:
997                 switch a.Name {
998                 case obj.NAME_NONE:
999                         c.instoffset = a.Offset
1000                         if a.Reg != 0 {
1001                                 if -BIG <= c.instoffset && c.instoffset < BIG {
1002                                         return C_SACON
1003                                 }
1004                                 if isint32(c.instoffset) {
1005                                         return C_LACON
1006                                 }
1007                                 return C_DACON
1008                         }
1009
1010                 case obj.NAME_EXTERN,
1011                         obj.NAME_STATIC:
1012                         s := a.Sym
1013                         if s == nil {
1014                                 return C_GOK
1015                         }
1016                         c.instoffset = a.Offset
1017                         return C_LACON
1018
1019                 case obj.NAME_AUTO:
1020                         c.instoffset = int64(c.autosize) + a.Offset
1021                         if c.instoffset >= -BIG && c.instoffset < BIG {
1022                                 return C_SACON
1023                         }
1024                         return C_LACON
1025
1026                 case obj.NAME_PARAM:
1027                         c.instoffset = int64(c.autosize) + a.Offset + c.ctxt.Arch.FixedFrameSize
1028                         if c.instoffset >= -BIG && c.instoffset < BIG {
1029                                 return C_SACON
1030                         }
1031                         return C_LACON
1032
1033                 default:
1034                         return C_GOK
1035                 }
1036
1037                 if c.instoffset >= 0 {
1038                         sbits := bits.Len64(uint64(c.instoffset))
1039                         switch {
1040                         case sbits <= 5:
1041                                 return C_ZCON + sbits
1042                         case sbits <= 8:
1043                                 return C_U8CON
1044                         case sbits <= 15:
1045                                 return C_U15CON
1046                         case sbits <= 16:
1047                                 return C_U16CON
1048                         case sbits <= 31:
1049                                 // Special case, a positive int32 value which is a multiple of 2^16
1050                                 if c.instoffset&0xFFFF == 0 {
1051                                         return C_U3216CON
1052                                 }
1053                                 return C_U32CON
1054                         case sbits <= 32:
1055                                 return C_U32CON
1056                         case sbits <= 33:
1057                                 return C_S34CON
1058                         default:
1059                                 return C_64CON
1060                         }
1061                 } else {
1062                         sbits := bits.Len64(uint64(^c.instoffset))
1063                         switch {
1064                         case sbits <= 15:
1065                                 return C_S16CON
1066                         case sbits <= 31:
1067                                 // Special case, a negative int32 value which is a multiple of 2^16
1068                                 if c.instoffset&0xFFFF == 0 {
1069                                         return C_S3216CON
1070                                 }
1071                                 return C_S32CON
1072                         case sbits <= 33:
1073                                 return C_S34CON
1074                         default:
1075                                 return C_64CON
1076                         }
1077                 }
1078
1079         case obj.TYPE_BRANCH:
1080                 if a.Sym != nil && c.ctxt.Flag_dynlink && !pfxEnabled {
1081                         return C_LBRAPIC
1082                 }
1083                 return C_SBRA
1084         }
1085
1086         return C_GOK
1087 }
1088
1089 func prasm(p *obj.Prog) {
1090         fmt.Printf("%v\n", p)
1091 }
1092
1093 func (c *ctxt9) oplook(p *obj.Prog) *Optab {
1094         a1 := int(p.Optab)
1095         if a1 != 0 {
1096                 return &optab[a1-1]
1097         }
1098         a1 = int(p.From.Class)
1099         if a1 == 0 {
1100                 a1 = c.aclass(&p.From) + 1
1101                 p.From.Class = int8(a1)
1102         }
1103         a1--
1104
1105         argsv := [3]int{C_NONE + 1, C_NONE + 1, C_NONE + 1}
1106         for i, ap := range p.RestArgs {
1107                 argsv[i] = int(ap.Addr.Class)
1108                 if argsv[i] == 0 {
1109                         argsv[i] = c.aclass(&ap.Addr) + 1
1110                         ap.Addr.Class = int8(argsv[i])
1111                 }
1112
1113         }
1114         a3 := argsv[0] - 1
1115         a4 := argsv[1] - 1
1116         a5 := argsv[2] - 1
1117
1118         a6 := int(p.To.Class)
1119         if a6 == 0 {
1120                 a6 = c.aclass(&p.To) + 1
1121                 p.To.Class = int8(a6)
1122         }
1123         a6--
1124
1125         a2 := C_NONE
1126         if p.Reg != 0 {
1127                 a2 = c.aclassreg(p.Reg)
1128         }
1129
1130         // c.ctxt.Logf("oplook %v %d %d %d %d\n", p, a1, a2, a3, a4, a5, a6)
1131         ops := oprange[p.As&obj.AMask]
1132         c1 := &xcmp[a1]
1133         c2 := &xcmp[a2]
1134         c3 := &xcmp[a3]
1135         c4 := &xcmp[a4]
1136         c5 := &xcmp[a5]
1137         c6 := &xcmp[a6]
1138         for i := range ops {
1139                 op := &ops[i]
1140                 if c1[op.a1] && c2[op.a2] && c3[op.a3] && c4[op.a4] && c5[op.a5] && c6[op.a6] {
1141                         p.Optab = uint16(cap(optab) - cap(ops) + i + 1)
1142                         return op
1143                 }
1144         }
1145
1146         c.ctxt.Diag("illegal combination %v %v %v %v %v %v %v", p.As, DRconv(a1), DRconv(a2), DRconv(a3), DRconv(a4), DRconv(a5), DRconv(a6))
1147         prasm(p)
1148         if ops == nil {
1149                 ops = optab
1150         }
1151         return &ops[0]
1152 }
1153
1154 // Compare two operand types (ex C_REG, or C_SCON)
1155 // and return true if b is compatible with a.
1156 //
1157 // Argument comparison isn't reflexitive, so care must be taken.
1158 // a is the argument type as found in optab, b is the argument as
1159 // fitted by aclass.
1160 func cmp(a int, b int) bool {
1161         if a == b {
1162                 return true
1163         }
1164         switch a {
1165
1166         case C_SPR:
1167                 if b == C_LR || b == C_XER || b == C_CTR {
1168                         return true
1169                 }
1170
1171         case C_U1CON:
1172                 return cmp(C_ZCON, b)
1173         case C_U2CON:
1174                 return cmp(C_U1CON, b)
1175         case C_U3CON:
1176                 return cmp(C_U2CON, b)
1177         case C_U4CON:
1178                 return cmp(C_U3CON, b)
1179         case C_U5CON:
1180                 return cmp(C_U4CON, b)
1181         case C_U8CON:
1182                 return cmp(C_U5CON, b)
1183         case C_U15CON:
1184                 return cmp(C_U8CON, b)
1185         case C_U16CON:
1186                 return cmp(C_U15CON, b)
1187
1188         case C_S16CON:
1189                 return cmp(C_U15CON, b)
1190         case C_32CON:
1191                 return cmp(C_S16CON, b) || cmp(C_U16CON, b) || cmp(C_32S16CON, b)
1192         case C_S34CON:
1193                 return cmp(C_32CON, b)
1194         case C_64CON:
1195                 return cmp(C_S34CON, b)
1196
1197         case C_32S16CON:
1198                 return cmp(C_ZCON, b)
1199
1200         case C_LACON:
1201                 return cmp(C_SACON, b)
1202
1203         case C_LBRA:
1204                 return cmp(C_SBRA, b)
1205
1206         case C_SOREG:
1207                 return cmp(C_ZOREG, b)
1208
1209         case C_LOREG:
1210                 return cmp(C_SOREG, b)
1211
1212         case C_XOREG:
1213                 return cmp(C_REG, b) || cmp(C_ZOREG, b)
1214
1215         // An even/odd register input always matches the regular register types.
1216         case C_REG:
1217                 return cmp(C_REGP, b) || (b == C_ZCON && r0iszero != 0)
1218         case C_FREG:
1219                 return cmp(C_FREGP, b)
1220         case C_VSREG:
1221                 /* Allow any VR argument as a VSR operand. */
1222                 return cmp(C_VSREGP, b) || cmp(C_VREG, b)
1223
1224         case C_ANY:
1225                 return true
1226         }
1227
1228         return false
1229 }
1230
1231 // Used when sorting the optab. Sorting is
1232 // done in a way so that the best choice of
1233 // opcode/operand combination is considered first.
1234 func optabLess(i, j int) bool {
1235         p1 := &optab[i]
1236         p2 := &optab[j]
1237         n := int(p1.as) - int(p2.as)
1238         // same opcode
1239         if n != 0 {
1240                 return n < 0
1241         }
1242         // Consider those that generate fewer
1243         // instructions first.
1244         n = int(p1.size) - int(p2.size)
1245         if n != 0 {
1246                 return n < 0
1247         }
1248         // operand order should match
1249         // better choices first
1250         n = int(p1.a1) - int(p2.a1)
1251         if n != 0 {
1252                 return n < 0
1253         }
1254         n = int(p1.a2) - int(p2.a2)
1255         if n != 0 {
1256                 return n < 0
1257         }
1258         n = int(p1.a3) - int(p2.a3)
1259         if n != 0 {
1260                 return n < 0
1261         }
1262         n = int(p1.a4) - int(p2.a4)
1263         if n != 0 {
1264                 return n < 0
1265         }
1266         n = int(p1.a5) - int(p2.a5)
1267         if n != 0 {
1268                 return n < 0
1269         }
1270         n = int(p1.a6) - int(p2.a6)
1271         if n != 0 {
1272                 return n < 0
1273         }
1274         return false
1275 }
1276
1277 // Add an entry to the opcode table for
1278 // a new opcode b0 with the same operand combinations
1279 // as opcode a.
1280 func opset(a, b0 obj.As) {
1281         oprange[a&obj.AMask] = oprange[b0]
1282 }
1283
1284 // Determine if the build configuration requires a TOC pointer.
1285 // It is assumed this always called after buildop.
1286 func NeedTOCpointer(ctxt *obj.Link) bool {
1287         return !pfxEnabled && ctxt.Flag_shared
1288 }
1289
1290 // Build the opcode table
1291 func buildop(ctxt *obj.Link) {
1292         // Limit PC-relative prefix instruction usage to supported and tested targets.
1293         pfxEnabled = buildcfg.GOPPC64 >= 10 && buildcfg.GOOS == "linux"
1294         cfg := fmt.Sprintf("power%d/%s/%s", buildcfg.GOPPC64, buildcfg.GOARCH, buildcfg.GOOS)
1295         if cfg == buildOpCfg {
1296                 // Already initialized to correct OS/cpu; stop now.
1297                 // This happens in the cmd/asm tests,
1298                 // each of which re-initializes the arch.
1299                 return
1300         }
1301         buildOpCfg = cfg
1302
1303         // Configure the optab entries which may generate prefix opcodes.
1304         prefixOptab := make([]Optab, 0, len(prefixableOptab))
1305         for _, entry := range prefixableOptab {
1306                 entry := entry
1307                 if pfxEnabled && buildcfg.GOPPC64 >= entry.minGOPPC64 {
1308                         // Enable prefix opcode generation and resize.
1309                         entry.ispfx = true
1310                         entry.size = entry.pfxsize
1311                 }
1312                 prefixOptab = append(prefixOptab, entry.Optab)
1313
1314         }
1315
1316         for i := 0; i < C_NCLASS; i++ {
1317                 for n := 0; n < C_NCLASS; n++ {
1318                         if cmp(n, i) {
1319                                 xcmp[i][n] = true
1320                         }
1321                 }
1322         }
1323
1324         // Append the generated entries, sort, and fill out oprange.
1325         optab = make([]Optab, 0, len(optabBase)+len(optabGen)+len(prefixOptab))
1326         optab = append(optab, optabBase...)
1327         optab = append(optab, optabGen...)
1328         optab = append(optab, prefixOptab...)
1329         sort.Slice(optab, optabLess)
1330
1331         for i := range optab {
1332                 // Use the legacy assembler function if none provided.
1333                 if optab[i].asmout == nil {
1334                         optab[i].asmout = asmout
1335                 }
1336         }
1337
1338         for i := 0; i < len(optab); {
1339                 r := optab[i].as
1340                 r0 := r & obj.AMask
1341                 start := i
1342                 for i < len(optab) && optab[i].as == r {
1343                         i++
1344                 }
1345                 oprange[r0] = optab[start:i]
1346
1347                 switch r {
1348                 default:
1349                         if !opsetGen(r) {
1350                                 ctxt.Diag("unknown op in build: %v", r)
1351                                 log.Fatalf("instruction missing from switch in asm9.go:buildop: %v", r)
1352                         }
1353
1354                 case ADCBF: /* unary indexed: op (b+a); op (b) */
1355                         opset(ADCBI, r0)
1356
1357                         opset(ADCBST, r0)
1358                         opset(ADCBT, r0)
1359                         opset(ADCBTST, r0)
1360                         opset(ADCBZ, r0)
1361                         opset(AICBI, r0)
1362
1363                 case ASTDCCC: /* indexed store: op s,(b+a); op s,(b) */
1364                         opset(ASTWCCC, r0)
1365                         opset(ASTHCCC, r0)
1366                         opset(ASTBCCC, r0)
1367
1368                 case AREM: /* macro */
1369                         opset(AREM, r0)
1370
1371                 case AREMU:
1372                         opset(AREMU, r0)
1373
1374                 case AREMD:
1375                         opset(AREMDU, r0)
1376
1377                 case AMULLW:
1378                         opset(AMULLD, r0)
1379
1380                 case ADIVW: /* op Rb[,Ra],Rd */
1381                         opset(AMULHW, r0)
1382
1383                         opset(AMULHWCC, r0)
1384                         opset(AMULHWU, r0)
1385                         opset(AMULHWUCC, r0)
1386                         opset(AMULLWCC, r0)
1387                         opset(AMULLWVCC, r0)
1388                         opset(AMULLWV, r0)
1389                         opset(ADIVWCC, r0)
1390                         opset(ADIVWV, r0)
1391                         opset(ADIVWVCC, r0)
1392                         opset(ADIVWU, r0)
1393                         opset(ADIVWUCC, r0)
1394                         opset(ADIVWUV, r0)
1395                         opset(ADIVWUVCC, r0)
1396                         opset(AMODUD, r0)
1397                         opset(AMODUW, r0)
1398                         opset(AMODSD, r0)
1399                         opset(AMODSW, r0)
1400                         opset(AADDCC, r0)
1401                         opset(AADDCV, r0)
1402                         opset(AADDCVCC, r0)
1403                         opset(AADDV, r0)
1404                         opset(AADDVCC, r0)
1405                         opset(AADDE, r0)
1406                         opset(AADDECC, r0)
1407                         opset(AADDEV, r0)
1408                         opset(AADDEVCC, r0)
1409                         opset(AMULHD, r0)
1410                         opset(AMULHDCC, r0)
1411                         opset(AMULHDU, r0)
1412                         opset(AMULHDUCC, r0)
1413                         opset(AMULLDCC, r0)
1414                         opset(AMULLDVCC, r0)
1415                         opset(AMULLDV, r0)
1416                         opset(ADIVD, r0)
1417                         opset(ADIVDCC, r0)
1418                         opset(ADIVDE, r0)
1419                         opset(ADIVDEU, r0)
1420                         opset(ADIVDECC, r0)
1421                         opset(ADIVDEUCC, r0)
1422                         opset(ADIVDVCC, r0)
1423                         opset(ADIVDV, r0)
1424                         opset(ADIVDU, r0)
1425                         opset(ADIVDUV, r0)
1426                         opset(ADIVDUVCC, r0)
1427                         opset(ADIVDUCC, r0)
1428
1429                 case ACRAND:
1430                         opset(ACRANDN, r0)
1431                         opset(ACREQV, r0)
1432                         opset(ACRNAND, r0)
1433                         opset(ACRNOR, r0)
1434                         opset(ACROR, r0)
1435                         opset(ACRORN, r0)
1436                         opset(ACRXOR, r0)
1437
1438                 case APOPCNTD: /* popcntd, popcntw, popcntb, cnttzw, cnttzd */
1439                         opset(APOPCNTW, r0)
1440                         opset(APOPCNTB, r0)
1441                         opset(ACNTTZW, r0)
1442                         opset(ACNTTZWCC, r0)
1443                         opset(ACNTTZD, r0)
1444                         opset(ACNTTZDCC, r0)
1445
1446                 case ACOPY: /* copy, paste. */
1447                         opset(APASTECC, r0)
1448
1449                 case AMADDHD: /* maddhd, maddhdu, maddld */
1450                         opset(AMADDHDU, r0)
1451                         opset(AMADDLD, r0)
1452
1453                 case AMOVBZ: /* lbz, stz, rlwm(r/r), lhz, lha, stz, and x variants */
1454                         opset(AMOVH, r0)
1455                         opset(AMOVHZ, r0)
1456
1457                 case AMOVBZU: /* lbz[x]u, stb[x]u, lhz[x]u, lha[x]u, sth[u]x, ld[x]u, std[u]x */
1458                         opset(AMOVHU, r0)
1459
1460                         opset(AMOVHZU, r0)
1461                         opset(AMOVWU, r0)
1462                         opset(AMOVWZU, r0)
1463                         opset(AMOVDU, r0)
1464                         opset(AMOVMW, r0)
1465
1466                 case ALVEBX: /* lvebx, lvehx, lvewx, lvx, lvxl, lvsl, lvsr */
1467                         opset(ALVEHX, r0)
1468                         opset(ALVEWX, r0)
1469                         opset(ALVX, r0)
1470                         opset(ALVXL, r0)
1471                         opset(ALVSL, r0)
1472                         opset(ALVSR, r0)
1473
1474                 case ASTVEBX: /* stvebx, stvehx, stvewx, stvx, stvxl */
1475                         opset(ASTVEHX, r0)
1476                         opset(ASTVEWX, r0)
1477                         opset(ASTVX, r0)
1478                         opset(ASTVXL, r0)
1479
1480                 case AVAND: /* vand, vandc, vnand */
1481                         opset(AVAND, r0)
1482                         opset(AVANDC, r0)
1483                         opset(AVNAND, r0)
1484
1485                 case AVMRGOW: /* vmrgew, vmrgow */
1486                         opset(AVMRGEW, r0)
1487
1488                 case AVOR: /* vor, vorc, vxor, vnor, veqv */
1489                         opset(AVOR, r0)
1490                         opset(AVORC, r0)
1491                         opset(AVXOR, r0)
1492                         opset(AVNOR, r0)
1493                         opset(AVEQV, r0)
1494
1495                 case AVADDUM: /* vaddubm, vadduhm, vadduwm, vaddudm, vadduqm */
1496                         opset(AVADDUBM, r0)
1497                         opset(AVADDUHM, r0)
1498                         opset(AVADDUWM, r0)
1499                         opset(AVADDUDM, r0)
1500                         opset(AVADDUQM, r0)
1501
1502                 case AVADDCU: /* vaddcuq, vaddcuw */
1503                         opset(AVADDCUQ, r0)
1504                         opset(AVADDCUW, r0)
1505
1506                 case AVADDUS: /* vaddubs, vadduhs, vadduws */
1507                         opset(AVADDUBS, r0)
1508                         opset(AVADDUHS, r0)
1509                         opset(AVADDUWS, r0)
1510
1511                 case AVADDSS: /* vaddsbs, vaddshs, vaddsws */
1512                         opset(AVADDSBS, r0)
1513                         opset(AVADDSHS, r0)
1514                         opset(AVADDSWS, r0)
1515
1516                 case AVADDE: /* vaddeuqm, vaddecuq */
1517                         opset(AVADDEUQM, r0)
1518                         opset(AVADDECUQ, r0)
1519
1520                 case AVSUBUM: /* vsububm, vsubuhm, vsubuwm, vsubudm, vsubuqm */
1521                         opset(AVSUBUBM, r0)
1522                         opset(AVSUBUHM, r0)
1523                         opset(AVSUBUWM, r0)
1524                         opset(AVSUBUDM, r0)
1525                         opset(AVSUBUQM, r0)
1526
1527                 case AVSUBCU: /* vsubcuq, vsubcuw */
1528                         opset(AVSUBCUQ, r0)
1529                         opset(AVSUBCUW, r0)
1530
1531                 case AVSUBUS: /* vsububs, vsubuhs, vsubuws */
1532                         opset(AVSUBUBS, r0)
1533                         opset(AVSUBUHS, r0)
1534                         opset(AVSUBUWS, r0)
1535
1536                 case AVSUBSS: /* vsubsbs, vsubshs, vsubsws */
1537                         opset(AVSUBSBS, r0)
1538                         opset(AVSUBSHS, r0)
1539                         opset(AVSUBSWS, r0)
1540
1541                 case AVSUBE: /* vsubeuqm, vsubecuq */
1542                         opset(AVSUBEUQM, r0)
1543                         opset(AVSUBECUQ, r0)
1544
1545                 case AVMULESB: /* vmulesb, vmulosb, vmuleub, vmuloub, vmulosh, vmulouh, vmulesw, vmulosw, vmuleuw, vmulouw, vmuluwm */
1546                         opset(AVMULOSB, r0)
1547                         opset(AVMULEUB, r0)
1548                         opset(AVMULOUB, r0)
1549                         opset(AVMULESH, r0)
1550                         opset(AVMULOSH, r0)
1551                         opset(AVMULEUH, r0)
1552                         opset(AVMULOUH, r0)
1553                         opset(AVMULESW, r0)
1554                         opset(AVMULOSW, r0)
1555                         opset(AVMULEUW, r0)
1556                         opset(AVMULOUW, r0)
1557                         opset(AVMULUWM, r0)
1558                 case AVPMSUM: /* vpmsumb, vpmsumh, vpmsumw, vpmsumd */
1559                         opset(AVPMSUMB, r0)
1560                         opset(AVPMSUMH, r0)
1561                         opset(AVPMSUMW, r0)
1562                         opset(AVPMSUMD, r0)
1563
1564                 case AVR: /* vrlb, vrlh, vrlw, vrld */
1565                         opset(AVRLB, r0)
1566                         opset(AVRLH, r0)
1567                         opset(AVRLW, r0)
1568                         opset(AVRLD, r0)
1569
1570                 case AVS: /* vs[l,r], vs[l,r]o, vs[l,r]b, vs[l,r]h, vs[l,r]w, vs[l,r]d */
1571                         opset(AVSLB, r0)
1572                         opset(AVSLH, r0)
1573                         opset(AVSLW, r0)
1574                         opset(AVSL, r0)
1575                         opset(AVSLO, r0)
1576                         opset(AVSRB, r0)
1577                         opset(AVSRH, r0)
1578                         opset(AVSRW, r0)
1579                         opset(AVSR, r0)
1580                         opset(AVSRO, r0)
1581                         opset(AVSLD, r0)
1582                         opset(AVSRD, r0)
1583
1584                 case AVSA: /* vsrab, vsrah, vsraw, vsrad */
1585                         opset(AVSRAB, r0)
1586                         opset(AVSRAH, r0)
1587                         opset(AVSRAW, r0)
1588                         opset(AVSRAD, r0)
1589
1590                 case AVSOI: /* vsldoi */
1591                         opset(AVSLDOI, r0)
1592
1593                 case AVCLZ: /* vclzb, vclzh, vclzw, vclzd */
1594                         opset(AVCLZB, r0)
1595                         opset(AVCLZH, r0)
1596                         opset(AVCLZW, r0)
1597                         opset(AVCLZD, r0)
1598
1599                 case AVPOPCNT: /* vpopcntb, vpopcnth, vpopcntw, vpopcntd */
1600                         opset(AVPOPCNTB, r0)
1601                         opset(AVPOPCNTH, r0)
1602                         opset(AVPOPCNTW, r0)
1603                         opset(AVPOPCNTD, r0)
1604
1605                 case AVCMPEQ: /* vcmpequb[.], vcmpequh[.], vcmpequw[.], vcmpequd[.] */
1606                         opset(AVCMPEQUB, r0)
1607                         opset(AVCMPEQUBCC, r0)
1608                         opset(AVCMPEQUH, r0)
1609                         opset(AVCMPEQUHCC, r0)
1610                         opset(AVCMPEQUW, r0)
1611                         opset(AVCMPEQUWCC, r0)
1612                         opset(AVCMPEQUD, r0)
1613                         opset(AVCMPEQUDCC, r0)
1614
1615                 case AVCMPGT: /* vcmpgt[u,s]b[.], vcmpgt[u,s]h[.], vcmpgt[u,s]w[.], vcmpgt[u,s]d[.] */
1616                         opset(AVCMPGTUB, r0)
1617                         opset(AVCMPGTUBCC, r0)
1618                         opset(AVCMPGTUH, r0)
1619                         opset(AVCMPGTUHCC, r0)
1620                         opset(AVCMPGTUW, r0)
1621                         opset(AVCMPGTUWCC, r0)
1622                         opset(AVCMPGTUD, r0)
1623                         opset(AVCMPGTUDCC, r0)
1624                         opset(AVCMPGTSB, r0)
1625                         opset(AVCMPGTSBCC, r0)
1626                         opset(AVCMPGTSH, r0)
1627                         opset(AVCMPGTSHCC, r0)
1628                         opset(AVCMPGTSW, r0)
1629                         opset(AVCMPGTSWCC, r0)
1630                         opset(AVCMPGTSD, r0)
1631                         opset(AVCMPGTSDCC, r0)
1632
1633                 case AVCMPNEZB: /* vcmpnezb[.] */
1634                         opset(AVCMPNEZBCC, r0)
1635                         opset(AVCMPNEB, r0)
1636                         opset(AVCMPNEBCC, r0)
1637                         opset(AVCMPNEH, r0)
1638                         opset(AVCMPNEHCC, r0)
1639                         opset(AVCMPNEW, r0)
1640                         opset(AVCMPNEWCC, r0)
1641
1642                 case AVPERM: /* vperm */
1643                         opset(AVPERMXOR, r0)
1644                         opset(AVPERMR, r0)
1645
1646                 case AVBPERMQ: /* vbpermq, vbpermd */
1647                         opset(AVBPERMD, r0)
1648
1649                 case AVSEL: /* vsel */
1650                         opset(AVSEL, r0)
1651
1652                 case AVSPLTB: /* vspltb, vsplth, vspltw */
1653                         opset(AVSPLTH, r0)
1654                         opset(AVSPLTW, r0)
1655
1656                 case AVSPLTISB: /* vspltisb, vspltish, vspltisw */
1657                         opset(AVSPLTISH, r0)
1658                         opset(AVSPLTISW, r0)
1659
1660                 case AVCIPH: /* vcipher, vcipherlast */
1661                         opset(AVCIPHER, r0)
1662                         opset(AVCIPHERLAST, r0)
1663
1664                 case AVNCIPH: /* vncipher, vncipherlast */
1665                         opset(AVNCIPHER, r0)
1666                         opset(AVNCIPHERLAST, r0)
1667
1668                 case AVSBOX: /* vsbox */
1669                         opset(AVSBOX, r0)
1670
1671                 case AVSHASIGMA: /* vshasigmaw, vshasigmad */
1672                         opset(AVSHASIGMAW, r0)
1673                         opset(AVSHASIGMAD, r0)
1674
1675                 case ALXVD2X: /* lxvd2x, lxvdsx, lxvw4x, lxvh8x, lxvb16x */
1676                         opset(ALXVDSX, r0)
1677                         opset(ALXVW4X, r0)
1678                         opset(ALXVH8X, r0)
1679                         opset(ALXVB16X, r0)
1680
1681                 case ALXV: /* lxv */
1682                         opset(ALXV, r0)
1683
1684                 case ALXVL: /* lxvl, lxvll, lxvx */
1685                         opset(ALXVLL, r0)
1686                         opset(ALXVX, r0)
1687
1688                 case ASTXVD2X: /* stxvd2x, stxvdsx, stxvw4x, stxvh8x, stxvb16x */
1689                         opset(ASTXVW4X, r0)
1690                         opset(ASTXVH8X, r0)
1691                         opset(ASTXVB16X, r0)
1692
1693                 case ASTXV: /* stxv */
1694                         opset(ASTXV, r0)
1695
1696                 case ASTXVL: /* stxvl, stxvll, stvx */
1697                         opset(ASTXVLL, r0)
1698                         opset(ASTXVX, r0)
1699
1700                 case ALXSDX: /* lxsdx  */
1701                         opset(ALXSDX, r0)
1702
1703                 case ASTXSDX: /* stxsdx */
1704                         opset(ASTXSDX, r0)
1705
1706                 case ALXSIWAX: /* lxsiwax, lxsiwzx  */
1707                         opset(ALXSIWZX, r0)
1708
1709                 case ASTXSIWX: /* stxsiwx */
1710                         opset(ASTXSIWX, r0)
1711
1712                 case AMFVSRD: /* mfvsrd, mfvsrwz (and extended mnemonics), mfvsrld */
1713                         opset(AMFFPRD, r0)
1714                         opset(AMFVRD, r0)
1715                         opset(AMFVSRWZ, r0)
1716                         opset(AMFVSRLD, r0)
1717
1718                 case AMTVSRD: /* mtvsrd, mtvsrwa, mtvsrwz (and extended mnemonics), mtvsrdd, mtvsrws */
1719                         opset(AMTFPRD, r0)
1720                         opset(AMTVRD, r0)
1721                         opset(AMTVSRWA, r0)
1722                         opset(AMTVSRWZ, r0)
1723                         opset(AMTVSRWS, r0)
1724
1725                 case AXXLAND: /* xxland, xxlandc, xxleqv, xxlnand */
1726                         opset(AXXLANDC, r0)
1727                         opset(AXXLEQV, r0)
1728                         opset(AXXLNAND, r0)
1729
1730                 case AXXLOR: /* xxlorc, xxlnor, xxlor, xxlxor */
1731                         opset(AXXLORC, r0)
1732                         opset(AXXLNOR, r0)
1733                         opset(AXXLORQ, r0)
1734                         opset(AXXLXOR, r0)
1735
1736                 case AXXSEL: /* xxsel */
1737                         opset(AXXSEL, r0)
1738
1739                 case AXXMRGHW: /* xxmrghw, xxmrglw */
1740                         opset(AXXMRGLW, r0)
1741
1742                 case AXXSPLTW: /* xxspltw */
1743                         opset(AXXSPLTW, r0)
1744
1745                 case AXXSPLTIB: /* xxspltib */
1746                         opset(AXXSPLTIB, r0)
1747
1748                 case AXXPERM: /* xxpermdi */
1749                         opset(AXXPERM, r0)
1750
1751                 case AXXSLDWI: /* xxsldwi */
1752                         opset(AXXPERMDI, r0)
1753                         opset(AXXSLDWI, r0)
1754
1755                 case AXXBRQ: /* xxbrq, xxbrd, xxbrw, xxbrh */
1756                         opset(AXXBRD, r0)
1757                         opset(AXXBRW, r0)
1758                         opset(AXXBRH, r0)
1759
1760                 case AXSCVDPSP: /* xscvdpsp, xscvspdp, xscvdpspn, xscvspdpn */
1761                         opset(AXSCVSPDP, r0)
1762                         opset(AXSCVDPSPN, r0)
1763                         opset(AXSCVSPDPN, r0)
1764
1765                 case AXVCVDPSP: /* xvcvdpsp, xvcvspdp */
1766                         opset(AXVCVSPDP, r0)
1767
1768                 case AXSCVDPSXDS: /* xscvdpsxds, xscvdpsxws, xscvdpuxds, xscvdpuxws */
1769                         opset(AXSCVDPSXWS, r0)
1770                         opset(AXSCVDPUXDS, r0)
1771                         opset(AXSCVDPUXWS, r0)
1772
1773                 case AXSCVSXDDP: /* xscvsxddp, xscvuxddp, xscvsxdsp, xscvuxdsp */
1774                         opset(AXSCVUXDDP, r0)
1775                         opset(AXSCVSXDSP, r0)
1776                         opset(AXSCVUXDSP, r0)
1777
1778                 case AXVCVDPSXDS: /* xvcvdpsxds, xvcvdpsxws, xvcvdpuxds, xvcvdpuxws, xvcvspsxds, xvcvspsxws, xvcvspuxds, xvcvspuxws */
1779                         opset(AXVCVDPSXDS, r0)
1780                         opset(AXVCVDPSXWS, r0)
1781                         opset(AXVCVDPUXDS, r0)
1782                         opset(AXVCVDPUXWS, r0)
1783                         opset(AXVCVSPSXDS, r0)
1784                         opset(AXVCVSPSXWS, r0)
1785                         opset(AXVCVSPUXDS, r0)
1786                         opset(AXVCVSPUXWS, r0)
1787
1788                 case AXVCVSXDDP: /* xvcvsxddp, xvcvsxwdp, xvcvuxddp, xvcvuxwdp, xvcvsxdsp, xvcvsxwsp, xvcvuxdsp, xvcvuxwsp */
1789                         opset(AXVCVSXWDP, r0)
1790                         opset(AXVCVUXDDP, r0)
1791                         opset(AXVCVUXWDP, r0)
1792                         opset(AXVCVSXDSP, r0)
1793                         opset(AXVCVSXWSP, r0)
1794                         opset(AXVCVUXDSP, r0)
1795                         opset(AXVCVUXWSP, r0)
1796
1797                 case AAND: /* logical op Rb,Rs,Ra; no literal */
1798                         opset(AANDN, r0)
1799                         opset(AANDNCC, r0)
1800                         opset(AEQV, r0)
1801                         opset(AEQVCC, r0)
1802                         opset(ANAND, r0)
1803                         opset(ANANDCC, r0)
1804                         opset(ANOR, r0)
1805                         opset(ANORCC, r0)
1806                         opset(AORCC, r0)
1807                         opset(AORN, r0)
1808                         opset(AORNCC, r0)
1809                         opset(AXORCC, r0)
1810
1811                 case AADDME: /* op Ra, Rd */
1812                         opset(AADDMECC, r0)
1813
1814                         opset(AADDMEV, r0)
1815                         opset(AADDMEVCC, r0)
1816                         opset(AADDZE, r0)
1817                         opset(AADDZECC, r0)
1818                         opset(AADDZEV, r0)
1819                         opset(AADDZEVCC, r0)
1820                         opset(ASUBME, r0)
1821                         opset(ASUBMECC, r0)
1822                         opset(ASUBMEV, r0)
1823                         opset(ASUBMEVCC, r0)
1824                         opset(ASUBZE, r0)
1825                         opset(ASUBZECC, r0)
1826                         opset(ASUBZEV, r0)
1827                         opset(ASUBZEVCC, r0)
1828
1829                 case AADDC:
1830                         opset(AADDCCC, r0)
1831
1832                 case ABEQ:
1833                         opset(ABGE, r0)
1834                         opset(ABGT, r0)
1835                         opset(ABLE, r0)
1836                         opset(ABLT, r0)
1837                         opset(ABNE, r0)
1838                         opset(ABVC, r0)
1839                         opset(ABVS, r0)
1840
1841                 case ABR:
1842                         opset(ABL, r0)
1843
1844                 case ABC:
1845                         opset(ABCL, r0)
1846
1847                 case ABDNZ:
1848                         opset(ABDZ, r0)
1849
1850                 case AEXTSB: /* op Rs, Ra */
1851                         opset(AEXTSBCC, r0)
1852
1853                         opset(AEXTSH, r0)
1854                         opset(AEXTSHCC, r0)
1855                         opset(ACNTLZW, r0)
1856                         opset(ACNTLZWCC, r0)
1857                         opset(ACNTLZD, r0)
1858                         opset(AEXTSW, r0)
1859                         opset(AEXTSWCC, r0)
1860                         opset(ACNTLZDCC, r0)
1861
1862                 case AFABS: /* fop [s,]d */
1863                         opset(AFABSCC, r0)
1864
1865                         opset(AFNABS, r0)
1866                         opset(AFNABSCC, r0)
1867                         opset(AFNEG, r0)
1868                         opset(AFNEGCC, r0)
1869                         opset(AFRSP, r0)
1870                         opset(AFRSPCC, r0)
1871                         opset(AFCTIW, r0)
1872                         opset(AFCTIWCC, r0)
1873                         opset(AFCTIWZ, r0)
1874                         opset(AFCTIWZCC, r0)
1875                         opset(AFCTID, r0)
1876                         opset(AFCTIDCC, r0)
1877                         opset(AFCTIDZ, r0)
1878                         opset(AFCTIDZCC, r0)
1879                         opset(AFCFID, r0)
1880                         opset(AFCFIDCC, r0)
1881                         opset(AFCFIDU, r0)
1882                         opset(AFCFIDUCC, r0)
1883                         opset(AFCFIDS, r0)
1884                         opset(AFCFIDSCC, r0)
1885                         opset(AFRES, r0)
1886                         opset(AFRESCC, r0)
1887                         opset(AFRIM, r0)
1888                         opset(AFRIMCC, r0)
1889                         opset(AFRIP, r0)
1890                         opset(AFRIPCC, r0)
1891                         opset(AFRIZ, r0)
1892                         opset(AFRIZCC, r0)
1893                         opset(AFRIN, r0)
1894                         opset(AFRINCC, r0)
1895                         opset(AFRSQRTE, r0)
1896                         opset(AFRSQRTECC, r0)
1897                         opset(AFSQRT, r0)
1898                         opset(AFSQRTCC, r0)
1899                         opset(AFSQRTS, r0)
1900                         opset(AFSQRTSCC, r0)
1901
1902                 case AFADD:
1903                         opset(AFADDS, r0)
1904                         opset(AFADDCC, r0)
1905                         opset(AFADDSCC, r0)
1906                         opset(AFCPSGN, r0)
1907                         opset(AFCPSGNCC, r0)
1908                         opset(AFDIV, r0)
1909                         opset(AFDIVS, r0)
1910                         opset(AFDIVCC, r0)
1911                         opset(AFDIVSCC, r0)
1912                         opset(AFSUB, r0)
1913                         opset(AFSUBS, r0)
1914                         opset(AFSUBCC, r0)
1915                         opset(AFSUBSCC, r0)
1916
1917                 case AFMADD:
1918                         opset(AFMADDCC, r0)
1919                         opset(AFMADDS, r0)
1920                         opset(AFMADDSCC, r0)
1921                         opset(AFMSUB, r0)
1922                         opset(AFMSUBCC, r0)
1923                         opset(AFMSUBS, r0)
1924                         opset(AFMSUBSCC, r0)
1925                         opset(AFNMADD, r0)
1926                         opset(AFNMADDCC, r0)
1927                         opset(AFNMADDS, r0)
1928                         opset(AFNMADDSCC, r0)
1929                         opset(AFNMSUB, r0)
1930                         opset(AFNMSUBCC, r0)
1931                         opset(AFNMSUBS, r0)
1932                         opset(AFNMSUBSCC, r0)
1933                         opset(AFSEL, r0)
1934                         opset(AFSELCC, r0)
1935
1936                 case AFMUL:
1937                         opset(AFMULS, r0)
1938                         opset(AFMULCC, r0)
1939                         opset(AFMULSCC, r0)
1940
1941                 case AFCMPO:
1942                         opset(AFCMPU, r0)
1943
1944                 case AMTFSB0:
1945                         opset(AMTFSB0CC, r0)
1946                         opset(AMTFSB1, r0)
1947                         opset(AMTFSB1CC, r0)
1948
1949                 case ANEG: /* op [Ra,] Rd */
1950                         opset(ANEGCC, r0)
1951
1952                         opset(ANEGV, r0)
1953                         opset(ANEGVCC, r0)
1954
1955                 case AOR: /* or/xor Rb,Rs,Ra; ori/xori $uimm,Rs,R */
1956                         opset(AXOR, r0)
1957
1958                 case AORIS: /* oris/xoris $uimm,Rs,Ra */
1959                         opset(AXORIS, r0)
1960
1961                 case ASLW:
1962                         opset(ASLWCC, r0)
1963                         opset(ASRW, r0)
1964                         opset(ASRWCC, r0)
1965                         opset(AROTLW, r0)
1966
1967                 case ASLD:
1968                         opset(ASLDCC, r0)
1969                         opset(ASRD, r0)
1970                         opset(ASRDCC, r0)
1971                         opset(AROTL, r0)
1972
1973                 case ASRAW: /* sraw Rb,Rs,Ra; srawi sh,Rs,Ra */
1974                         opset(ASRAWCC, r0)
1975
1976                 case AEXTSWSLI:
1977                         opset(AEXTSWSLICC, r0)
1978
1979                 case ASRAD: /* sraw Rb,Rs,Ra; srawi sh,Rs,Ra */
1980                         opset(ASRADCC, r0)
1981
1982                 case ASUB: /* SUB Ra,Rb,Rd => subf Rd,ra,rb */
1983                         opset(ASUB, r0)
1984
1985                         opset(ASUBCC, r0)
1986                         opset(ASUBV, r0)
1987                         opset(ASUBVCC, r0)
1988                         opset(ASUBCCC, r0)
1989                         opset(ASUBCV, r0)
1990                         opset(ASUBCVCC, r0)
1991                         opset(ASUBE, r0)
1992                         opset(ASUBECC, r0)
1993                         opset(ASUBEV, r0)
1994                         opset(ASUBEVCC, r0)
1995
1996                 case ASYNC:
1997                         opset(AISYNC, r0)
1998                         opset(ALWSYNC, r0)
1999                         opset(APTESYNC, r0)
2000                         opset(ATLBSYNC, r0)
2001
2002                 case ARLWNM:
2003                         opset(ARLWNMCC, r0)
2004                         opset(ARLWMI, r0)
2005                         opset(ARLWMICC, r0)
2006
2007                 case ARLDMI:
2008                         opset(ARLDMICC, r0)
2009                         opset(ARLDIMI, r0)
2010                         opset(ARLDIMICC, r0)
2011
2012                 case ARLDC:
2013                         opset(ARLDCCC, r0)
2014
2015                 case ARLDCL:
2016                         opset(ARLDCR, r0)
2017                         opset(ARLDCLCC, r0)
2018                         opset(ARLDCRCC, r0)
2019
2020                 case ARLDICL:
2021                         opset(ARLDICLCC, r0)
2022                         opset(ARLDICR, r0)
2023                         opset(ARLDICRCC, r0)
2024                         opset(ARLDIC, r0)
2025                         opset(ARLDICCC, r0)
2026                         opset(ACLRLSLDI, r0)
2027
2028                 case AFMOVD:
2029                         opset(AFMOVDCC, r0)
2030                         opset(AFMOVDU, r0)
2031                         opset(AFMOVS, r0)
2032                         opset(AFMOVSU, r0)
2033
2034                 case ALDAR:
2035                         opset(ALBAR, r0)
2036                         opset(ALHAR, r0)
2037                         opset(ALWAR, r0)
2038
2039                 case ASYSCALL: /* just the op; flow of control */
2040                         opset(ARFI, r0)
2041
2042                         opset(ARFCI, r0)
2043                         opset(ARFID, r0)
2044                         opset(AHRFID, r0)
2045
2046                 case AMOVHBR:
2047                         opset(AMOVWBR, r0)
2048                         opset(AMOVDBR, r0)
2049
2050                 case ASLBMFEE:
2051                         opset(ASLBMFEV, r0)
2052
2053                 case ATW:
2054                         opset(ATD, r0)
2055
2056                 case ATLBIE:
2057                         opset(ASLBIE, r0)
2058                         opset(ATLBIEL, r0)
2059
2060                 case AEIEIO:
2061                         opset(ASLBIA, r0)
2062
2063                 case ACMP:
2064                         opset(ACMPW, r0)
2065
2066                 case ACMPU:
2067                         opset(ACMPWU, r0)
2068
2069                 case ACMPB:
2070                         opset(ACMPB, r0)
2071
2072                 case AFTDIV:
2073                         opset(AFTDIV, r0)
2074
2075                 case AFTSQRT:
2076                         opset(AFTSQRT, r0)
2077
2078                 case AMOVW: /* load/store/move word with sign extension; move 32-bit literals  */
2079                         opset(AMOVWZ, r0) /* Same as above, but zero extended */
2080
2081                 case AVCLZLSBB:
2082                         opset(AVCTZLSBB, r0)
2083
2084                 case AADD,
2085                         AADDIS,
2086                         AANDCC, /* and. Rb,Rs,Ra; andi. $uimm,Rs,Ra */
2087                         AANDISCC,
2088                         AFMOVSX,
2089                         AFMOVSZ,
2090                         ALSW,
2091                         AMOVD,  /* load/store/move 64-bit values, including 32-bit literals with/without sign-extension */
2092                         AMOVB,  /* macro: move byte with sign extension */
2093                         AMOVBU, /* macro: move byte with sign extension & update */
2094                         AMOVFL,
2095                         /* op $s[,r2],r3; op r1[,r2],r3; no cc/v */
2096                         ASUBC, /* op r1,$s,r3; op r1[,r2],r3 */
2097                         ASTSW,
2098                         ASLBMTE,
2099                         AWORD,
2100                         ADWORD,
2101                         ADARN,
2102                         AVMSUMUDM,
2103                         AADDEX,
2104                         ACMPEQB,
2105                         ACLRLSLWI,
2106                         AMTVSRDD,
2107                         APNOP,
2108                         AISEL,
2109                         ASETB,
2110                         obj.ANOP,
2111                         obj.ATEXT,
2112                         obj.AUNDEF,
2113                         obj.AFUNCDATA,
2114                         obj.APCALIGN,
2115                         obj.APCDATA,
2116                         obj.ADUFFZERO,
2117                         obj.ADUFFCOPY:
2118                         break
2119                 }
2120         }
2121 }
2122
2123 func OPVXX1(o uint32, xo uint32, oe uint32) uint32 {
2124         return o<<26 | xo<<1 | oe<<11
2125 }
2126
2127 func OPVXX2(o uint32, xo uint32, oe uint32) uint32 {
2128         return o<<26 | xo<<2 | oe<<11
2129 }
2130
2131 func OPVXX2VA(o uint32, xo uint32, oe uint32) uint32 {
2132         return o<<26 | xo<<2 | oe<<16
2133 }
2134
2135 func OPVXX3(o uint32, xo uint32, oe uint32) uint32 {
2136         return o<<26 | xo<<3 | oe<<11
2137 }
2138
2139 func OPVXX4(o uint32, xo uint32, oe uint32) uint32 {
2140         return o<<26 | xo<<4 | oe<<11
2141 }
2142
2143 func OPDQ(o uint32, xo uint32, oe uint32) uint32 {
2144         return o<<26 | xo | oe<<4
2145 }
2146
2147 func OPVX(o uint32, xo uint32, oe uint32, rc uint32) uint32 {
2148         return o<<26 | xo | oe<<11 | rc&1
2149 }
2150
2151 func OPVC(o uint32, xo uint32, oe uint32, rc uint32) uint32 {
2152         return o<<26 | xo | oe<<11 | (rc&1)<<10
2153 }
2154
2155 func OPVCC(o uint32, xo uint32, oe uint32, rc uint32) uint32 {
2156         return o<<26 | xo<<1 | oe<<10 | rc&1
2157 }
2158
2159 func OPCC(o uint32, xo uint32, rc uint32) uint32 {
2160         return OPVCC(o, xo, 0, rc)
2161 }
2162
2163 /* Generate MD-form opcode */
2164 func OPMD(o, xo, rc uint32) uint32 {
2165         return o<<26 | xo<<2 | rc&1
2166 }
2167
2168 /* the order is dest, a/s, b/imm for both arithmetic and logical operations. */
2169 func AOP_RRR(op uint32, d uint32, a uint32, b uint32) uint32 {
2170         return op | (d&31)<<21 | (a&31)<<16 | (b&31)<<11
2171 }
2172
2173 /* VX-form 2-register operands, r/none/r */
2174 func AOP_RR(op uint32, d uint32, a uint32) uint32 {
2175         return op | (d&31)<<21 | (a&31)<<11
2176 }
2177
2178 /* VA-form 4-register operands */
2179 func AOP_RRRR(op uint32, d uint32, a uint32, b uint32, c uint32) uint32 {
2180         return op | (d&31)<<21 | (a&31)<<16 | (b&31)<<11 | (c&31)<<6
2181 }
2182
2183 func AOP_IRR(op uint32, d uint32, a uint32, simm uint32) uint32 {
2184         return op | (d&31)<<21 | (a&31)<<16 | simm&0xFFFF
2185 }
2186
2187 /* VX-form 2-register + UIM operands */
2188 func AOP_VIRR(op uint32, d uint32, a uint32, simm uint32) uint32 {
2189         return op | (d&31)<<21 | (simm&0xFFFF)<<16 | (a&31)<<11
2190 }
2191
2192 /* VX-form 2-register + ST + SIX operands */
2193 func AOP_IIRR(op uint32, d uint32, a uint32, sbit uint32, simm uint32) uint32 {
2194         return op | (d&31)<<21 | (a&31)<<16 | (sbit&1)<<15 | (simm&0xF)<<11
2195 }
2196
2197 /* VA-form 3-register + SHB operands */
2198 func AOP_IRRR(op uint32, d uint32, a uint32, b uint32, simm uint32) uint32 {
2199         return op | (d&31)<<21 | (a&31)<<16 | (b&31)<<11 | (simm&0xF)<<6
2200 }
2201
2202 /* VX-form 1-register + SIM operands */
2203 func AOP_IR(op uint32, d uint32, simm uint32) uint32 {
2204         return op | (d&31)<<21 | (simm&31)<<16
2205 }
2206
2207 /* XX1-form 3-register operands, 1 VSR operand */
2208 func AOP_XX1(op uint32, r uint32, a uint32, b uint32) uint32 {
2209         return op | (r&31)<<21 | (a&31)<<16 | (b&31)<<11 | (r&32)>>5
2210 }
2211
2212 /* XX2-form 3-register operands, 2 VSR operands */
2213 func AOP_XX2(op uint32, xt uint32, a uint32, xb uint32) uint32 {
2214         return op | (xt&31)<<21 | (a&3)<<16 | (xb&31)<<11 | (xb&32)>>4 | (xt&32)>>5
2215 }
2216
2217 /* XX3-form 3 VSR operands */
2218 func AOP_XX3(op uint32, xt uint32, xa uint32, xb uint32) uint32 {
2219         return op | (xt&31)<<21 | (xa&31)<<16 | (xb&31)<<11 | (xa&32)>>3 | (xb&32)>>4 | (xt&32)>>5
2220 }
2221
2222 /* XX3-form 3 VSR operands + immediate */
2223 func AOP_XX3I(op uint32, xt uint32, xa uint32, xb uint32, c uint32) uint32 {
2224         return op | (xt&31)<<21 | (xa&31)<<16 | (xb&31)<<11 | (c&3)<<8 | (xa&32)>>3 | (xb&32)>>4 | (xt&32)>>5
2225 }
2226
2227 /* XX4-form, 4 VSR operands */
2228 func AOP_XX4(op uint32, xt uint32, xa uint32, xb uint32, xc uint32) uint32 {
2229         return op | (xt&31)<<21 | (xa&31)<<16 | (xb&31)<<11 | (xc&31)<<6 | (xc&32)>>2 | (xa&32)>>3 | (xb&32)>>4 | (xt&32)>>5
2230 }
2231
2232 /* DQ-form, VSR register, register + offset operands */
2233 func AOP_DQ(op uint32, xt uint32, a uint32, b uint32) uint32 {
2234         /* The EA for this instruction form is (RA) + DQ << 4, where DQ is a 12-bit signed integer. */
2235         /* In order to match the output of the GNU objdump (and make the usage in Go asm easier), the */
2236         /* instruction is called using the sign extended value (i.e. a valid offset would be -32752 or 32752, */
2237         /* not -2047 or 2047), so 'b' needs to be adjusted to the expected 12-bit DQ value. Bear in mind that */
2238         /* bits 0 to 3 in 'dq' need to be zero, otherwise this will generate an illegal instruction. */
2239         /* If in doubt how this instruction form is encoded, refer to ISA 3.0b, pages 492 and 507. */
2240         dq := b >> 4
2241         return op | (xt&31)<<21 | (a&31)<<16 | (dq&4095)<<4 | (xt&32)>>2
2242 }
2243
2244 /* Z23-form, 3-register operands + CY field */
2245 func AOP_Z23I(op uint32, d uint32, a uint32, b uint32, c uint32) uint32 {
2246         return op | (d&31)<<21 | (a&31)<<16 | (b&31)<<11 | (c&3)<<9
2247 }
2248
2249 /* X-form, 3-register operands + EH field */
2250 func AOP_RRRI(op uint32, d uint32, a uint32, b uint32, c uint32) uint32 {
2251         return op | (d&31)<<21 | (a&31)<<16 | (b&31)<<11 | (c & 1)
2252 }
2253
2254 func LOP_RRR(op uint32, a uint32, s uint32, b uint32) uint32 {
2255         return op | (s&31)<<21 | (a&31)<<16 | (b&31)<<11
2256 }
2257
2258 func LOP_IRR(op uint32, a uint32, s uint32, uimm uint32) uint32 {
2259         return op | (s&31)<<21 | (a&31)<<16 | uimm&0xFFFF
2260 }
2261
2262 func OP_BR(op uint32, li uint32, aa uint32) uint32 {
2263         return op | li&0x03FFFFFC | aa<<1
2264 }
2265
2266 func OP_BC(op uint32, bo uint32, bi uint32, bd uint32, aa uint32) uint32 {
2267         return op | (bo&0x1F)<<21 | (bi&0x1F)<<16 | bd&0xFFFC | aa<<1
2268 }
2269
2270 func OP_BCR(op uint32, bo uint32, bi uint32) uint32 {
2271         return op | (bo&0x1F)<<21 | (bi&0x1F)<<16
2272 }
2273
2274 func OP_RLW(op uint32, a uint32, s uint32, sh uint32, mb uint32, me uint32) uint32 {
2275         return op | (s&31)<<21 | (a&31)<<16 | (sh&31)<<11 | (mb&31)<<6 | (me&31)<<1
2276 }
2277
2278 func AOP_RLDIC(op uint32, a uint32, s uint32, sh uint32, m uint32) uint32 {
2279         return op | (s&31)<<21 | (a&31)<<16 | (sh&31)<<11 | ((sh&32)>>5)<<1 | (m&31)<<6 | ((m&32)>>5)<<5
2280 }
2281
2282 func AOP_EXTSWSLI(op uint32, a uint32, s uint32, sh uint32) uint32 {
2283         return op | (a&31)<<21 | (s&31)<<16 | (sh&31)<<11 | ((sh&32)>>5)<<1
2284 }
2285
2286 func AOP_ISEL(op uint32, t uint32, a uint32, b uint32, bc uint32) uint32 {
2287         return op | (t&31)<<21 | (a&31)<<16 | (b&31)<<11 | (bc&0x1F)<<6
2288 }
2289
2290 func AOP_PFX_00_8LS(r, ie uint32) uint32 {
2291         return 1<<26 | 0<<24 | 0<<23 | (r&1)<<20 | (ie & 0x3FFFF)
2292 }
2293 func AOP_PFX_10_MLS(r, ie uint32) uint32 {
2294         return 1<<26 | 2<<24 | 0<<23 | (r&1)<<20 | (ie & 0x3FFFF)
2295 }
2296
2297 const (
2298         /* each rhs is OPVCC(_, _, _, _) */
2299         OP_ADD      = 31<<26 | 266<<1 | 0<<10 | 0
2300         OP_ADDI     = 14<<26 | 0<<1 | 0<<10 | 0
2301         OP_ADDIS    = 15<<26 | 0<<1 | 0<<10 | 0
2302         OP_ANDI     = 28<<26 | 0<<1 | 0<<10 | 0
2303         OP_EXTSB    = 31<<26 | 954<<1 | 0<<10 | 0
2304         OP_EXTSH    = 31<<26 | 922<<1 | 0<<10 | 0
2305         OP_EXTSW    = 31<<26 | 986<<1 | 0<<10 | 0
2306         OP_ISEL     = 31<<26 | 15<<1 | 0<<10 | 0
2307         OP_MCRF     = 19<<26 | 0<<1 | 0<<10 | 0
2308         OP_MCRFS    = 63<<26 | 64<<1 | 0<<10 | 0
2309         OP_MCRXR    = 31<<26 | 512<<1 | 0<<10 | 0
2310         OP_MFCR     = 31<<26 | 19<<1 | 0<<10 | 0
2311         OP_MFFS     = 63<<26 | 583<<1 | 0<<10 | 0
2312         OP_MFSPR    = 31<<26 | 339<<1 | 0<<10 | 0
2313         OP_MFSR     = 31<<26 | 595<<1 | 0<<10 | 0
2314         OP_MFSRIN   = 31<<26 | 659<<1 | 0<<10 | 0
2315         OP_MTCRF    = 31<<26 | 144<<1 | 0<<10 | 0
2316         OP_MTFSF    = 63<<26 | 711<<1 | 0<<10 | 0
2317         OP_MTFSFI   = 63<<26 | 134<<1 | 0<<10 | 0
2318         OP_MTSPR    = 31<<26 | 467<<1 | 0<<10 | 0
2319         OP_MTSR     = 31<<26 | 210<<1 | 0<<10 | 0
2320         OP_MTSRIN   = 31<<26 | 242<<1 | 0<<10 | 0
2321         OP_MULLW    = 31<<26 | 235<<1 | 0<<10 | 0
2322         OP_MULLD    = 31<<26 | 233<<1 | 0<<10 | 0
2323         OP_OR       = 31<<26 | 444<<1 | 0<<10 | 0
2324         OP_ORI      = 24<<26 | 0<<1 | 0<<10 | 0
2325         OP_ORIS     = 25<<26 | 0<<1 | 0<<10 | 0
2326         OP_RLWINM   = 21<<26 | 0<<1 | 0<<10 | 0
2327         OP_RLWNM    = 23<<26 | 0<<1 | 0<<10 | 0
2328         OP_SUBF     = 31<<26 | 40<<1 | 0<<10 | 0
2329         OP_RLDIC    = 30<<26 | 4<<1 | 0<<10 | 0
2330         OP_RLDICR   = 30<<26 | 2<<1 | 0<<10 | 0
2331         OP_RLDICL   = 30<<26 | 0<<1 | 0<<10 | 0
2332         OP_RLDCL    = 30<<26 | 8<<1 | 0<<10 | 0
2333         OP_EXTSWSLI = 31<<26 | 445<<2
2334         OP_SETB     = 31<<26 | 128<<1
2335 )
2336
2337 func pfxadd(rt, ra int16, r uint32, imm32 int64) (uint32, uint32) {
2338         return AOP_PFX_10_MLS(r, uint32(imm32>>16)), AOP_IRR(14<<26, uint32(rt), uint32(ra), uint32(imm32))
2339 }
2340
2341 func pfxload(a obj.As, reg int16, base int16, r uint32) (uint32, uint32) {
2342         switch a {
2343         case AMOVH:
2344                 return AOP_PFX_10_MLS(r, 0), AOP_IRR(42<<26, uint32(reg), uint32(base), 0)
2345         case AMOVW:
2346                 return AOP_PFX_00_8LS(r, 0), AOP_IRR(41<<26, uint32(reg), uint32(base), 0)
2347         case AMOVD:
2348                 return AOP_PFX_00_8LS(r, 0), AOP_IRR(57<<26, uint32(reg), uint32(base), 0)
2349         case AMOVBZ, AMOVB:
2350                 return AOP_PFX_10_MLS(r, 0), AOP_IRR(34<<26, uint32(reg), uint32(base), 0)
2351         case AMOVHZ:
2352                 return AOP_PFX_10_MLS(r, 0), AOP_IRR(40<<26, uint32(reg), uint32(base), 0)
2353         case AMOVWZ:
2354                 return AOP_PFX_10_MLS(r, 0), AOP_IRR(32<<26, uint32(reg), uint32(base), 0)
2355         case AFMOVS:
2356                 return AOP_PFX_10_MLS(r, 0), AOP_IRR(48<<26, uint32(reg), uint32(base), 0)
2357         case AFMOVD:
2358                 return AOP_PFX_10_MLS(r, 0), AOP_IRR(50<<26, uint32(reg), uint32(base), 0)
2359         }
2360         log.Fatalf("Error no pfxload for %v\n", a)
2361         return 0, 0
2362 }
2363
2364 func pfxstore(a obj.As, reg int16, base int16, r uint32) (uint32, uint32) {
2365         switch a {
2366         case AMOVD:
2367                 return AOP_PFX_00_8LS(r, 0), AOP_IRR(61<<26, uint32(reg), uint32(base), 0)
2368         case AMOVBZ, AMOVB:
2369                 return AOP_PFX_10_MLS(r, 0), AOP_IRR(38<<26, uint32(reg), uint32(base), 0)
2370         case AMOVHZ, AMOVH:
2371                 return AOP_PFX_10_MLS(r, 0), AOP_IRR(44<<26, uint32(reg), uint32(base), 0)
2372         case AMOVWZ, AMOVW:
2373                 return AOP_PFX_10_MLS(r, 0), AOP_IRR(36<<26, uint32(reg), uint32(base), 0)
2374         case AFMOVS:
2375                 return AOP_PFX_10_MLS(r, 0), AOP_IRR(52<<26, uint32(reg), uint32(base), 0)
2376         case AFMOVD:
2377                 return AOP_PFX_10_MLS(r, 0), AOP_IRR(54<<26, uint32(reg), uint32(base), 0)
2378         }
2379         log.Fatalf("Error no pfxstore for %v\n", a)
2380         return 0, 0
2381 }
2382
2383 func oclass(a *obj.Addr) int {
2384         return int(a.Class) - 1
2385 }
2386
2387 const (
2388         D_FORM = iota
2389         DS_FORM
2390 )
2391
2392 // This function determines when a non-indexed load or store is D or
2393 // DS form for use in finding the size of the offset field in the instruction.
2394 // The size is needed when setting the offset value in the instruction
2395 // and when generating relocation for that field.
2396 // DS form instructions include: ld, ldu, lwa, std, stdu.  All other
2397 // loads and stores with an offset field are D form.  This function should
2398 // only be called with the same opcodes as are handled by opstore and opload.
2399 func (c *ctxt9) opform(insn uint32) int {
2400         switch insn {
2401         default:
2402                 c.ctxt.Diag("bad insn in loadform: %x", insn)
2403         case OPVCC(58, 0, 0, 0), // ld
2404                 OPVCC(58, 0, 0, 1),        // ldu
2405                 OPVCC(58, 0, 0, 0) | 1<<1, // lwa
2406                 OPVCC(62, 0, 0, 0),        // std
2407                 OPVCC(62, 0, 0, 1):        //stdu
2408                 return DS_FORM
2409         case OP_ADDI, // add
2410                 OPVCC(32, 0, 0, 0), // lwz
2411                 OPVCC(33, 0, 0, 0), // lwzu
2412                 OPVCC(34, 0, 0, 0), // lbz
2413                 OPVCC(35, 0, 0, 0), // lbzu
2414                 OPVCC(40, 0, 0, 0), // lhz
2415                 OPVCC(41, 0, 0, 0), // lhzu
2416                 OPVCC(42, 0, 0, 0), // lha
2417                 OPVCC(43, 0, 0, 0), // lhau
2418                 OPVCC(46, 0, 0, 0), // lmw
2419                 OPVCC(48, 0, 0, 0), // lfs
2420                 OPVCC(49, 0, 0, 0), // lfsu
2421                 OPVCC(50, 0, 0, 0), // lfd
2422                 OPVCC(51, 0, 0, 0), // lfdu
2423                 OPVCC(36, 0, 0, 0), // stw
2424                 OPVCC(37, 0, 0, 0), // stwu
2425                 OPVCC(38, 0, 0, 0), // stb
2426                 OPVCC(39, 0, 0, 0), // stbu
2427                 OPVCC(44, 0, 0, 0), // sth
2428                 OPVCC(45, 0, 0, 0), // sthu
2429                 OPVCC(47, 0, 0, 0), // stmw
2430                 OPVCC(52, 0, 0, 0), // stfs
2431                 OPVCC(53, 0, 0, 0), // stfsu
2432                 OPVCC(54, 0, 0, 0), // stfd
2433                 OPVCC(55, 0, 0, 0): // stfdu
2434                 return D_FORM
2435         }
2436         return 0
2437 }
2438
2439 // Encode instructions and create relocation for accessing s+d according to the
2440 // instruction op with source or destination (as appropriate) register reg.
2441 func (c *ctxt9) symbolAccess(s *obj.LSym, d int64, reg int16, op uint32, reuse bool) (o1, o2 uint32, rel *obj.Reloc) {
2442         if c.ctxt.Headtype == objabi.Haix {
2443                 // Every symbol access must be made via a TOC anchor.
2444                 c.ctxt.Diag("symbolAccess called for %s", s.Name)
2445         }
2446         var base uint32
2447         form := c.opform(op)
2448         if c.ctxt.Flag_shared {
2449                 base = REG_R2
2450         } else {
2451                 base = REG_R0
2452         }
2453         // If reg can be reused when computing the symbol address,
2454         // use it instead of REGTMP.
2455         if !reuse {
2456                 o1 = AOP_IRR(OP_ADDIS, REGTMP, base, 0)
2457                 o2 = AOP_IRR(op, uint32(reg), REGTMP, 0)
2458         } else {
2459                 o1 = AOP_IRR(OP_ADDIS, uint32(reg), base, 0)
2460                 o2 = AOP_IRR(op, uint32(reg), uint32(reg), 0)
2461         }
2462         rel = obj.Addrel(c.cursym)
2463         rel.Off = int32(c.pc)
2464         rel.Siz = 8
2465         rel.Sym = s
2466         rel.Add = d
2467         if c.ctxt.Flag_shared {
2468                 switch form {
2469                 case D_FORM:
2470                         rel.Type = objabi.R_ADDRPOWER_TOCREL
2471                 case DS_FORM:
2472                         rel.Type = objabi.R_ADDRPOWER_TOCREL_DS
2473                 }
2474
2475         } else {
2476                 switch form {
2477                 case D_FORM:
2478                         rel.Type = objabi.R_ADDRPOWER
2479                 case DS_FORM:
2480                         rel.Type = objabi.R_ADDRPOWER_DS
2481                 }
2482         }
2483         return
2484 }
2485
2486 /*
2487  * 32-bit masks
2488  */
2489 func getmask(m *[2]uint32, v uint32) bool {
2490         m[1] = 0
2491         m[0] = 0
2492         if v != ^uint32(0) && v&(1<<31) != 0 && v&1 != 0 { /* MB > ME */
2493                 if getmask(m, ^v) {
2494                         i := m[0]
2495                         m[0] = m[1] + 1
2496                         m[1] = i - 1
2497                         return true
2498                 }
2499
2500                 return false
2501         }
2502
2503         for i := 0; i < 32; i++ {
2504                 if v&(1<<uint(31-i)) != 0 {
2505                         m[0] = uint32(i)
2506                         for {
2507                                 m[1] = uint32(i)
2508                                 i++
2509                                 if i >= 32 || v&(1<<uint(31-i)) == 0 {
2510                                         break
2511                                 }
2512                         }
2513
2514                         for ; i < 32; i++ {
2515                                 if v&(1<<uint(31-i)) != 0 {
2516                                         return false
2517                                 }
2518                         }
2519                         return true
2520                 }
2521         }
2522
2523         return false
2524 }
2525
2526 func (c *ctxt9) maskgen(p *obj.Prog, v uint32) (mb, me uint32) {
2527         var m [2]uint32
2528         if !getmask(&m, v) {
2529                 c.ctxt.Diag("cannot generate mask #%x\n%v", v, p)
2530         }
2531         return m[0], m[1]
2532 }
2533
2534 /*
2535  * 64-bit masks (rldic etc)
2536  */
2537 func getmask64(m []byte, v uint64) bool {
2538         m[1] = 0
2539         m[0] = m[1]
2540         for i := 0; i < 64; i++ {
2541                 if v&(uint64(1)<<uint(63-i)) != 0 {
2542                         m[0] = byte(i)
2543                         for {
2544                                 m[1] = byte(i)
2545                                 i++
2546                                 if i >= 64 || v&(uint64(1)<<uint(63-i)) == 0 {
2547                                         break
2548                                 }
2549                         }
2550
2551                         for ; i < 64; i++ {
2552                                 if v&(uint64(1)<<uint(63-i)) != 0 {
2553                                         return false
2554                                 }
2555                         }
2556                         return true
2557                 }
2558         }
2559
2560         return false
2561 }
2562
2563 func (c *ctxt9) maskgen64(p *obj.Prog, m []byte, v uint64) {
2564         if !getmask64(m, v) {
2565                 c.ctxt.Diag("cannot generate mask #%x\n%v", v, p)
2566         }
2567 }
2568
2569 func loadu32(r int, d int64) uint32 {
2570         v := int32(d >> 16)
2571         if isuint32(uint64(d)) {
2572                 return LOP_IRR(OP_ORIS, uint32(r), REGZERO, uint32(v))
2573         }
2574         return AOP_IRR(OP_ADDIS, uint32(r), REGZERO, uint32(v))
2575 }
2576
2577 func high16adjusted(d int32) uint16 {
2578         if d&0x8000 != 0 {
2579                 return uint16((d >> 16) + 1)
2580         }
2581         return uint16(d >> 16)
2582 }
2583
2584 func asmout(c *ctxt9, p *obj.Prog, o *Optab, out *[5]uint32) {
2585         o1 := uint32(0)
2586         o2 := uint32(0)
2587         o3 := uint32(0)
2588         o4 := uint32(0)
2589         o5 := uint32(0)
2590
2591         //print("%v => case %d\n", p, o->type);
2592         switch o.type_ {
2593         default:
2594                 c.ctxt.Diag("unknown type %d", o.type_)
2595                 prasm(p)
2596
2597         case 0: /* pseudo ops */
2598                 break
2599
2600         case 2: /* int/cr/fp op Rb,[Ra],Rd */
2601                 r := int(p.Reg)
2602
2603                 if r == 0 {
2604                         r = int(p.To.Reg)
2605                 }
2606                 o1 = AOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), uint32(r), uint32(p.From.Reg))
2607
2608         case 3: /* mov $soreg/addcon/andcon/ucon, r ==> addis/oris/addi/ori $i,reg',r */
2609                 d := c.vregoff(&p.From)
2610
2611                 v := int32(d)
2612                 r := int(p.From.Reg)
2613                 if r == 0 {
2614                         r = c.getimpliedreg(&p.From, p)
2615                 }
2616                 if r0iszero != 0 /*TypeKind(100016)*/ && p.To.Reg == 0 && (r != 0 || v != 0) {
2617                         c.ctxt.Diag("literal operation on R0\n%v", p)
2618                 }
2619                 a := OP_ADDI
2620                 if o.a1 == C_UCON {
2621                         if d&0xffff != 0 {
2622                                 log.Fatalf("invalid handling of %v", p)
2623                         }
2624                         // For UCON operands the value is right shifted 16, using ADDIS if the
2625                         // value should be signed, ORIS if unsigned.
2626                         v >>= 16
2627                         if r == REGZERO && isuint32(uint64(d)) {
2628                                 o1 = LOP_IRR(OP_ORIS, uint32(p.To.Reg), REGZERO, uint32(v))
2629                                 break
2630                         }
2631
2632                         a = OP_ADDIS
2633                 } else if int64(int16(d)) != d {
2634                         // Operand is 16 bit value with sign bit set
2635                         if o.a1 == C_ANDCON {
2636                                 // Needs unsigned 16 bit so use ORI
2637                                 if r == 0 || r == REGZERO {
2638                                         o1 = LOP_IRR(uint32(OP_ORI), uint32(p.To.Reg), uint32(0), uint32(v))
2639                                         break
2640                                 }
2641                                 // With ADDCON, needs signed 16 bit value, fall through to use ADDI
2642                         } else if o.a1 != C_ADDCON {
2643                                 log.Fatalf("invalid handling of %v", p)
2644                         }
2645                 }
2646
2647                 o1 = AOP_IRR(uint32(a), uint32(p.To.Reg), uint32(r), uint32(v))
2648
2649         case 4: /* add/mul $scon,[r1],r2 */
2650                 v := c.regoff(&p.From)
2651
2652                 r := int(p.Reg)
2653                 if r == 0 {
2654                         r = int(p.To.Reg)
2655                 }
2656                 if r0iszero != 0 /*TypeKind(100016)*/ && p.To.Reg == 0 {
2657                         c.ctxt.Diag("literal operation on R0\n%v", p)
2658                 }
2659                 if int32(int16(v)) != v {
2660                         log.Fatalf("mishandled instruction %v", p)
2661                 }
2662                 o1 = AOP_IRR(c.opirr(p.As), uint32(p.To.Reg), uint32(r), uint32(v))
2663
2664         case 5: /* syscall */
2665                 o1 = c.oprrr(p.As)
2666
2667         case 6: /* logical op Rb,[Rs,]Ra; no literal */
2668                 r := int(p.Reg)
2669
2670                 if r == 0 {
2671                         r = int(p.To.Reg)
2672                 }
2673                 // AROTL and AROTLW are extended mnemonics, which map to RLDCL and RLWNM.
2674                 switch p.As {
2675                 case AROTL:
2676                         o1 = AOP_RLDIC(OP_RLDCL, uint32(p.To.Reg), uint32(r), uint32(p.From.Reg), uint32(0))
2677                 case AROTLW:
2678                         o1 = OP_RLW(OP_RLWNM, uint32(p.To.Reg), uint32(r), uint32(p.From.Reg), 0, 31)
2679                 default:
2680                         if p.As == AOR && p.From.Type == obj.TYPE_CONST && p.From.Offset == 0 {
2681                                 // Compile "OR $0, Rx, Ry" into ori. If Rx == Ry == 0, this is the preferred
2682                                 // hardware no-op. This happens because $0 matches C_REG before C_ZCON.
2683                                 o1 = LOP_IRR(OP_ORI, uint32(p.To.Reg), uint32(r), 0)
2684                         } else {
2685                                 o1 = LOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), uint32(r), uint32(p.From.Reg))
2686                         }
2687                 }
2688
2689         case 7: /* mov r, soreg ==> stw o(r) */
2690                 r := int(p.To.Reg)
2691
2692                 if r == 0 {
2693                         r = c.getimpliedreg(&p.To, p)
2694                 }
2695                 v := c.regoff(&p.To)
2696                 if int32(int16(v)) != v {
2697                         log.Fatalf("mishandled instruction %v", p)
2698                 }
2699                 // Offsets in DS form stores must be a multiple of 4
2700                 inst := c.opstore(p.As)
2701                 if c.opform(inst) == DS_FORM && v&0x3 != 0 {
2702                         log.Fatalf("invalid offset for DS form load/store %v", p)
2703                 }
2704                 o1 = AOP_IRR(inst, uint32(p.From.Reg), uint32(r), uint32(v))
2705
2706         case 8: /* mov soreg, r ==> lbz/lhz/lwz o(r), lbz o(r) + extsb r,r */
2707                 r := int(p.From.Reg)
2708
2709                 if r == 0 {
2710                         r = c.getimpliedreg(&p.From, p)
2711                 }
2712                 v := c.regoff(&p.From)
2713                 if int32(int16(v)) != v {
2714                         log.Fatalf("mishandled instruction %v", p)
2715                 }
2716                 // Offsets in DS form loads must be a multiple of 4
2717                 inst := c.opload(p.As)
2718                 if c.opform(inst) == DS_FORM && v&0x3 != 0 {
2719                         log.Fatalf("invalid offset for DS form load/store %v", p)
2720                 }
2721                 o1 = AOP_IRR(inst, uint32(p.To.Reg), uint32(r), uint32(v))
2722
2723                 // Sign extend MOVB operations. This is ignored for other cases (o.size == 4).
2724                 o2 = LOP_RRR(OP_EXTSB, uint32(p.To.Reg), uint32(p.To.Reg), 0)
2725
2726         case 10: /* sub Ra,[Rb],Rd => subf Rd,Ra,Rb */
2727                 r := int(p.Reg)
2728
2729                 if r == 0 {
2730                         r = int(p.To.Reg)
2731                 }
2732                 o1 = AOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), uint32(p.From.Reg), uint32(r))
2733
2734         case 11: /* br/bl lbra */
2735                 v := int32(0)
2736
2737                 if p.To.Target() != nil {
2738                         v = int32(p.To.Target().Pc - p.Pc)
2739                         if v&03 != 0 {
2740                                 c.ctxt.Diag("odd branch target address\n%v", p)
2741                                 v &^= 03
2742                         }
2743
2744                         if v < -(1<<25) || v >= 1<<24 {
2745                                 c.ctxt.Diag("branch too far\n%v", p)
2746                         }
2747                 }
2748
2749                 o1 = OP_BR(c.opirr(p.As), uint32(v), 0)
2750                 if p.To.Sym != nil {
2751                         rel := obj.Addrel(c.cursym)
2752                         rel.Off = int32(c.pc)
2753                         rel.Siz = 4
2754                         rel.Sym = p.To.Sym
2755                         v += int32(p.To.Offset)
2756                         if v&03 != 0 {
2757                                 c.ctxt.Diag("odd branch target address\n%v", p)
2758                                 v &^= 03
2759                         }
2760
2761                         rel.Add = int64(v)
2762                         rel.Type = objabi.R_CALLPOWER
2763                 }
2764                 o2 = 0x60000000 // nop, sometimes overwritten by ld r2, 24(r1) when dynamic linking
2765
2766         case 13: /* mov[bhwd]{z,} r,r */
2767                 // This needs to handle "MOV* $0, Rx".  This shows up because $0 also
2768                 // matches C_REG if r0iszero. This happens because C_REG sorts before C_ANDCON
2769                 // TODO: fix the above behavior and cleanup this exception.
2770                 if p.From.Type == obj.TYPE_CONST {
2771                         o1 = LOP_IRR(OP_ADDI, REGZERO, uint32(p.To.Reg), 0)
2772                         break
2773                 }
2774                 if p.To.Type == obj.TYPE_CONST {
2775                         c.ctxt.Diag("cannot move into constant 0\n%v", p)
2776                 }
2777
2778                 switch p.As {
2779                 case AMOVB:
2780                         o1 = LOP_RRR(OP_EXTSB, uint32(p.To.Reg), uint32(p.From.Reg), 0)
2781                 case AMOVBZ:
2782                         o1 = OP_RLW(OP_RLWINM, uint32(p.To.Reg), uint32(p.From.Reg), 0, 24, 31)
2783                 case AMOVH:
2784                         o1 = LOP_RRR(OP_EXTSH, uint32(p.To.Reg), uint32(p.From.Reg), 0)
2785                 case AMOVHZ:
2786                         o1 = OP_RLW(OP_RLWINM, uint32(p.To.Reg), uint32(p.From.Reg), 0, 16, 31)
2787                 case AMOVW:
2788                         o1 = LOP_RRR(OP_EXTSW, uint32(p.To.Reg), uint32(p.From.Reg), 0)
2789                 case AMOVWZ:
2790                         o1 = OP_RLW(OP_RLDIC, uint32(p.To.Reg), uint32(p.From.Reg), 0, 0, 0) | 1<<5 /* MB=32 */
2791                 case AMOVD:
2792                         o1 = LOP_RRR(OP_OR, uint32(p.To.Reg), uint32(p.From.Reg), uint32(p.From.Reg))
2793                 default:
2794                         c.ctxt.Diag("internal: bad register move/truncation\n%v", p)
2795                 }
2796
2797         case 14: /* rldc[lr] Rb,Rs,$mask,Ra -- left, right give different masks */
2798                 r := int(p.Reg)
2799
2800                 if r == 0 {
2801                         r = int(p.To.Reg)
2802                 }
2803                 d := c.vregoff(p.GetFrom3())
2804                 var a int
2805                 switch p.As {
2806
2807                 // These opcodes expect a mask operand that has to be converted into the
2808                 // appropriate operand.  The way these were defined, not all valid masks are possible.
2809                 // Left here for compatibility in case they were used or generated.
2810                 case ARLDCL, ARLDCLCC:
2811                         var mask [2]uint8
2812                         c.maskgen64(p, mask[:], uint64(d))
2813
2814                         a = int(mask[0]) /* MB */
2815                         if mask[1] != 63 {
2816                                 c.ctxt.Diag("invalid mask for rotate: %x (end != bit 63)\n%v", uint64(d), p)
2817                         }
2818                         o1 = LOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), uint32(r), uint32(p.From.Reg))
2819                         o1 |= (uint32(a) & 31) << 6
2820                         if a&0x20 != 0 {
2821                                 o1 |= 1 << 5 /* mb[5] is top bit */
2822                         }
2823
2824                 case ARLDCR, ARLDCRCC:
2825                         var mask [2]uint8
2826                         c.maskgen64(p, mask[:], uint64(d))
2827
2828                         a = int(mask[1]) /* ME */
2829                         if mask[0] != 0 {
2830                                 c.ctxt.Diag("invalid mask for rotate: %x %x (start != 0)\n%v", uint64(d), mask[0], p)
2831                         }
2832                         o1 = LOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), uint32(r), uint32(p.From.Reg))
2833                         o1 |= (uint32(a) & 31) << 6
2834                         if a&0x20 != 0 {
2835                                 o1 |= 1 << 5 /* mb[5] is top bit */
2836                         }
2837
2838                 // These opcodes use a shift count like the ppc64 asm, no mask conversion done
2839                 case ARLDICR, ARLDICRCC:
2840                         me := int(d)
2841                         sh := c.regoff(&p.From)
2842                         if me < 0 || me > 63 || sh > 63 {
2843                                 c.ctxt.Diag("Invalid me or sh for RLDICR: %x %x\n%v", int(d), sh, p)
2844                         }
2845                         o1 = AOP_RLDIC(c.oprrr(p.As), uint32(p.To.Reg), uint32(r), uint32(sh), uint32(me))
2846
2847                 case ARLDICL, ARLDICLCC, ARLDIC, ARLDICCC:
2848                         mb := int(d)
2849                         sh := c.regoff(&p.From)
2850                         if mb < 0 || mb > 63 || sh > 63 {
2851                                 c.ctxt.Diag("Invalid mb or sh for RLDIC, RLDICL: %x %x\n%v", mb, sh, p)
2852                         }
2853                         o1 = AOP_RLDIC(c.oprrr(p.As), uint32(p.To.Reg), uint32(r), uint32(sh), uint32(mb))
2854
2855                 case ACLRLSLDI:
2856                         // This is an extended mnemonic defined in the ISA section C.8.1
2857                         // clrlsldi ra,rs,b,n --> rldic ra,rs,n,b-n
2858                         // It maps onto RLDIC so is directly generated here based on the operands from
2859                         // the clrlsldi.
2860                         n := int32(d)
2861                         b := c.regoff(&p.From)
2862                         if n > b || b > 63 {
2863                                 c.ctxt.Diag("Invalid n or b for CLRLSLDI: %x %x\n%v", n, b, p)
2864                         }
2865                         o1 = AOP_RLDIC(OP_RLDIC, uint32(p.To.Reg), uint32(r), uint32(n), uint32(b)-uint32(n))
2866
2867                 default:
2868                         c.ctxt.Diag("unexpected op in rldc case\n%v", p)
2869                         a = 0
2870                 }
2871
2872         case 17, /* bc bo,bi,lbra (same for now) */
2873                 16: /* bc bo,bi,sbra */
2874                 a := 0
2875
2876                 r := int(p.Reg)
2877
2878                 if p.From.Type == obj.TYPE_CONST {
2879                         a = int(c.regoff(&p.From))
2880                 } else if p.From.Type == obj.TYPE_REG {
2881                         if r != 0 {
2882                                 c.ctxt.Diag("unexpected register setting for branch with CR: %d\n", r)
2883                         }
2884                         // BI values for the CR
2885                         switch p.From.Reg {
2886                         case REG_CR0:
2887                                 r = BI_CR0
2888                         case REG_CR1:
2889                                 r = BI_CR1
2890                         case REG_CR2:
2891                                 r = BI_CR2
2892                         case REG_CR3:
2893                                 r = BI_CR3
2894                         case REG_CR4:
2895                                 r = BI_CR4
2896                         case REG_CR5:
2897                                 r = BI_CR5
2898                         case REG_CR6:
2899                                 r = BI_CR6
2900                         case REG_CR7:
2901                                 r = BI_CR7
2902                         default:
2903                                 c.ctxt.Diag("unrecognized register: expecting CR\n")
2904                         }
2905                 }
2906                 v := int32(0)
2907                 if p.To.Target() != nil {
2908                         v = int32(p.To.Target().Pc - p.Pc)
2909                 }
2910                 if v&03 != 0 {
2911                         c.ctxt.Diag("odd branch target address\n%v", p)
2912                         v &^= 03
2913                 }
2914
2915                 if v < -(1<<16) || v >= 1<<15 {
2916                         c.ctxt.Diag("branch too far\n%v", p)
2917                 }
2918                 o1 = OP_BC(c.opirr(p.As), uint32(a), uint32(r), uint32(v), 0)
2919
2920         case 18: /* br/bl (lr/ctr); bc/bcl bo,bi,(lr/ctr) */
2921                 var v int32
2922                 var bh uint32 = 0
2923                 if p.As == ABC || p.As == ABCL {
2924                         v = c.regoff(&p.From) & 31
2925                 } else {
2926                         v = 20 /* unconditional */
2927                 }
2928                 r := int(p.Reg)
2929                 if r == 0 {
2930                         r = 0
2931                 }
2932                 switch oclass(&p.To) {
2933                 case C_CTR:
2934                         o1 = OPVCC(19, 528, 0, 0)
2935
2936                 case C_LR:
2937                         o1 = OPVCC(19, 16, 0, 0)
2938
2939                 default:
2940                         c.ctxt.Diag("bad optab entry (18): %d\n%v", p.To.Class, p)
2941                         v = 0
2942                 }
2943
2944                 // Insert optional branch hint for bclr[l]/bcctr[l]
2945                 if p.From3Type() != obj.TYPE_NONE {
2946                         bh = uint32(p.GetFrom3().Offset)
2947                         if bh == 2 || bh > 3 {
2948                                 log.Fatalf("BH must be 0,1,3 for %v", p)
2949                         }
2950                         o1 |= bh << 11
2951                 }
2952
2953                 if p.As == ABL || p.As == ABCL {
2954                         o1 |= 1
2955                 }
2956                 o1 = OP_BCR(o1, uint32(v), uint32(r))
2957
2958         case 19: /* mov $lcon,r ==> cau+or */
2959                 d := c.vregoff(&p.From)
2960                 if o.ispfx {
2961                         o1, o2 = pfxadd(p.To.Reg, REG_R0, PFX_R_ABS, d)
2962                 } else {
2963                         o1 = loadu32(int(p.To.Reg), d)
2964                         o2 = LOP_IRR(OP_ORI, uint32(p.To.Reg), uint32(p.To.Reg), uint32(int32(d)))
2965                 }
2966
2967         case 20: /* add $ucon,,r | addis $addcon,r,r */
2968                 v := c.regoff(&p.From)
2969
2970                 r := int(p.Reg)
2971                 if r == 0 {
2972                         r = int(p.To.Reg)
2973                 }
2974                 if p.As == AADD && (r0iszero == 0 /*TypeKind(100016)*/ && p.Reg == 0 || r0iszero != 0 /*TypeKind(100016)*/ && p.To.Reg == 0) {
2975                         c.ctxt.Diag("literal operation on R0\n%v", p)
2976                 }
2977                 if p.As == AADDIS {
2978                         o1 = AOP_IRR(c.opirr(p.As), uint32(p.To.Reg), uint32(r), uint32(v))
2979                 } else {
2980                         o1 = AOP_IRR(c.opirr(AADDIS), uint32(p.To.Reg), uint32(r), uint32(v)>>16)
2981                 }
2982
2983         case 22: /* add $lcon/$andcon,r1,r2 ==> oris+ori+add/ori+add, add $s34con,r1 ==> addis+ori+slw+ori+add */
2984                 if p.To.Reg == REGTMP || p.Reg == REGTMP {
2985                         c.ctxt.Diag("can't synthesize large constant\n%v", p)
2986                 }
2987                 d := c.vregoff(&p.From)
2988                 r := int(p.Reg)
2989                 if r == 0 {
2990                         r = int(p.To.Reg)
2991                 }
2992                 if p.From.Sym != nil {
2993                         c.ctxt.Diag("%v is not supported", p)
2994                 }
2995                 if o.ispfx {
2996                         o1, o2 = pfxadd(int16(p.To.Reg), int16(r), PFX_R_ABS, d)
2997                 } else if o.size == 8 {
2998                         o1 = LOP_IRR(OP_ORI, REGTMP, REGZERO, uint32(int32(d)))          // tmp = uint16(d)
2999                         o2 = AOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), REGTMP, uint32(r)) // to = tmp + from
3000                 } else if o.size == 12 {
3001                         // Note, o1 is ADDIS if d is negative, ORIS otherwise.
3002                         o1 = loadu32(REGTMP, d)                                          // tmp = d & 0xFFFF0000
3003                         o2 = LOP_IRR(OP_ORI, REGTMP, REGTMP, uint32(int32(d)))           // tmp |= d & 0xFFFF
3004                         o3 = AOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), REGTMP, uint32(r)) // to = from + tmp
3005                 } else {
3006                         // For backwards compatibility with GOPPC64 < 10, generate 34b constants in register.
3007                         o1 = LOP_IRR(OP_ADDIS, REGZERO, REGTMP, uint32(d>>32))  // tmp = sign_extend((d>>32)&0xFFFF0000)
3008                         o2 = LOP_IRR(OP_ORI, REGTMP, REGTMP, uint32(d>>16))     // tmp |= (d>>16)&0xFFFF
3009                         o3 = AOP_RLDIC(OP_RLDICR, REGTMP, REGTMP, 16, 63-16)    // tmp <<= 16
3010                         o4 = LOP_IRR(OP_ORI, REGTMP, REGTMP, uint32(uint16(d))) // tmp |= d&0xFFFF
3011                         o5 = AOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), REGTMP, uint32(r))
3012                 }
3013
3014         case 23: /* and $lcon/$addcon,r1,r2 ==> oris+ori+and/addi+and */
3015                 if p.To.Reg == REGTMP || p.Reg == REGTMP {
3016                         c.ctxt.Diag("can't synthesize large constant\n%v", p)
3017                 }
3018                 d := c.vregoff(&p.From)
3019                 r := int(p.Reg)
3020                 if r == 0 {
3021                         r = int(p.To.Reg)
3022                 }
3023
3024                 // With ADDCON operand, generate 2 instructions using ADDI for signed value,
3025                 // with LCON operand generate 3 instructions.
3026                 if o.size == 8 {
3027                         o1 = LOP_IRR(OP_ADDI, REGZERO, REGTMP, uint32(int32(d)))
3028                         o2 = LOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), REGTMP, uint32(r))
3029                 } else {
3030                         o1 = loadu32(REGTMP, d)
3031                         o2 = LOP_IRR(OP_ORI, REGTMP, REGTMP, uint32(int32(d)))
3032                         o3 = LOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), REGTMP, uint32(r))
3033                 }
3034                 if p.From.Sym != nil {
3035                         c.ctxt.Diag("%v is not supported", p)
3036                 }
3037
3038         case 24: /* lfd fA,float64(0) -> xxlxor xsA,xsaA,xsaA + fneg for -0 */
3039                 o1 = AOP_XX3I(c.oprrr(AXXLXOR), uint32(p.To.Reg), uint32(p.To.Reg), uint32(p.To.Reg), uint32(0))
3040                 // This is needed for -0.
3041                 if o.size == 8 {
3042                         o2 = AOP_RRR(c.oprrr(AFNEG), uint32(p.To.Reg), 0, uint32(p.To.Reg))
3043                 }
3044
3045         case 25:
3046                 /* sld[.] $sh,rS,rA -> rldicr[.] $sh,rS,mask(0,63-sh),rA; srd[.] -> rldicl */
3047                 v := c.regoff(&p.From)
3048
3049                 if v < 0 {
3050                         v = 0
3051                 } else if v > 63 {
3052                         v = 63
3053                 }
3054                 r := int(p.Reg)
3055                 if r == 0 {
3056                         r = int(p.To.Reg)
3057                 }
3058                 var a int
3059                 op := uint32(0)
3060                 switch p.As {
3061                 case ASLD, ASLDCC:
3062                         a = int(63 - v)
3063                         op = OP_RLDICR
3064
3065                 case ASRD, ASRDCC:
3066                         a = int(v)
3067                         v = 64 - v
3068                         op = OP_RLDICL
3069                 case AROTL:
3070                         a = int(0)
3071                         op = OP_RLDICL
3072                 case AEXTSWSLI, AEXTSWSLICC:
3073                         a = int(v)
3074                 default:
3075                         c.ctxt.Diag("unexpected op in sldi case\n%v", p)
3076                         a = 0
3077                         o1 = 0
3078                 }
3079
3080                 if p.As == AEXTSWSLI || p.As == AEXTSWSLICC {
3081                         o1 = AOP_EXTSWSLI(OP_EXTSWSLI, uint32(r), uint32(p.To.Reg), uint32(v))
3082
3083                 } else {
3084                         o1 = AOP_RLDIC(op, uint32(p.To.Reg), uint32(r), uint32(v), uint32(a))
3085                 }
3086                 if p.As == ASLDCC || p.As == ASRDCC || p.As == AEXTSWSLICC {
3087                         o1 |= 1 // Set the condition code bit
3088                 }
3089
3090         case 26: /* mov $lsext/auto/oreg,,r2 ==> addis+addi */
3091                 v := c.vregoff(&p.From)
3092                 r := int(p.From.Reg)
3093                 var rel *obj.Reloc
3094
3095                 switch p.From.Name {
3096                 case obj.NAME_EXTERN, obj.NAME_STATIC:
3097                         // Load a 32 bit constant, or relocation depending on if a symbol is attached
3098                         o1, o2, rel = c.symbolAccess(p.From.Sym, v, p.To.Reg, OP_ADDI, true)
3099                 default:
3100                         if r == 0 {
3101                                 r = c.getimpliedreg(&p.From, p)
3102                         }
3103                         // Add a 32 bit offset to a register.
3104                         o1 = AOP_IRR(OP_ADDIS, uint32(p.To.Reg), uint32(r), uint32(high16adjusted(int32(v))))
3105                         o2 = AOP_IRR(OP_ADDI, uint32(p.To.Reg), uint32(p.To.Reg), uint32(v))
3106                 }
3107
3108                 if o.ispfx {
3109                         if rel == nil {
3110                                 o1, o2 = pfxadd(int16(p.To.Reg), int16(r), PFX_R_ABS, v)
3111                         } else {
3112                                 o1, o2 = pfxadd(int16(p.To.Reg), REG_R0, PFX_R_PCREL, 0)
3113                                 rel.Type = objabi.R_ADDRPOWER_PCREL34
3114                         }
3115                 }
3116
3117         case 27: /* subc ra,$simm,rd => subfic rd,ra,$simm */
3118                 v := c.regoff(p.GetFrom3())
3119
3120                 r := int(p.From.Reg)
3121                 o1 = AOP_IRR(c.opirr(p.As), uint32(p.To.Reg), uint32(r), uint32(v))
3122
3123         case 28: /* subc r1,$lcon,r2 ==> cau+or+subfc */
3124                 if p.To.Reg == REGTMP || p.From.Reg == REGTMP {
3125                         c.ctxt.Diag("can't synthesize large constant\n%v", p)
3126                 }
3127                 v := c.regoff(p.GetFrom3())
3128                 o1 = AOP_IRR(OP_ADDIS, REGTMP, REGZERO, uint32(v)>>16)
3129                 o2 = LOP_IRR(OP_ORI, REGTMP, REGTMP, uint32(v))
3130                 o3 = AOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), uint32(p.From.Reg), REGTMP)
3131                 if p.From.Sym != nil {
3132                         c.ctxt.Diag("%v is not supported", p)
3133                 }
3134
3135         case 29: /* rldic[lr]? $sh,s,$mask,a -- left, right, plain give different masks */
3136                 v := c.regoff(&p.From)
3137
3138                 d := c.vregoff(p.GetFrom3())
3139                 var mask [2]uint8
3140                 c.maskgen64(p, mask[:], uint64(d))
3141                 var a int
3142                 switch p.As {
3143                 case ARLDC, ARLDCCC:
3144                         a = int(mask[0]) /* MB */
3145                         if int32(mask[1]) != (63 - v) {
3146                                 c.ctxt.Diag("invalid mask for shift: %x %x (shift %d)\n%v", uint64(d), mask[1], v, p)
3147                         }
3148
3149                 case ARLDCL, ARLDCLCC:
3150                         a = int(mask[0]) /* MB */
3151                         if mask[1] != 63 {
3152                                 c.ctxt.Diag("invalid mask for shift: %x %s (shift %d)\n%v", uint64(d), mask[1], v, p)
3153                         }
3154
3155                 case ARLDCR, ARLDCRCC:
3156                         a = int(mask[1]) /* ME */
3157                         if mask[0] != 0 {
3158                                 c.ctxt.Diag("invalid mask for shift: %x %x (shift %d)\n%v", uint64(d), mask[0], v, p)
3159                         }
3160
3161                 default:
3162                         c.ctxt.Diag("unexpected op in rldic case\n%v", p)
3163                         a = 0
3164                 }
3165
3166                 o1 = AOP_RRR(c.opirr(p.As), uint32(p.Reg), uint32(p.To.Reg), (uint32(v) & 0x1F))
3167                 o1 |= (uint32(a) & 31) << 6
3168                 if v&0x20 != 0 {
3169                         o1 |= 1 << 1
3170                 }
3171                 if a&0x20 != 0 {
3172                         o1 |= 1 << 5 /* mb[5] is top bit */
3173                 }
3174
3175         case 30: /* rldimi $sh,s,$mask,a */
3176                 v := c.regoff(&p.From)
3177
3178                 d := c.vregoff(p.GetFrom3())
3179
3180                 // Original opcodes had mask operands which had to be converted to a shift count as expected by
3181                 // the ppc64 asm.
3182                 switch p.As {
3183                 case ARLDMI, ARLDMICC:
3184                         var mask [2]uint8
3185                         c.maskgen64(p, mask[:], uint64(d))
3186                         if int32(mask[1]) != (63 - v) {
3187                                 c.ctxt.Diag("invalid mask for shift: %x %x (shift %d)\n%v", uint64(d), mask[1], v, p)
3188                         }
3189                         o1 = AOP_RRR(c.opirr(p.As), uint32(p.Reg), uint32(p.To.Reg), (uint32(v) & 0x1F))
3190                         o1 |= (uint32(mask[0]) & 31) << 6
3191                         if v&0x20 != 0 {
3192                                 o1 |= 1 << 1
3193                         }
3194                         if mask[0]&0x20 != 0 {
3195                                 o1 |= 1 << 5 /* mb[5] is top bit */
3196                         }
3197
3198                 // Opcodes with shift count operands.
3199                 case ARLDIMI, ARLDIMICC:
3200                         o1 = AOP_RRR(c.opirr(p.As), uint32(p.Reg), uint32(p.To.Reg), (uint32(v) & 0x1F))
3201                         o1 |= (uint32(d) & 31) << 6
3202                         if d&0x20 != 0 {
3203                                 o1 |= 1 << 5
3204                         }
3205                         if v&0x20 != 0 {
3206                                 o1 |= 1 << 1
3207                         }
3208                 }
3209
3210         case 31: /* dword */
3211                 d := c.vregoff(&p.From)
3212
3213                 if c.ctxt.Arch.ByteOrder == binary.BigEndian {
3214                         o1 = uint32(d >> 32)
3215                         o2 = uint32(d)
3216                 } else {
3217                         o1 = uint32(d)
3218                         o2 = uint32(d >> 32)
3219                 }
3220
3221                 if p.From.Sym != nil {
3222                         rel := obj.Addrel(c.cursym)
3223                         rel.Off = int32(c.pc)
3224                         rel.Siz = 8
3225                         rel.Sym = p.From.Sym
3226                         rel.Add = p.From.Offset
3227                         rel.Type = objabi.R_ADDR
3228                         o2 = 0
3229                         o1 = o2
3230                 }
3231
3232         case 32: /* fmul frc,fra,frd */
3233                 r := int(p.Reg)
3234
3235                 if r == 0 {
3236                         r = int(p.To.Reg)
3237                 }
3238                 o1 = AOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), uint32(r), 0) | (uint32(p.From.Reg)&31)<<6
3239
3240         case 33: /* fabs [frb,]frd; fmr. frb,frd */
3241                 r := int(p.From.Reg)
3242
3243                 if oclass(&p.From) == C_NONE {
3244                         r = int(p.To.Reg)
3245                 }
3246                 o1 = AOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), 0, uint32(r))
3247
3248         case 34: /* FMADDx fra,frb,frc,frt (t=a*c±b) */
3249                 o1 = AOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), uint32(p.From.Reg), uint32(p.Reg)) | (uint32(p.GetFrom3().Reg)&31)<<6
3250
3251         case 35: /* mov r,lext/lauto/loreg ==> cau $(v>>16),sb,r'; store o(r') */
3252                 v := c.regoff(&p.To)
3253
3254                 r := int(p.To.Reg)
3255                 if r == 0 {
3256                         r = c.getimpliedreg(&p.To, p)
3257                 }
3258                 // Offsets in DS form stores must be a multiple of 4
3259                 if o.ispfx {
3260                         o1, o2 = pfxstore(p.As, p.From.Reg, int16(r), PFX_R_ABS)
3261                         o1 |= uint32((v >> 16) & 0x3FFFF)
3262                         o2 |= uint32(v & 0xFFFF)
3263                 } else {
3264                         inst := c.opstore(p.As)
3265                         if c.opform(inst) == DS_FORM && v&0x3 != 0 {
3266                                 log.Fatalf("invalid offset for DS form load/store %v", p)
3267                         }
3268                         o1 = AOP_IRR(OP_ADDIS, REGTMP, uint32(r), uint32(high16adjusted(v)))
3269                         o2 = AOP_IRR(inst, uint32(p.From.Reg), REGTMP, uint32(v))
3270                 }
3271
3272         case 36: /* mov b/bz/h/hz lext/lauto/lreg,r ==> lbz+extsb/lbz/lha/lhz etc */
3273                 v := c.regoff(&p.From)
3274
3275                 r := int(p.From.Reg)
3276                 if r == 0 {
3277                         r = c.getimpliedreg(&p.From, p)
3278                 }
3279
3280                 if o.ispfx {
3281                         o1, o2 = pfxload(p.As, p.To.Reg, int16(r), PFX_R_ABS)
3282                         o1 |= uint32((v >> 16) & 0x3FFFF)
3283                         o2 |= uint32(v & 0xFFFF)
3284                 } else {
3285                         if o.a6 == C_REG {
3286                                 // Reuse the base register when loading a GPR (C_REG) to avoid
3287                                 // using REGTMP (R31) when possible.
3288                                 o1 = AOP_IRR(OP_ADDIS, uint32(p.To.Reg), uint32(r), uint32(high16adjusted(v)))
3289                                 o2 = AOP_IRR(c.opload(p.As), uint32(p.To.Reg), uint32(p.To.Reg), uint32(v))
3290                         } else {
3291                                 o1 = AOP_IRR(OP_ADDIS, uint32(REGTMP), uint32(r), uint32(high16adjusted(v)))
3292                                 o2 = AOP_IRR(c.opload(p.As), uint32(p.To.Reg), uint32(REGTMP), uint32(v))
3293                         }
3294                 }
3295
3296                 // Sign extend MOVB if needed
3297                 o3 = LOP_RRR(OP_EXTSB, uint32(p.To.Reg), uint32(p.To.Reg), 0)
3298
3299         case 40: /* word */
3300                 o1 = uint32(c.regoff(&p.From))
3301
3302         case 41: /* stswi */
3303                 if p.To.Type == obj.TYPE_MEM && p.To.Index == 0 && p.To.Offset != 0 {
3304                         c.ctxt.Diag("Invalid addressing mode used in index type instruction: %v", p.As)
3305                 }
3306
3307                 o1 = AOP_RRR(c.opirr(p.As), uint32(p.From.Reg), uint32(p.To.Reg), 0) | (uint32(c.regoff(p.GetFrom3()))&0x7F)<<11
3308
3309         case 42: /* lswi */
3310                 if p.From.Type == obj.TYPE_MEM && p.From.Index == 0 && p.From.Offset != 0 {
3311                         c.ctxt.Diag("Invalid addressing mode used in index type instruction: %v", p.As)
3312                 }
3313                 o1 = AOP_RRR(c.opirr(p.As), uint32(p.To.Reg), uint32(p.From.Reg), 0) | (uint32(c.regoff(p.GetFrom3()))&0x7F)<<11
3314
3315         case 43: /* data cache instructions: op (Ra+[Rb]), [th|l] */
3316                 /* TH field for dcbt/dcbtst: */
3317                 /* 0 = Block access - program will soon access EA. */
3318                 /* 8-15 = Stream access - sequence of access (data stream). See section 4.3.2 of the ISA for details. */
3319                 /* 16 = Block access - program will soon make a transient access to EA. */
3320                 /* 17 = Block access - program will not access EA for a long time. */
3321
3322                 /* L field for dcbf: */
3323                 /* 0 = invalidates the block containing EA in all processors. */
3324                 /* 1 = same as 0, but with limited scope (i.e. block in the current processor will not be reused soon). */
3325                 /* 3 = same as 1, but with even more limited scope (i.e. block in the current processor primary cache will not be reused soon). */
3326                 if p.To.Type == obj.TYPE_NONE {
3327                         o1 = AOP_RRR(c.oprrr(p.As), 0, uint32(p.From.Index), uint32(p.From.Reg))
3328                 } else {
3329                         th := c.regoff(&p.To)
3330                         o1 = AOP_RRR(c.oprrr(p.As), uint32(th), uint32(p.From.Index), uint32(p.From.Reg))
3331                 }
3332
3333         case 44: /* indexed store */
3334                 o1 = AOP_RRR(c.opstorex(p.As), uint32(p.From.Reg), uint32(p.To.Index), uint32(p.To.Reg))
3335
3336         case 45: /* indexed load */
3337                 switch p.As {
3338                 /* The assembler accepts a 4-operand l*arx instruction. The fourth operand is an Exclusive Access Hint (EH) */
3339                 /* The EH field can be used as a lock acquire/release hint as follows: */
3340                 /* 0 = Atomic Update (fetch-and-operate or similar algorithm) */
3341                 /* 1 = Exclusive Access (lock acquire and release) */
3342                 case ALBAR, ALHAR, ALWAR, ALDAR:
3343                         if p.From3Type() != obj.TYPE_NONE {
3344                                 eh := int(c.regoff(p.GetFrom3()))
3345                                 if eh > 1 {
3346                                         c.ctxt.Diag("illegal EH field\n%v", p)
3347                                 }
3348                                 o1 = AOP_RRRI(c.oploadx(p.As), uint32(p.To.Reg), uint32(p.From.Index), uint32(p.From.Reg), uint32(eh))
3349                         } else {
3350                                 o1 = AOP_RRR(c.oploadx(p.As), uint32(p.To.Reg), uint32(p.From.Index), uint32(p.From.Reg))
3351                         }
3352                 default:
3353                         o1 = AOP_RRR(c.oploadx(p.As), uint32(p.To.Reg), uint32(p.From.Index), uint32(p.From.Reg))
3354                 }
3355         case 46: /* plain op */
3356                 o1 = c.oprrr(p.As)
3357
3358         case 47: /* op Ra, Rd; also op [Ra,] Rd */
3359                 r := int(p.From.Reg)
3360
3361                 if r == 0 {
3362                         r = int(p.To.Reg)
3363                 }
3364                 o1 = AOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), uint32(r), 0)
3365
3366         case 48: /* op Rs, Ra */
3367                 r := int(p.From.Reg)
3368
3369                 if r == 0 {
3370                         r = int(p.To.Reg)
3371                 }
3372                 o1 = LOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), uint32(r), 0)
3373
3374         case 49: /* op Rb; op $n, Rb */
3375                 if p.From.Type != obj.TYPE_REG { /* tlbie $L, rB */
3376                         v := c.regoff(&p.From) & 1
3377                         o1 = AOP_RRR(c.oprrr(p.As), 0, 0, uint32(p.To.Reg)) | uint32(v)<<21
3378                 } else {
3379                         o1 = AOP_RRR(c.oprrr(p.As), 0, 0, uint32(p.From.Reg))
3380                 }
3381
3382         case 50: /* rem[u] r1[,r2],r3 */
3383                 r := int(p.Reg)
3384
3385                 if r == 0 {
3386                         r = int(p.To.Reg)
3387                 }
3388                 v := c.oprrr(p.As)
3389                 t := v & (1<<10 | 1) /* OE|Rc */
3390                 o1 = AOP_RRR(v&^t, REGTMP, uint32(r), uint32(p.From.Reg))
3391                 o2 = AOP_RRR(OP_MULLW, REGTMP, REGTMP, uint32(p.From.Reg))
3392                 o3 = AOP_RRR(OP_SUBF|t, uint32(p.To.Reg), REGTMP, uint32(r))
3393                 if p.As == AREMU {
3394                         o4 = o3
3395
3396                         /* Clear top 32 bits */
3397                         o3 = OP_RLW(OP_RLDIC, REGTMP, REGTMP, 0, 0, 0) | 1<<5
3398                 }
3399
3400         case 51: /* remd[u] r1[,r2],r3 */
3401                 r := int(p.Reg)
3402
3403                 if r == 0 {
3404                         r = int(p.To.Reg)
3405                 }
3406                 v := c.oprrr(p.As)
3407                 t := v & (1<<10 | 1) /* OE|Rc */
3408                 o1 = AOP_RRR(v&^t, REGTMP, uint32(r), uint32(p.From.Reg))
3409                 o2 = AOP_RRR(OP_MULLD, REGTMP, REGTMP, uint32(p.From.Reg))
3410                 o3 = AOP_RRR(OP_SUBF|t, uint32(p.To.Reg), REGTMP, uint32(r))
3411                 /* cases 50,51: removed; can be reused. */
3412
3413                 /* cases 50,51: removed; can be reused. */
3414
3415         case 52: /* mtfsbNx cr(n) */
3416                 v := c.regoff(&p.From) & 31
3417
3418                 o1 = AOP_RRR(c.oprrr(p.As), uint32(v), 0, 0)
3419
3420         case 53: /* mffsX ,fr1 */
3421                 o1 = AOP_RRR(OP_MFFS, uint32(p.To.Reg), 0, 0)
3422
3423         case 55: /* op Rb, Rd */
3424                 o1 = AOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), 0, uint32(p.From.Reg))
3425
3426         case 56: /* sra $sh,[s,]a; srd $sh,[s,]a */
3427                 v := c.regoff(&p.From)
3428
3429                 r := int(p.Reg)
3430                 if r == 0 {
3431                         r = int(p.To.Reg)
3432                 }
3433                 o1 = AOP_RRR(c.opirr(p.As), uint32(r), uint32(p.To.Reg), uint32(v)&31)
3434                 if (p.As == ASRAD || p.As == ASRADCC) && (v&0x20 != 0) {
3435                         o1 |= 1 << 1 /* mb[5] */
3436                 }
3437
3438         case 57: /* slw $sh,[s,]a -> rlwinm ... */
3439                 v := c.regoff(&p.From)
3440
3441                 r := int(p.Reg)
3442                 if r == 0 {
3443                         r = int(p.To.Reg)
3444                 }
3445
3446                 /*
3447                          * Let user (gs) shoot himself in the foot.
3448                          * qc has already complained.
3449                          *
3450                         if(v < 0 || v > 31)
3451                                 ctxt->diag("illegal shift %ld\n%v", v, p);
3452                 */
3453                 if v < 0 {
3454                         v = 0
3455                 } else if v > 32 {
3456                         v = 32
3457                 }
3458                 var mask [2]uint8
3459                 switch p.As {
3460                 case AROTLW:
3461                         mask[0], mask[1] = 0, 31
3462                 case ASRW, ASRWCC:
3463                         mask[0], mask[1] = uint8(v), 31
3464                         v = 32 - v
3465                 default:
3466                         mask[0], mask[1] = 0, uint8(31-v)
3467                 }
3468                 o1 = OP_RLW(OP_RLWINM, uint32(p.To.Reg), uint32(r), uint32(v), uint32(mask[0]), uint32(mask[1]))
3469                 if p.As == ASLWCC || p.As == ASRWCC {
3470                         o1 |= 1 // set the condition code
3471                 }
3472
3473         case 58: /* logical $andcon,[s],a */
3474                 v := c.regoff(&p.From)
3475
3476                 r := int(p.Reg)
3477                 if r == 0 {
3478                         r = int(p.To.Reg)
3479                 }
3480                 o1 = LOP_IRR(c.opirr(p.As), uint32(p.To.Reg), uint32(r), uint32(v))
3481
3482         case 59: /* or/xor/and $ucon,,r | oris/xoris/andis $addcon,r,r */
3483                 v := c.regoff(&p.From)
3484
3485                 r := int(p.Reg)
3486                 if r == 0 {
3487                         r = int(p.To.Reg)
3488                 }
3489                 switch p.As {
3490                 case AOR:
3491                         o1 = LOP_IRR(c.opirr(AORIS), uint32(p.To.Reg), uint32(r), uint32(v)>>16) /* oris, xoris, andis. */
3492                 case AXOR:
3493                         o1 = LOP_IRR(c.opirr(AXORIS), uint32(p.To.Reg), uint32(r), uint32(v)>>16)
3494                 case AANDCC:
3495                         o1 = LOP_IRR(c.opirr(AANDISCC), uint32(p.To.Reg), uint32(r), uint32(v)>>16)
3496                 default:
3497                         o1 = LOP_IRR(c.opirr(p.As), uint32(p.To.Reg), uint32(r), uint32(v))
3498                 }
3499
3500         case 60: /* tw to,a,b */
3501                 r := int(c.regoff(&p.From) & 31)
3502
3503                 o1 = AOP_RRR(c.oprrr(p.As), uint32(r), uint32(p.Reg), uint32(p.To.Reg))
3504
3505         case 61: /* tw to,a,$simm */
3506                 r := int(c.regoff(&p.From) & 31)
3507
3508                 v := c.regoff(&p.To)
3509                 o1 = AOP_IRR(c.opirr(p.As), uint32(r), uint32(p.Reg), uint32(v))
3510
3511         case 62: /* clrlslwi $sh,s,$mask,a */
3512                 v := c.regoff(&p.From)
3513                 n := c.regoff(p.GetFrom3())
3514                 // This is an extended mnemonic described in the ISA C.8.2
3515                 // clrlslwi ra,rs,b,n -> rlwinm ra,rs,n,b-n,31-n
3516                 // It maps onto rlwinm which is directly generated here.
3517                 if n > v || v >= 32 {
3518                         c.ctxt.Diag("Invalid n or b for CLRLSLWI: %x %x\n%v", v, n, p)
3519                 }
3520
3521                 o1 = OP_RLW(OP_RLWINM, uint32(p.To.Reg), uint32(p.Reg), uint32(n), uint32(v-n), uint32(31-n))
3522
3523         case 63: /* rlwimi/rlwnm/rlwinm [$sh,b],s,[$mask or mb,me],a*/
3524                 var mb, me uint32
3525                 if len(p.RestArgs) == 1 { // Mask needs decomposed into mb and me.
3526                         mb, me = c.maskgen(p, uint32(p.RestArgs[0].Addr.Offset))
3527                 } else { // Otherwise, mask is already passed as mb and me in RestArgs.
3528                         mb, me = uint32(p.RestArgs[0].Addr.Offset), uint32(p.RestArgs[1].Addr.Offset)
3529                 }
3530                 if p.From.Type == obj.TYPE_CONST {
3531                         o1 = OP_RLW(c.opirr(p.As), uint32(p.To.Reg), uint32(p.Reg), uint32(p.From.Offset), mb, me)
3532                 } else {
3533                         o1 = OP_RLW(c.oprrr(p.As), uint32(p.To.Reg), uint32(p.Reg), uint32(p.From.Reg), mb, me)
3534                 }
3535
3536         case 64: /* mtfsf fr[, $m] {,fpcsr} */
3537                 var v int32
3538                 if p.From3Type() != obj.TYPE_NONE {
3539                         v = c.regoff(p.GetFrom3()) & 255
3540                 } else {
3541                         v = 255
3542                 }
3543                 o1 = OP_MTFSF | uint32(v)<<17 | uint32(p.From.Reg)<<11
3544
3545         case 65: /* MOVFL $imm,FPSCR(n) => mtfsfi crfd,imm */
3546                 if p.To.Reg == 0 {
3547                         c.ctxt.Diag("must specify FPSCR(n)\n%v", p)
3548                 }
3549                 o1 = OP_MTFSFI | (uint32(p.To.Reg)&15)<<23 | (uint32(c.regoff(&p.From))&31)<<12
3550
3551         case 66: /* mov spr,r1; mov r1,spr */
3552                 var r int
3553                 var v int32
3554                 if REG_R0 <= p.From.Reg && p.From.Reg <= REG_R31 {
3555                         r = int(p.From.Reg)
3556                         v = int32(p.To.Reg)
3557                         o1 = OPVCC(31, 467, 0, 0) /* mtspr */
3558                 } else {
3559                         r = int(p.To.Reg)
3560                         v = int32(p.From.Reg)
3561                         o1 = OPVCC(31, 339, 0, 0) /* mfspr */
3562                 }
3563
3564                 o1 = AOP_RRR(o1, uint32(r), 0, 0) | (uint32(v)&0x1f)<<16 | ((uint32(v)>>5)&0x1f)<<11
3565
3566         case 67: /* mcrf crfD,crfS */
3567                 if p.From.Reg == REG_CR || p.To.Reg == REG_CR {
3568                         c.ctxt.Diag("CR argument must be a conditional register field (CR0-CR7)\n%v", p)
3569                 }
3570                 o1 = AOP_RRR(OP_MCRF, ((uint32(p.To.Reg) & 7) << 2), ((uint32(p.From.Reg) & 7) << 2), 0)
3571
3572         case 68: /* mfcr rD; mfocrf CRM,rD */
3573                 o1 = AOP_RRR(OP_MFCR, uint32(p.To.Reg), 0, 0) /*  form, whole register */
3574                 if p.From.Reg != REG_CR {
3575                         v := uint32(1) << uint(7-(p.From.Reg&7)) /* CR(n) */
3576                         o1 |= 1<<20 | v<<12                      /* new form, mfocrf */
3577                 }
3578
3579         case 69: /* mtcrf CRM,rS, mtocrf CRx,rS */
3580                 var v uint32
3581                 if p.To.Reg == REG_CR {
3582                         v = 0xff
3583                 } else if p.To.Offset != 0 { // MOVFL gpr, constant
3584                         v = uint32(p.To.Offset)
3585                 } else { // p.To.Reg == REG_CRx
3586                         v = 1 << uint(7-(p.To.Reg&7))
3587                 }
3588                 // Use mtocrf form if only one CR field moved.
3589                 if bits.OnesCount32(v) == 1 {
3590                         v |= 1 << 8
3591                 }
3592
3593                 o1 = AOP_RRR(OP_MTCRF, uint32(p.From.Reg), 0, 0) | uint32(v)<<12
3594
3595         case 70: /* [f]cmp r,r,cr*/
3596                 var r int
3597                 if p.Reg == 0 {
3598                         r = 0
3599                 } else {
3600                         r = (int(p.Reg) & 7) << 2
3601                 }
3602                 o1 = AOP_RRR(c.oprrr(p.As), uint32(r), uint32(p.From.Reg), uint32(p.To.Reg))
3603
3604         case 71: /* cmp[l] r,i,cr*/
3605                 var r int
3606                 if p.Reg == 0 {
3607                         r = 0
3608                 } else {
3609                         r = (int(p.Reg) & 7) << 2
3610                 }
3611                 o1 = AOP_RRR(c.opirr(p.As), uint32(r), uint32(p.From.Reg), 0) | uint32(c.regoff(&p.To))&0xffff
3612
3613         case 72: /* slbmte (Rb+Rs -> slb[Rb]) -> Rs, Rb */
3614                 o1 = AOP_RRR(c.oprrr(p.As), uint32(p.From.Reg), 0, uint32(p.To.Reg))
3615
3616         case 73: /* mcrfs crfD,crfS */
3617                 if p.From.Type != obj.TYPE_REG || p.From.Reg != REG_FPSCR || p.To.Type != obj.TYPE_REG || p.To.Reg < REG_CR0 || REG_CR7 < p.To.Reg {
3618                         c.ctxt.Diag("illegal FPSCR/CR field number\n%v", p)
3619                 }
3620                 o1 = AOP_RRR(OP_MCRFS, ((uint32(p.To.Reg) & 7) << 2), ((0 & 7) << 2), 0)
3621
3622         case 77: /* syscall $scon, syscall Rx */
3623                 if p.From.Type == obj.TYPE_CONST {
3624                         if p.From.Offset > BIG || p.From.Offset < -BIG {
3625                                 c.ctxt.Diag("illegal syscall, sysnum too large: %v", p)
3626                         }
3627                         o1 = AOP_IRR(OP_ADDI, REGZERO, REGZERO, uint32(p.From.Offset))
3628                 } else if p.From.Type == obj.TYPE_REG {
3629                         o1 = LOP_RRR(OP_OR, REGZERO, uint32(p.From.Reg), uint32(p.From.Reg))
3630                 } else {
3631                         c.ctxt.Diag("illegal syscall: %v", p)
3632                         o1 = 0x7fe00008 // trap always
3633                 }
3634
3635                 o2 = c.oprrr(p.As)
3636                 o3 = AOP_RRR(c.oprrr(AXOR), REGZERO, REGZERO, REGZERO) // XOR R0, R0
3637
3638         case 78: /* undef */
3639                 o1 = 0 /* "An instruction consisting entirely of binary 0s is guaranteed
3640                    always to be an illegal instruction."  */
3641
3642         /* relocation operations */
3643         case 74:
3644                 var rel *obj.Reloc
3645                 v := c.vregoff(&p.To)
3646                 // Offsets in DS form stores must be a multiple of 4
3647                 inst := c.opstore(p.As)
3648
3649                 // Can't reuse base for store instructions.
3650                 o1, o2, rel = c.symbolAccess(p.To.Sym, v, p.From.Reg, inst, false)
3651
3652                 // Rewrite as a prefixed store if supported.
3653                 if o.ispfx {
3654                         o1, o2 = pfxstore(p.As, p.From.Reg, REG_R0, PFX_R_PCREL)
3655                         rel.Type = objabi.R_ADDRPOWER_PCREL34
3656                 } else if c.opform(inst) == DS_FORM && v&0x3 != 0 {
3657                         log.Fatalf("invalid offset for DS form load/store %v", p)
3658                 }
3659
3660         case 75: // 32 bit offset symbol loads (got/toc/addr)
3661                 var rel *obj.Reloc
3662                 v := p.From.Offset
3663
3664                 // Offsets in DS form loads must be a multiple of 4
3665                 inst := c.opload(p.As)
3666                 switch p.From.Name {
3667                 case obj.NAME_GOTREF, obj.NAME_TOCREF:
3668                         if v != 0 {
3669                                 c.ctxt.Diag("invalid offset for GOT/TOC access %v", p)
3670                         }
3671                         o1 = AOP_IRR(OP_ADDIS, uint32(p.To.Reg), REG_R2, 0)
3672                         o2 = AOP_IRR(inst, uint32(p.To.Reg), uint32(p.To.Reg), 0)
3673                         rel = obj.Addrel(c.cursym)
3674                         rel.Off = int32(c.pc)
3675                         rel.Siz = 8
3676                         rel.Sym = p.From.Sym
3677                         switch p.From.Name {
3678                         case obj.NAME_GOTREF:
3679                                 rel.Type = objabi.R_ADDRPOWER_GOT
3680                         case obj.NAME_TOCREF:
3681                                 rel.Type = objabi.R_ADDRPOWER_TOCREL_DS
3682                         }
3683                 default:
3684                         reuseBaseReg := o.a6 == C_REG
3685                         // Reuse To.Reg as base register if it is a GPR.
3686                         o1, o2, rel = c.symbolAccess(p.From.Sym, v, p.To.Reg, inst, reuseBaseReg)
3687                 }
3688
3689                 // Convert to prefixed forms if supported.
3690                 if o.ispfx {
3691                         switch rel.Type {
3692                         case objabi.R_ADDRPOWER, objabi.R_ADDRPOWER_DS,
3693                                 objabi.R_ADDRPOWER_TOCREL, objabi.R_ADDRPOWER_TOCREL_DS:
3694                                 o1, o2 = pfxload(p.As, p.To.Reg, REG_R0, PFX_R_PCREL)
3695                                 rel.Type = objabi.R_ADDRPOWER_PCREL34
3696                         case objabi.R_POWER_TLS_IE:
3697                                 o1, o2 = pfxload(p.As, p.To.Reg, REG_R0, PFX_R_PCREL)
3698                                 rel.Type = objabi.R_POWER_TLS_IE_PCREL34
3699                         case objabi.R_ADDRPOWER_GOT:
3700                                 o1, o2 = pfxload(p.As, p.To.Reg, REG_R0, PFX_R_PCREL)
3701                                 rel.Type = objabi.R_ADDRPOWER_GOT_PCREL34
3702                         default:
3703                                 // We've failed to convert a TOC-relative relocation to a PC-relative one.
3704                                 log.Fatalf("Unable convert TOC-relative relocation %v to PC-relative", rel.Type)
3705                         }
3706                 } else if c.opform(inst) == DS_FORM && v&0x3 != 0 {
3707                         log.Fatalf("invalid offset for DS form load/store %v", p)
3708                 }
3709
3710                 o3 = LOP_RRR(OP_EXTSB, uint32(p.To.Reg), uint32(p.To.Reg), 0)
3711
3712         case 79:
3713                 if p.From.Offset != 0 {
3714                         c.ctxt.Diag("invalid offset against tls var %v", p)
3715                 }
3716                 rel := obj.Addrel(c.cursym)
3717                 rel.Off = int32(c.pc)
3718                 rel.Siz = 8
3719                 rel.Sym = p.From.Sym
3720                 if !o.ispfx {
3721                         o1 = AOP_IRR(OP_ADDIS, uint32(p.To.Reg), REG_R13, 0)
3722                         o2 = AOP_IRR(OP_ADDI, uint32(p.To.Reg), uint32(p.To.Reg), 0)
3723                         rel.Type = objabi.R_POWER_TLS_LE
3724                 } else {
3725                         o1, o2 = pfxadd(p.To.Reg, REG_R13, PFX_R_ABS, 0)
3726                         rel.Type = objabi.R_POWER_TLS_LE_TPREL34
3727                 }
3728
3729         case 80:
3730                 if p.From.Offset != 0 {
3731                         c.ctxt.Diag("invalid offset against tls var %v", p)
3732                 }
3733                 rel := obj.Addrel(c.cursym)
3734                 rel.Off = int32(c.pc)
3735                 rel.Siz = 8
3736                 rel.Sym = p.From.Sym
3737                 rel.Type = objabi.R_POWER_TLS_IE
3738                 if !o.ispfx {
3739                         o1 = AOP_IRR(OP_ADDIS, uint32(p.To.Reg), REG_R2, 0)
3740                         o2 = AOP_IRR(c.opload(AMOVD), uint32(p.To.Reg), uint32(p.To.Reg), 0)
3741                 } else {
3742                         o1, o2 = pfxload(p.As, p.To.Reg, REG_R0, PFX_R_PCREL)
3743                         rel.Type = objabi.R_POWER_TLS_IE_PCREL34
3744                 }
3745                 o3 = AOP_RRR(OP_ADD, uint32(p.To.Reg), uint32(p.To.Reg), REG_R13)
3746                 rel = obj.Addrel(c.cursym)
3747                 rel.Off = int32(c.pc) + 8
3748                 rel.Siz = 4
3749                 rel.Sym = p.From.Sym
3750                 rel.Type = objabi.R_POWER_TLS
3751
3752         case 82: /* vector instructions, VX-form and VC-form */
3753                 if p.From.Type == obj.TYPE_REG {
3754                         /* reg reg none OR reg reg reg */
3755                         /* 3-register operand order: VRA, VRB, VRT */
3756                         /* 2-register operand order: VRA, VRT */
3757                         o1 = AOP_RRR(c.oprrr(p.As), uint32(p.To.Reg), uint32(p.From.Reg), uint32(p.Reg))
3758                 } else if p.From3Type() == obj.TYPE_CONST {
3759                         /* imm imm reg reg */
3760                         /* operand order: SIX, VRA, ST, VRT */
3761                         six := int(c.regoff(&p.From))
3762                         st := int(c.regoff(p.GetFrom3()))
3763                         o1 = AOP_IIRR(c.opiirr(p.As), uint32(p.To.Reg), uint32(p.Reg), uint32(st), uint32(six))
3764                 } else if p.From3Type() == obj.TYPE_NONE && p.Reg != 0 {
3765                         /* imm reg reg */
3766                         /* operand order: UIM, VRB, VRT */
3767                         uim := int(c.regoff(&p.From))
3768                         o1 = AOP_VIRR(c.opirr(p.As), uint32(p.To.Reg), uint32(p.Reg), uint32(uim))
3769                 } else {
3770                         /* imm reg */
3771                         /* operand order: SIM, VRT */
3772                         sim := int(c.regoff(&p.From))
3773                         o1 = AOP_IR(c.opirr(p.As), uint32(p.To.Reg), uint32(sim))
3774                 }
3775
3776         case 83: /* vector instructions, VA-form */
3777                 if p.From.Type == obj.TYPE_REG {
3778                         /* reg reg reg reg */
3779                         /* 4-register operand order: VRA, VRB, VRC, VRT */
3780                         o1 = AOP_RRRR(c.oprrr(p.As), uint32(p.To.Reg), uint32(p.From.Reg), uint32(p.Reg), uint32(p.GetFrom3().Reg))
3781                 } else if p.From.Type == obj.TYPE_CONST {
3782                         /* imm reg reg reg */
3783                         /* operand order: SHB, VRA, VRB, VRT */
3784                         shb := int(c.regoff(&p.From))
3785                         o1 = AOP_IRRR(c.opirrr(p.As), uint32(p.To.Reg), uint32(p.Reg), uint32(p.GetFrom3().Reg), uint32(shb))
3786                 }
3787
3788         case 84: // ISEL BC,RA,RB,RT -> isel rt,ra,rb,bc
3789                 bc := c.vregoff(&p.From)
3790                 if o.a1 == C_CRBIT {
3791                         // CR bit is encoded as a register, not a constant.
3792                         bc = int64(p.From.Reg)
3793                 }
3794
3795                 // rt = To.Reg, ra = p.Reg, rb = p.From3.Reg
3796                 o1 = AOP_ISEL(OP_ISEL, uint32(p.To.Reg), uint32(p.Reg), uint32(p.GetFrom3().Reg), uint32(bc))
3797
3798         case 85: /* vector instructions, VX-form */
3799                 /* reg none reg */
3800                 /* 2-register operand order: VRB, VRT */
3801                 o1 = AOP_RR(c.oprrr(p.As), uint32(p.To.Reg), uint32(p.From.Reg))
3802
3803         case 86: /* VSX indexed store, XX1-form */
3804                 /* reg reg reg */
3805                 /* 3-register operand order: XT, (RB)(RA*1) */
3806                 o1 = AOP_XX1(c.opstorex(p.As), uint32(p.From.Reg), uint32(p.To.Index), uint32(p.To.Reg))
3807
3808         case 87: /* VSX indexed load, XX1-form */
3809                 /* reg reg reg */
3810                 /* 3-register operand order: (RB)(RA*1), XT */
3811                 o1 = AOP_XX1(c.oploadx(p.As), uint32(p.To.Reg), uint32(p.From.Index), uint32(p.From.Reg))
3812
3813         case 88: /* VSX mfvsr* instructions, XX1-form XS,RA */
3814                 o1 = AOP_XX1(c.oprrr(p.As), uint32(p.From.Reg), uint32(p.To.Reg), uint32(p.Reg))
3815
3816         case 89: /* VSX instructions, XX2-form */
3817                 /* reg none reg OR reg imm reg */
3818                 /* 2-register operand order: XB, XT or XB, UIM, XT*/
3819                 uim := int(c.regoff(p.GetFrom3()))
3820                 o1 = AOP_XX2(c.oprrr(p.As), uint32(p.To.Reg), uint32(uim), uint32(p.From.Reg))
3821
3822         case 90: /* VSX instructions, XX3-form */
3823                 if p.From3Type() == obj.TYPE_NONE {
3824                         /* reg reg reg */
3825                         /* 3-register operand order: XA, XB, XT */
3826                         o1 = AOP_XX3(c.oprrr(p.As), uint32(p.To.Reg), uint32(p.From.Reg), uint32(p.Reg))
3827                 } else if p.From3Type() == obj.TYPE_CONST {
3828                         /* reg reg reg imm */
3829                         /* operand order: XA, XB, DM, XT */
3830                         dm := int(c.regoff(p.GetFrom3()))
3831                         o1 = AOP_XX3I(c.oprrr(p.As), uint32(p.To.Reg), uint32(p.From.Reg), uint32(p.Reg), uint32(dm))
3832                 }
3833
3834         case 91: /* VSX instructions, XX4-form */
3835                 /* reg reg reg reg */
3836                 /* 3-register operand order: XA, XB, XC, XT */
3837                 o1 = AOP_XX4(c.oprrr(p.As), uint32(p.To.Reg), uint32(p.From.Reg), uint32(p.Reg), uint32(p.GetFrom3().Reg))
3838
3839         case 92: /* X-form instructions, 3-operands */
3840                 if p.To.Type == obj.TYPE_CONST {
3841                         /* imm reg reg */
3842                         xf := int32(p.From.Reg)
3843                         if REG_F0 <= xf && xf <= REG_F31 {
3844                                 /* operand order: FRA, FRB, BF */
3845                                 bf := int(c.regoff(&p.To)) << 2
3846                                 o1 = AOP_RRR(c.opirr(p.As), uint32(bf), uint32(p.From.Reg), uint32(p.Reg))
3847                         } else {
3848                                 /* operand order: RA, RB, L */
3849                                 l := int(c.regoff(&p.To))
3850                                 o1 = AOP_RRR(c.opirr(p.As), uint32(l), uint32(p.From.Reg), uint32(p.Reg))
3851                         }
3852                 } else if p.From3Type() == obj.TYPE_CONST {
3853                         /* reg reg imm */
3854                         /* operand order: RB, L, RA */
3855                         l := int(c.regoff(p.GetFrom3()))
3856                         o1 = AOP_RRR(c.opirr(p.As), uint32(l), uint32(p.To.Reg), uint32(p.From.Reg))
3857                 } else if p.To.Type == obj.TYPE_REG {
3858                         cr := int32(p.To.Reg)
3859                         if REG_CR0 <= cr && cr <= REG_CR7 {
3860                                 /* cr reg reg */
3861                                 /* operand order: RA, RB, BF */
3862                                 bf := (int(p.To.Reg) & 7) << 2
3863                                 o1 = AOP_RRR(c.opirr(p.As), uint32(bf), uint32(p.From.Reg), uint32(p.Reg))
3864                         } else if p.From.Type == obj.TYPE_CONST {
3865                                 /* reg imm */
3866                                 /* operand order: L, RT */
3867                                 l := int(c.regoff(&p.From))
3868                                 o1 = AOP_RRR(c.opirr(p.As), uint32(p.To.Reg), uint32(l), uint32(p.Reg))
3869                         } else {
3870                                 switch p.As {
3871                                 case ACOPY, APASTECC:
3872                                         o1 = AOP_RRR(c.opirr(p.As), uint32(1), uint32(p.From.Reg), uint32(p.To.Reg))
3873                                 default:
3874                                         /* reg reg reg */
3875                                         /* operand order: RS, RB, RA */
3876                                         o1 = AOP_RRR(c.oprrr(p.As), uint32(p.From.Reg), uint32(p.To.Reg), uint32(p.Reg))
3877                                 }
3878                         }
3879                 }
3880
3881         case 93: /* X-form instructions, 2-operands */
3882                 if p.To.Type == obj.TYPE_CONST {
3883                         /* imm reg */
3884                         /* operand order: FRB, BF */
3885                         bf := int(c.regoff(&p.To)) << 2
3886                         o1 = AOP_RR(c.opirr(p.As), uint32(bf), uint32(p.From.Reg))
3887                 } else if p.Reg == 0 {
3888                         /* popcnt* r,r, X-form */
3889                         /* operand order: RS, RA */
3890                         o1 = AOP_RRR(c.oprrr(p.As), uint32(p.From.Reg), uint32(p.To.Reg), uint32(p.Reg))
3891                 }
3892
3893         case 94: /* Z23-form instructions, 4-operands */
3894                 /* reg reg reg imm */
3895                 /* operand order: RA, RB, CY, RT */
3896                 cy := int(c.regoff(p.GetFrom3()))
3897                 o1 = AOP_Z23I(c.oprrr(p.As), uint32(p.To.Reg), uint32(p.From.Reg), uint32(p.Reg), uint32(cy))
3898
3899         case 96: /* VSX load, DQ-form */
3900                 /* reg imm reg */
3901                 /* operand order: (RA)(DQ), XT */
3902                 dq := int16(c.regoff(&p.From))
3903                 if (dq & 15) != 0 {
3904                         c.ctxt.Diag("invalid offset for DQ form load/store %v", dq)
3905                 }
3906                 o1 = AOP_DQ(c.opload(p.As), uint32(p.To.Reg), uint32(p.From.Reg), uint32(dq))
3907
3908         case 97: /* VSX store, DQ-form */
3909                 /* reg imm reg */
3910                 /* operand order: XT, (RA)(DQ) */
3911                 dq := int16(c.regoff(&p.To))
3912                 if (dq & 15) != 0 {
3913                         c.ctxt.Diag("invalid offset for DQ form load/store %v", dq)
3914                 }
3915                 o1 = AOP_DQ(c.opstore(p.As), uint32(p.From.Reg), uint32(p.To.Reg), uint32(dq))
3916         case 98: /* VSX indexed load or load with length (also left-justified), x-form */
3917                 /* vsreg, reg, reg */
3918                 o1 = AOP_XX1(c.opload(p.As), uint32(p.To.Reg), uint32(p.From.Reg), uint32(p.Reg))
3919         case 99: /* VSX store with length (also left-justified) x-form */
3920                 /* reg, reg, vsreg */
3921                 o1 = AOP_XX1(c.opstore(p.As), uint32(p.From.Reg), uint32(p.Reg), uint32(p.To.Reg))
3922         case 100: /* VSX X-form XXSPLTIB */
3923                 if p.From.Type == obj.TYPE_CONST {
3924                         /* imm reg */
3925                         uim := int(c.regoff(&p.From))
3926                         /* imm reg */
3927                         /* Use AOP_XX1 form with 0 for one of the registers. */
3928                         o1 = AOP_XX1(c.oprrr(p.As), uint32(p.To.Reg), uint32(0), uint32(uim))
3929                 } else {
3930                         c.ctxt.Diag("invalid ops for %v", p.As)
3931                 }
3932         case 101:
3933                 o1 = AOP_XX2(c.oprrr(p.As), uint32(p.To.Reg), uint32(0), uint32(p.From.Reg))
3934
3935         case 104: /* VSX mtvsr* instructions, XX1-form RA,RB,XT */
3936                 o1 = AOP_XX1(c.oprrr(p.As), uint32(p.To.Reg), uint32(p.From.Reg), uint32(p.Reg))
3937
3938         case 106: /* MOVD spr, soreg */
3939                 v := int32(p.From.Reg)
3940                 o1 = OPVCC(31, 339, 0, 0) /* mfspr */
3941                 o1 = AOP_RRR(o1, uint32(REGTMP), 0, 0) | (uint32(v)&0x1f)<<16 | ((uint32(v)>>5)&0x1f)<<11
3942                 so := c.regoff(&p.To)
3943                 o2 = AOP_IRR(c.opstore(AMOVD), uint32(REGTMP), uint32(p.To.Reg), uint32(so))
3944                 if so&0x3 != 0 {
3945                         log.Fatalf("invalid offset for DS form load/store %v", p)
3946                 }
3947                 if p.To.Reg == REGTMP {
3948                         log.Fatalf("SPR move to memory will clobber R31 %v", p)
3949                 }
3950
3951         case 107: /* MOVD soreg, spr */
3952                 v := int32(p.From.Reg)
3953                 so := c.regoff(&p.From)
3954                 o1 = AOP_IRR(c.opload(AMOVD), uint32(REGTMP), uint32(v), uint32(so))
3955                 o2 = OPVCC(31, 467, 0, 0) /* mtspr */
3956                 v = int32(p.To.Reg)
3957                 o2 = AOP_RRR(o2, uint32(REGTMP), 0, 0) | (uint32(v)&0x1f)<<16 | ((uint32(v)>>5)&0x1f)<<11
3958                 if so&0x3 != 0 {
3959                         log.Fatalf("invalid offset for DS form load/store %v", p)
3960                 }
3961
3962         case 108: /* mov r, xoreg ==> stwx rx,ry */
3963                 r := int(p.To.Reg)
3964                 o1 = AOP_RRR(c.opstorex(p.As), uint32(p.From.Reg), uint32(p.To.Index), uint32(r))
3965
3966         case 109: /* mov xoreg, r ==> lbzx/lhzx/lwzx rx,ry, lbzx rx,ry + extsb r,r */
3967                 r := int(p.From.Reg)
3968
3969                 o1 = AOP_RRR(c.oploadx(p.As), uint32(p.To.Reg), uint32(p.From.Index), uint32(r))
3970                 // Sign extend MOVB operations. This is ignored for other cases (o.size == 4).
3971                 o2 = LOP_RRR(OP_EXTSB, uint32(p.To.Reg), uint32(p.To.Reg), 0)
3972
3973         case 110: /* SETB creg, rt */
3974                 bfa := uint32(p.From.Reg) << 2
3975                 rt := uint32(p.To.Reg)
3976                 o1 = LOP_RRR(OP_SETB, bfa, rt, 0)
3977         }
3978
3979         out[0] = o1
3980         out[1] = o2
3981         out[2] = o3
3982         out[3] = o4
3983         out[4] = o5
3984 }
3985
3986 func (c *ctxt9) vregoff(a *obj.Addr) int64 {
3987         c.instoffset = 0
3988         if a != nil {
3989                 c.aclass(a)
3990         }
3991         return c.instoffset
3992 }
3993
3994 func (c *ctxt9) regoff(a *obj.Addr) int32 {
3995         return int32(c.vregoff(a))
3996 }
3997
3998 func (c *ctxt9) oprrr(a obj.As) uint32 {
3999         switch a {
4000         case AADD:
4001                 return OPVCC(31, 266, 0, 0)
4002         case AADDCC:
4003                 return OPVCC(31, 266, 0, 1)
4004         case AADDV:
4005                 return OPVCC(31, 266, 1, 0)
4006         case AADDVCC:
4007                 return OPVCC(31, 266, 1, 1)
4008         case AADDC:
4009                 return OPVCC(31, 10, 0, 0)
4010         case AADDCCC:
4011                 return OPVCC(31, 10, 0, 1)
4012         case AADDCV:
4013                 return OPVCC(31, 10, 1, 0)
4014         case AADDCVCC:
4015                 return OPVCC(31, 10, 1, 1)
4016         case AADDE:
4017                 return OPVCC(31, 138, 0, 0)
4018         case AADDECC:
4019                 return OPVCC(31, 138, 0, 1)
4020         case AADDEV:
4021                 return OPVCC(31, 138, 1, 0)
4022         case AADDEVCC:
4023                 return OPVCC(31, 138, 1, 1)
4024         case AADDME:
4025                 return OPVCC(31, 234, 0, 0)
4026         case AADDMECC:
4027                 return OPVCC(31, 234, 0, 1)
4028         case AADDMEV:
4029                 return OPVCC(31, 234, 1, 0)
4030         case AADDMEVCC:
4031                 return OPVCC(31, 234, 1, 1)
4032         case AADDZE:
4033                 return OPVCC(31, 202, 0, 0)
4034         case AADDZECC:
4035                 return OPVCC(31, 202, 0, 1)
4036         case AADDZEV:
4037                 return OPVCC(31, 202, 1, 0)
4038         case AADDZEVCC:
4039                 return OPVCC(31, 202, 1, 1)
4040         case AADDEX:
4041                 return OPVCC(31, 170, 0, 0) /* addex - v3.0b */
4042
4043         case AAND:
4044                 return OPVCC(31, 28, 0, 0)
4045         case AANDCC:
4046                 return OPVCC(31, 28, 0, 1)
4047         case AANDN:
4048                 return OPVCC(31, 60, 0, 0)
4049         case AANDNCC:
4050                 return OPVCC(31, 60, 0, 1)
4051
4052         case ACMP:
4053                 return OPVCC(31, 0, 0, 0) | 1<<21 /* L=1 */
4054         case ACMPU:
4055                 return OPVCC(31, 32, 0, 0) | 1<<21
4056         case ACMPW:
4057                 return OPVCC(31, 0, 0, 0) /* L=0 */
4058         case ACMPWU:
4059                 return OPVCC(31, 32, 0, 0)
4060         case ACMPB:
4061                 return OPVCC(31, 508, 0, 0) /* cmpb - v2.05 */
4062         case ACMPEQB:
4063                 return OPVCC(31, 224, 0, 0) /* cmpeqb - v3.00 */
4064
4065         case ACNTLZW:
4066                 return OPVCC(31, 26, 0, 0)
4067         case ACNTLZWCC:
4068                 return OPVCC(31, 26, 0, 1)
4069         case ACNTLZD:
4070                 return OPVCC(31, 58, 0, 0)
4071         case ACNTLZDCC:
4072                 return OPVCC(31, 58, 0, 1)
4073
4074         case ACRAND:
4075                 return OPVCC(19, 257, 0, 0)
4076         case ACRANDN:
4077                 return OPVCC(19, 129, 0, 0)
4078         case ACREQV:
4079                 return OPVCC(19, 289, 0, 0)
4080         case ACRNAND:
4081                 return OPVCC(19, 225, 0, 0)
4082         case ACRNOR:
4083                 return OPVCC(19, 33, 0, 0)
4084         case ACROR:
4085                 return OPVCC(19, 449, 0, 0)
4086         case ACRORN:
4087                 return OPVCC(19, 417, 0, 0)
4088         case ACRXOR:
4089                 return OPVCC(19, 193, 0, 0)
4090
4091         case ADCBF:
4092                 return OPVCC(31, 86, 0, 0)
4093         case ADCBI:
4094                 return OPVCC(31, 470, 0, 0)
4095         case ADCBST:
4096                 return OPVCC(31, 54, 0, 0)
4097         case ADCBT:
4098                 return OPVCC(31, 278, 0, 0)
4099         case ADCBTST:
4100                 return OPVCC(31, 246, 0, 0)
4101         case ADCBZ:
4102                 return OPVCC(31, 1014, 0, 0)
4103
4104         case AMODUD:
4105                 return OPVCC(31, 265, 0, 0) /* modud - v3.0 */
4106         case AMODUW:
4107                 return OPVCC(31, 267, 0, 0) /* moduw - v3.0 */
4108         case AMODSD:
4109                 return OPVCC(31, 777, 0, 0) /* modsd - v3.0 */
4110         case AMODSW:
4111                 return OPVCC(31, 779, 0, 0) /* modsw - v3.0 */
4112
4113         case ADIVW, AREM:
4114                 return OPVCC(31, 491, 0, 0)
4115
4116         case ADIVWCC:
4117                 return OPVCC(31, 491, 0, 1)
4118
4119         case ADIVWV:
4120                 return OPVCC(31, 491, 1, 0)
4121
4122         case ADIVWVCC:
4123                 return OPVCC(31, 491, 1, 1)
4124
4125         case ADIVWU, AREMU:
4126                 return OPVCC(31, 459, 0, 0)
4127
4128         case ADIVWUCC:
4129                 return OPVCC(31, 459, 0, 1)
4130
4131         case ADIVWUV:
4132                 return OPVCC(31, 459, 1, 0)
4133
4134         case ADIVWUVCC:
4135                 return OPVCC(31, 459, 1, 1)
4136
4137         case ADIVD, AREMD:
4138                 return OPVCC(31, 489, 0, 0)
4139
4140         case ADIVDCC:
4141                 return OPVCC(31, 489, 0, 1)
4142
4143         case ADIVDE:
4144                 return OPVCC(31, 425, 0, 0)
4145
4146         case ADIVDECC:
4147                 return OPVCC(31, 425, 0, 1)
4148
4149         case ADIVDEU:
4150                 return OPVCC(31, 393, 0, 0)
4151
4152         case ADIVDEUCC:
4153                 return OPVCC(31, 393, 0, 1)
4154
4155         case ADIVDV:
4156                 return OPVCC(31, 489, 1, 0)
4157
4158         case ADIVDVCC:
4159                 return OPVCC(31, 489, 1, 1)
4160
4161         case ADIVDU, AREMDU:
4162                 return OPVCC(31, 457, 0, 0)
4163
4164         case ADIVDUCC:
4165                 return OPVCC(31, 457, 0, 1)
4166
4167         case ADIVDUV:
4168                 return OPVCC(31, 457, 1, 0)
4169
4170         case ADIVDUVCC:
4171                 return OPVCC(31, 457, 1, 1)
4172
4173         case AEIEIO:
4174                 return OPVCC(31, 854, 0, 0)
4175
4176         case AEQV:
4177                 return OPVCC(31, 284, 0, 0)
4178         case AEQVCC:
4179                 return OPVCC(31, 284, 0, 1)
4180
4181         case AEXTSB:
4182                 return OPVCC(31, 954, 0, 0)
4183         case AEXTSBCC:
4184                 return OPVCC(31, 954, 0, 1)
4185         case AEXTSH:
4186                 return OPVCC(31, 922, 0, 0)
4187         case AEXTSHCC:
4188                 return OPVCC(31, 922, 0, 1)
4189         case AEXTSW:
4190                 return OPVCC(31, 986, 0, 0)
4191         case AEXTSWCC:
4192                 return OPVCC(31, 986, 0, 1)
4193
4194         case AFABS:
4195                 return OPVCC(63, 264, 0, 0)
4196         case AFABSCC:
4197                 return OPVCC(63, 264, 0, 1)
4198         case AFADD:
4199                 return OPVCC(63, 21, 0, 0)
4200         case AFADDCC:
4201                 return OPVCC(63, 21, 0, 1)
4202         case AFADDS:
4203                 return OPVCC(59, 21, 0, 0)
4204         case AFADDSCC:
4205                 return OPVCC(59, 21, 0, 1)
4206         case AFCMPO:
4207                 return OPVCC(63, 32, 0, 0)
4208         case AFCMPU:
4209                 return OPVCC(63, 0, 0, 0)
4210         case AFCFID:
4211                 return OPVCC(63, 846, 0, 0)
4212         case AFCFIDCC:
4213                 return OPVCC(63, 846, 0, 1)
4214         case AFCFIDU:
4215                 return OPVCC(63, 974, 0, 0)
4216         case AFCFIDUCC:
4217                 return OPVCC(63, 974, 0, 1)
4218         case AFCFIDS:
4219                 return OPVCC(59, 846, 0, 0)
4220         case AFCFIDSCC:
4221                 return OPVCC(59, 846, 0, 1)
4222         case AFCTIW:
4223                 return OPVCC(63, 14, 0, 0)
4224         case AFCTIWCC:
4225                 return OPVCC(63, 14, 0, 1)
4226         case AFCTIWZ:
4227                 return OPVCC(63, 15, 0, 0)
4228         case AFCTIWZCC:
4229                 return OPVCC(63, 15, 0, 1)
4230         case AFCTID:
4231                 return OPVCC(63, 814, 0, 0)
4232         case AFCTIDCC:
4233                 return OPVCC(63, 814, 0, 1)
4234         case AFCTIDZ:
4235                 return OPVCC(63, 815, 0, 0)
4236         case AFCTIDZCC:
4237                 return OPVCC(63, 815, 0, 1)
4238         case AFDIV:
4239                 return OPVCC(63, 18, 0, 0)
4240         case AFDIVCC:
4241                 return OPVCC(63, 18, 0, 1)
4242         case AFDIVS:
4243                 return OPVCC(59, 18, 0, 0)
4244         case AFDIVSCC:
4245                 return OPVCC(59, 18, 0, 1)
4246         case AFMADD:
4247                 return OPVCC(63, 29, 0, 0)
4248         case AFMADDCC:
4249                 return OPVCC(63, 29, 0, 1)
4250         case AFMADDS:
4251                 return OPVCC(59, 29, 0, 0)
4252         case AFMADDSCC:
4253                 return OPVCC(59, 29, 0, 1)
4254
4255         case AFMOVS, AFMOVD:
4256                 return OPVCC(63, 72, 0, 0) /* load */
4257         case AFMOVDCC:
4258                 return OPVCC(63, 72, 0, 1)
4259         case AFMSUB:
4260                 return OPVCC(63, 28, 0, 0)
4261         case AFMSUBCC:
4262                 return OPVCC(63, 28, 0, 1)
4263         case AFMSUBS:
4264                 return OPVCC(59, 28, 0, 0)
4265         case AFMSUBSCC:
4266                 return OPVCC(59, 28, 0, 1)
4267         case AFMUL:
4268                 return OPVCC(63, 25, 0, 0)
4269         case AFMULCC:
4270                 return OPVCC(63, 25, 0, 1)
4271         case AFMULS:
4272                 return OPVCC(59, 25, 0, 0)
4273         case AFMULSCC:
4274                 return OPVCC(59, 25, 0, 1)
4275         case AFNABS:
4276                 return OPVCC(63, 136, 0, 0)
4277         case AFNABSCC:
4278                 return OPVCC(63, 136, 0, 1)
4279         case AFNEG:
4280                 return OPVCC(63, 40, 0, 0)
4281         case AFNEGCC:
4282                 return OPVCC(63, 40, 0, 1)
4283         case AFNMADD:
4284                 return OPVCC(63, 31, 0, 0)
4285         case AFNMADDCC:
4286                 return OPVCC(63, 31, 0, 1)
4287         case AFNMADDS:
4288                 return OPVCC(59, 31, 0, 0)
4289         case AFNMADDSCC:
4290                 return OPVCC(59, 31, 0, 1)
4291         case AFNMSUB:
4292                 return OPVCC(63, 30, 0, 0)
4293         case AFNMSUBCC:
4294                 return OPVCC(63, 30, 0, 1)
4295         case AFNMSUBS:
4296                 return OPVCC(59, 30, 0, 0)
4297         case AFNMSUBSCC:
4298                 return OPVCC(59, 30, 0, 1)
4299         case AFCPSGN:
4300                 return OPVCC(63, 8, 0, 0)
4301         case AFCPSGNCC:
4302                 return OPVCC(63, 8, 0, 1)
4303         case AFRES:
4304                 return OPVCC(59, 24, 0, 0)
4305         case AFRESCC:
4306                 return OPVCC(59, 24, 0, 1)
4307         case AFRIM:
4308                 return OPVCC(63, 488, 0, 0)
4309         case AFRIMCC:
4310                 return OPVCC(63, 488, 0, 1)
4311         case AFRIP:
4312                 return OPVCC(63, 456, 0, 0)
4313         case AFRIPCC:
4314                 return OPVCC(63, 456, 0, 1)
4315         case AFRIZ:
4316                 return OPVCC(63, 424, 0, 0)
4317         case AFRIZCC:
4318                 return OPVCC(63, 424, 0, 1)
4319         case AFRIN:
4320                 return OPVCC(63, 392, 0, 0)
4321         case AFRINCC:
4322                 return OPVCC(63, 392, 0, 1)
4323         case AFRSP:
4324                 return OPVCC(63, 12, 0, 0)
4325         case AFRSPCC:
4326                 return OPVCC(63, 12, 0, 1)
4327         case AFRSQRTE:
4328                 return OPVCC(63, 26, 0, 0)
4329         case AFRSQRTECC:
4330                 return OPVCC(63, 26, 0, 1)
4331         case AFSEL:
4332                 return OPVCC(63, 23, 0, 0)
4333         case AFSELCC:
4334                 return OPVCC(63, 23, 0, 1)
4335         case AFSQRT:
4336                 return OPVCC(63, 22, 0, 0)
4337         case AFSQRTCC:
4338                 return OPVCC(63, 22, 0, 1)
4339         case AFSQRTS:
4340                 return OPVCC(59, 22, 0, 0)
4341         case AFSQRTSCC:
4342                 return OPVCC(59, 22, 0, 1)
4343         case AFSUB:
4344                 return OPVCC(63, 20, 0, 0)
4345         case AFSUBCC:
4346                 return OPVCC(63, 20, 0, 1)
4347         case AFSUBS:
4348                 return OPVCC(59, 20, 0, 0)
4349         case AFSUBSCC:
4350                 return OPVCC(59, 20, 0, 1)
4351
4352         case AICBI:
4353                 return OPVCC(31, 982, 0, 0)
4354         case AISYNC:
4355                 return OPVCC(19, 150, 0, 0)
4356
4357         case AMTFSB0:
4358                 return OPVCC(63, 70, 0, 0)
4359         case AMTFSB0CC:
4360                 return OPVCC(63, 70, 0, 1)
4361         case AMTFSB1:
4362                 return OPVCC(63, 38, 0, 0)
4363         case AMTFSB1CC:
4364                 return OPVCC(63, 38, 0, 1)
4365
4366         case AMULHW:
4367                 return OPVCC(31, 75, 0, 0)
4368         case AMULHWCC:
4369                 return OPVCC(31, 75, 0, 1)
4370         case AMULHWU:
4371                 return OPVCC(31, 11, 0, 0)
4372         case AMULHWUCC:
4373                 return OPVCC(31, 11, 0, 1)
4374         case AMULLW:
4375                 return OPVCC(31, 235, 0, 0)
4376         case AMULLWCC:
4377                 return OPVCC(31, 235, 0, 1)
4378         case AMULLWV:
4379                 return OPVCC(31, 235, 1, 0)
4380         case AMULLWVCC:
4381                 return OPVCC(31, 235, 1, 1)
4382
4383         case AMULHD:
4384                 return OPVCC(31, 73, 0, 0)
4385         case AMULHDCC:
4386                 return OPVCC(31, 73, 0, 1)
4387         case AMULHDU:
4388                 return OPVCC(31, 9, 0, 0)
4389         case AMULHDUCC:
4390                 return OPVCC(31, 9, 0, 1)
4391         case AMULLD:
4392                 return OPVCC(31, 233, 0, 0)
4393         case AMULLDCC:
4394                 return OPVCC(31, 233, 0, 1)
4395         case AMULLDV:
4396                 return OPVCC(31, 233, 1, 0)
4397         case AMULLDVCC:
4398                 return OPVCC(31, 233, 1, 1)
4399
4400         case ANAND:
4401                 return OPVCC(31, 476, 0, 0)
4402         case ANANDCC:
4403                 return OPVCC(31, 476, 0, 1)
4404         case ANEG:
4405                 return OPVCC(31, 104, 0, 0)
4406         case ANEGCC:
4407                 return OPVCC(31, 104, 0, 1)
4408         case ANEGV:
4409                 return OPVCC(31, 104, 1, 0)
4410         case ANEGVCC:
4411                 return OPVCC(31, 104, 1, 1)
4412         case ANOR:
4413                 return OPVCC(31, 124, 0, 0)
4414         case ANORCC:
4415                 return OPVCC(31, 124, 0, 1)
4416         case AOR:
4417                 return OPVCC(31, 444, 0, 0)
4418         case AORCC:
4419                 return OPVCC(31, 444, 0, 1)
4420         case AORN:
4421                 return OPVCC(31, 412, 0, 0)
4422         case AORNCC:
4423                 return OPVCC(31, 412, 0, 1)
4424
4425         case APOPCNTD:
4426                 return OPVCC(31, 506, 0, 0) /* popcntd - v2.06 */
4427         case APOPCNTW:
4428                 return OPVCC(31, 378, 0, 0) /* popcntw - v2.06 */
4429         case APOPCNTB:
4430                 return OPVCC(31, 122, 0, 0) /* popcntb - v2.02 */
4431         case ACNTTZW:
4432                 return OPVCC(31, 538, 0, 0) /* cnttzw - v3.00 */
4433         case ACNTTZWCC:
4434                 return OPVCC(31, 538, 0, 1) /* cnttzw. - v3.00 */
4435         case ACNTTZD:
4436                 return OPVCC(31, 570, 0, 0) /* cnttzd - v3.00 */
4437         case ACNTTZDCC:
4438                 return OPVCC(31, 570, 0, 1) /* cnttzd. - v3.00 */
4439
4440         case ARFI:
4441                 return OPVCC(19, 50, 0, 0)
4442         case ARFCI:
4443                 return OPVCC(19, 51, 0, 0)
4444         case ARFID:
4445                 return OPVCC(19, 18, 0, 0)
4446         case AHRFID:
4447                 return OPVCC(19, 274, 0, 0)
4448
4449         case ARLWNM:
4450                 return OPVCC(23, 0, 0, 0)
4451         case ARLWNMCC:
4452                 return OPVCC(23, 0, 0, 1)
4453
4454         case ARLDCL:
4455                 return OPVCC(30, 8, 0, 0)
4456         case ARLDCLCC:
4457                 return OPVCC(30, 0, 0, 1)
4458
4459         case ARLDCR:
4460                 return OPVCC(30, 9, 0, 0)
4461         case ARLDCRCC:
4462                 return OPVCC(30, 9, 0, 1)
4463
4464         case ARLDICL:
4465                 return OPVCC(30, 0, 0, 0)
4466         case ARLDICLCC:
4467                 return OPVCC(30, 0, 0, 1)
4468         case ARLDICR:
4469                 return OPMD(30, 1, 0) // rldicr
4470         case ARLDICRCC:
4471                 return OPMD(30, 1, 1) // rldicr.
4472
4473         case ARLDIC:
4474                 return OPMD(30, 2, 0) // rldic
4475         case ARLDICCC:
4476                 return OPMD(30, 2, 1) // rldic.
4477
4478         case ASYSCALL:
4479                 return OPVCC(17, 1, 0, 0)
4480
4481         case ASLW:
4482                 return OPVCC(31, 24, 0, 0)
4483         case ASLWCC:
4484                 return OPVCC(31, 24, 0, 1)
4485         case ASLD:
4486                 return OPVCC(31, 27, 0, 0)
4487         case ASLDCC:
4488                 return OPVCC(31, 27, 0, 1)
4489
4490         case ASRAW:
4491                 return OPVCC(31, 792, 0, 0)
4492         case ASRAWCC:
4493                 return OPVCC(31, 792, 0, 1)
4494         case ASRAD:
4495                 return OPVCC(31, 794, 0, 0)
4496         case ASRADCC:
4497                 return OPVCC(31, 794, 0, 1)
4498
4499         case AEXTSWSLI:
4500                 return OPVCC(31, 445, 0, 0)
4501         case AEXTSWSLICC:
4502                 return OPVCC(31, 445, 0, 1)
4503
4504         case ASRW:
4505                 return OPVCC(31, 536, 0, 0)
4506         case ASRWCC:
4507                 return OPVCC(31, 536, 0, 1)
4508         case ASRD:
4509                 return OPVCC(31, 539, 0, 0)
4510         case ASRDCC:
4511                 return OPVCC(31, 539, 0, 1)
4512
4513         case ASUB:
4514                 return OPVCC(31, 40, 0, 0)
4515         case ASUBCC:
4516                 return OPVCC(31, 40, 0, 1)
4517         case ASUBV:
4518                 return OPVCC(31, 40, 1, 0)
4519         case ASUBVCC:
4520                 return OPVCC(31, 40, 1, 1)
4521         case ASUBC:
4522                 return OPVCC(31, 8, 0, 0)
4523         case ASUBCCC:
4524                 return OPVCC(31, 8, 0, 1)
4525         case ASUBCV:
4526                 return OPVCC(31, 8, 1, 0)
4527         case ASUBCVCC:
4528                 return OPVCC(31, 8, 1, 1)
4529         case ASUBE:
4530                 return OPVCC(31, 136, 0, 0)
4531         case ASUBECC:
4532                 return OPVCC(31, 136, 0, 1)
4533         case ASUBEV:
4534                 return OPVCC(31, 136, 1, 0)
4535         case ASUBEVCC:
4536                 return OPVCC(31, 136, 1, 1)
4537         case ASUBME:
4538                 return OPVCC(31, 232, 0, 0)
4539         case ASUBMECC:
4540                 return OPVCC(31, 232, 0, 1)
4541         case ASUBMEV:
4542                 return OPVCC(31, 232, 1, 0)
4543         case ASUBMEVCC:
4544                 return OPVCC(31, 232, 1, 1)
4545         case ASUBZE:
4546                 return OPVCC(31, 200, 0, 0)
4547         case ASUBZECC:
4548                 return OPVCC(31, 200, 0, 1)
4549         case ASUBZEV:
4550                 return OPVCC(31, 200, 1, 0)
4551         case ASUBZEVCC:
4552                 return OPVCC(31, 200, 1, 1)
4553
4554         case ASYNC:
4555                 return OPVCC(31, 598, 0, 0)
4556         case ALWSYNC:
4557                 return OPVCC(31, 598, 0, 0) | 1<<21
4558
4559         case APTESYNC:
4560                 return OPVCC(31, 598, 0, 0) | 2<<21
4561
4562         case ATLBIE:
4563                 return OPVCC(31, 306, 0, 0)
4564         case ATLBIEL:
4565                 return OPVCC(31, 274, 0, 0)
4566         case ATLBSYNC:
4567                 return OPVCC(31, 566, 0, 0)
4568         case ASLBIA:
4569                 return OPVCC(31, 498, 0, 0)
4570         case ASLBIE:
4571                 return OPVCC(31, 434, 0, 0)
4572         case ASLBMFEE:
4573                 return OPVCC(31, 915, 0, 0)
4574         case ASLBMFEV:
4575                 return OPVCC(31, 851, 0, 0)
4576         case ASLBMTE:
4577                 return OPVCC(31, 402, 0, 0)
4578
4579         case ATW:
4580                 return OPVCC(31, 4, 0, 0)
4581         case ATD:
4582                 return OPVCC(31, 68, 0, 0)
4583
4584         /* Vector (VMX/Altivec) instructions */
4585         /* ISA 2.03 enables these for PPC970. For POWERx processors, these */
4586         /* are enabled starting at POWER6 (ISA 2.05). */
4587         case AVAND:
4588                 return OPVX(4, 1028, 0, 0) /* vand - v2.03 */
4589         case AVANDC:
4590                 return OPVX(4, 1092, 0, 0) /* vandc - v2.03 */
4591         case AVNAND:
4592                 return OPVX(4, 1412, 0, 0) /* vnand - v2.07 */
4593
4594         case AVOR:
4595                 return OPVX(4, 1156, 0, 0) /* vor - v2.03 */
4596         case AVORC:
4597                 return OPVX(4, 1348, 0, 0) /* vorc - v2.07 */
4598         case AVNOR:
4599                 return OPVX(4, 1284, 0, 0) /* vnor - v2.03 */
4600         case AVXOR:
4601                 return OPVX(4, 1220, 0, 0) /* vxor - v2.03 */
4602         case AVEQV:
4603                 return OPVX(4, 1668, 0, 0) /* veqv - v2.07 */
4604
4605         case AVADDUBM:
4606                 return OPVX(4, 0, 0, 0) /* vaddubm - v2.03 */
4607         case AVADDUHM:
4608                 return OPVX(4, 64, 0, 0) /* vadduhm - v2.03 */
4609         case AVADDUWM:
4610                 return OPVX(4, 128, 0, 0) /* vadduwm - v2.03 */
4611         case AVADDUDM:
4612                 return OPVX(4, 192, 0, 0) /* vaddudm - v2.07 */
4613         case AVADDUQM:
4614                 return OPVX(4, 256, 0, 0) /* vadduqm - v2.07 */
4615
4616         case AVADDCUQ:
4617                 return OPVX(4, 320, 0, 0) /* vaddcuq - v2.07 */
4618         case AVADDCUW:
4619                 return OPVX(4, 384, 0, 0) /* vaddcuw - v2.03 */
4620
4621         case AVADDUBS:
4622                 return OPVX(4, 512, 0, 0) /* vaddubs - v2.03 */
4623         case AVADDUHS:
4624                 return OPVX(4, 576, 0, 0) /* vadduhs - v2.03 */
4625         case AVADDUWS:
4626                 return OPVX(4, 640, 0, 0) /* vadduws - v2.03 */
4627
4628         case AVADDSBS:
4629                 return OPVX(4, 768, 0, 0) /* vaddsbs - v2.03 */
4630         case AVADDSHS:
4631                 return OPVX(4, 832, 0, 0) /* vaddshs - v2.03 */
4632         case AVADDSWS:
4633                 return OPVX(4, 896, 0, 0) /* vaddsws - v2.03 */
4634
4635         case AVADDEUQM:
4636                 return OPVX(4, 60, 0, 0) /* vaddeuqm - v2.07 */
4637         case AVADDECUQ:
4638                 return OPVX(4, 61, 0, 0) /* vaddecuq - v2.07 */
4639
4640         case AVMULESB:
4641                 return OPVX(4, 776, 0, 0) /* vmulesb - v2.03 */
4642         case AVMULOSB:
4643                 return OPVX(4, 264, 0, 0) /* vmulosb - v2.03 */
4644         case AVMULEUB:
4645                 return OPVX(4, 520, 0, 0) /* vmuleub - v2.03 */
4646         case AVMULOUB:
4647                 return OPVX(4, 8, 0, 0) /* vmuloub - v2.03 */
4648         case AVMULESH:
4649                 return OPVX(4, 840, 0, 0) /* vmulesh - v2.03 */
4650         case AVMULOSH:
4651                 return OPVX(4, 328, 0, 0) /* vmulosh - v2.03 */
4652         case AVMULEUH:
4653                 return OPVX(4, 584, 0, 0) /* vmuleuh - v2.03 */
4654         case AVMULOUH:
4655                 return OPVX(4, 72, 0, 0) /* vmulouh - v2.03 */
4656         case AVMULESW:
4657                 return OPVX(4, 904, 0, 0) /* vmulesw - v2.07 */
4658         case AVMULOSW:
4659                 return OPVX(4, 392, 0, 0) /* vmulosw - v2.07 */
4660         case AVMULEUW:
4661                 return OPVX(4, 648, 0, 0) /* vmuleuw - v2.07 */
4662         case AVMULOUW:
4663                 return OPVX(4, 136, 0, 0) /* vmulouw - v2.07 */
4664         case AVMULUWM:
4665                 return OPVX(4, 137, 0, 0) /* vmuluwm - v2.07 */
4666
4667         case AVPMSUMB:
4668                 return OPVX(4, 1032, 0, 0) /* vpmsumb - v2.07 */
4669         case AVPMSUMH:
4670                 return OPVX(4, 1096, 0, 0) /* vpmsumh - v2.07 */
4671         case AVPMSUMW:
4672                 return OPVX(4, 1160, 0, 0) /* vpmsumw - v2.07 */
4673         case AVPMSUMD:
4674                 return OPVX(4, 1224, 0, 0) /* vpmsumd - v2.07 */
4675
4676         case AVMSUMUDM:
4677                 return OPVX(4, 35, 0, 0) /* vmsumudm - v3.00b */
4678
4679         case AVSUBUBM:
4680                 return OPVX(4, 1024, 0, 0) /* vsububm - v2.03 */
4681         case AVSUBUHM:
4682                 return OPVX(4, 1088, 0, 0) /* vsubuhm - v2.03 */
4683         case AVSUBUWM:
4684                 return OPVX(4, 1152, 0, 0) /* vsubuwm - v2.03 */
4685         case AVSUBUDM:
4686                 return OPVX(4, 1216, 0, 0) /* vsubudm - v2.07 */
4687         case AVSUBUQM:
4688                 return OPVX(4, 1280, 0, 0) /* vsubuqm - v2.07 */
4689
4690         case AVSUBCUQ:
4691                 return OPVX(4, 1344, 0, 0) /* vsubcuq - v2.07 */
4692         case AVSUBCUW:
4693                 return OPVX(4, 1408, 0, 0) /* vsubcuw - v2.03 */
4694
4695         case AVSUBUBS:
4696                 return OPVX(4, 1536, 0, 0) /* vsububs - v2.03 */
4697         case AVSUBUHS:
4698                 return OPVX(4, 1600, 0, 0) /* vsubuhs - v2.03 */
4699         case AVSUBUWS:
4700                 return OPVX(4, 1664, 0, 0) /* vsubuws - v2.03 */
4701
4702         case AVSUBSBS:
4703                 return OPVX(4, 1792, 0, 0) /* vsubsbs - v2.03 */
4704         case AVSUBSHS:
4705                 return OPVX(4, 1856, 0, 0) /* vsubshs - v2.03 */
4706         case AVSUBSWS:
4707                 return OPVX(4, 1920, 0, 0) /* vsubsws - v2.03 */
4708
4709         case AVSUBEUQM:
4710                 return OPVX(4, 62, 0, 0) /* vsubeuqm - v2.07 */
4711         case AVSUBECUQ:
4712                 return OPVX(4, 63, 0, 0) /* vsubecuq - v2.07 */
4713
4714         case AVRLB:
4715                 return OPVX(4, 4, 0, 0) /* vrlb - v2.03 */
4716         case AVRLH:
4717                 return OPVX(4, 68, 0, 0) /* vrlh - v2.03 */
4718         case AVRLW:
4719                 return OPVX(4, 132, 0, 0) /* vrlw - v2.03 */
4720         case AVRLD:
4721                 return OPVX(4, 196, 0, 0) /* vrld - v2.07 */
4722
4723         case AVMRGOW:
4724                 return OPVX(4, 1676, 0, 0) /* vmrgow - v2.07 */
4725         case AVMRGEW:
4726                 return OPVX(4, 1932, 0, 0) /* vmrgew - v2.07 */
4727
4728         case AVSLB:
4729                 return OPVX(4, 260, 0, 0) /* vslh - v2.03 */
4730         case AVSLH:
4731                 return OPVX(4, 324, 0, 0) /* vslh - v2.03 */
4732         case AVSLW:
4733                 return OPVX(4, 388, 0, 0) /* vslw - v2.03 */
4734         case AVSL:
4735                 return OPVX(4, 452, 0, 0) /* vsl - v2.03 */
4736         case AVSLO:
4737                 return OPVX(4, 1036, 0, 0) /* vsl - v2.03 */
4738         case AVSRB:
4739                 return OPVX(4, 516, 0, 0) /* vsrb - v2.03 */
4740         case AVSRH:
4741                 return OPVX(4, 580, 0, 0) /* vsrh - v2.03 */
4742         case AVSRW:
4743                 return OPVX(4, 644, 0, 0) /* vsrw - v2.03 */
4744         case AVSR:
4745                 return OPVX(4, 708, 0, 0) /* vsr - v2.03 */
4746         case AVSRO:
4747                 return OPVX(4, 1100, 0, 0) /* vsro - v2.03 */
4748         case AVSLD:
4749                 return OPVX(4, 1476, 0, 0) /* vsld - v2.07 */
4750         case AVSRD:
4751                 return OPVX(4, 1732, 0, 0) /* vsrd - v2.07 */
4752
4753         case AVSRAB:
4754                 return OPVX(4, 772, 0, 0) /* vsrab - v2.03 */
4755         case AVSRAH:
4756                 return OPVX(4, 836, 0, 0) /* vsrah - v2.03 */
4757         case AVSRAW:
4758                 return OPVX(4, 900, 0, 0) /* vsraw - v2.03 */
4759         case AVSRAD:
4760                 return OPVX(4, 964, 0, 0) /* vsrad - v2.07 */
4761
4762         case AVBPERMQ:
4763                 return OPVC(4, 1356, 0, 0) /* vbpermq - v2.07 */
4764         case AVBPERMD:
4765                 return OPVC(4, 1484, 0, 0) /* vbpermd - v3.00 */
4766
4767         case AVCLZB:
4768                 return OPVX(4, 1794, 0, 0) /* vclzb - v2.07 */
4769         case AVCLZH:
4770                 return OPVX(4, 1858, 0, 0) /* vclzh - v2.07 */
4771         case AVCLZW:
4772                 return OPVX(4, 1922, 0, 0) /* vclzw - v2.07 */
4773         case AVCLZD:
4774                 return OPVX(4, 1986, 0, 0) /* vclzd - v2.07 */
4775
4776         case AVCLZLSBB:
4777                 return OPVX(4, 1538, 0, 0) /* vclzlsbb - v3.0 */
4778         case AVCTZLSBB:
4779                 return OPVX(4, 1538, 0, 0) | 1<<16 /* vctzlsbb - v3.0 */
4780
4781         case AVPOPCNTB:
4782                 return OPVX(4, 1795, 0, 0) /* vpopcntb - v2.07 */
4783         case AVPOPCNTH:
4784                 return OPVX(4, 1859, 0, 0) /* vpopcnth - v2.07 */
4785         case AVPOPCNTW:
4786                 return OPVX(4, 1923, 0, 0) /* vpopcntw - v2.07 */
4787         case AVPOPCNTD:
4788                 return OPVX(4, 1987, 0, 0) /* vpopcntd - v2.07 */
4789
4790         case AVCMPEQUB:
4791                 return OPVC(4, 6, 0, 0) /* vcmpequb - v2.03 */
4792         case AVCMPEQUBCC:
4793                 return OPVC(4, 6, 0, 1) /* vcmpequb. - v2.03 */
4794         case AVCMPEQUH:
4795                 return OPVC(4, 70, 0, 0) /* vcmpequh - v2.03 */
4796         case AVCMPEQUHCC:
4797                 return OPVC(4, 70, 0, 1) /* vcmpequh. - v2.03 */
4798         case AVCMPEQUW:
4799                 return OPVC(4, 134, 0, 0) /* vcmpequw - v2.03 */
4800         case AVCMPEQUWCC:
4801                 return OPVC(4, 134, 0, 1) /* vcmpequw. - v2.03 */
4802         case AVCMPEQUD:
4803                 return OPVC(4, 199, 0, 0) /* vcmpequd - v2.07 */
4804         case AVCMPEQUDCC:
4805                 return OPVC(4, 199, 0, 1) /* vcmpequd. - v2.07 */
4806
4807         case AVCMPGTUB:
4808                 return OPVC(4, 518, 0, 0) /* vcmpgtub - v2.03 */
4809         case AVCMPGTUBCC:
4810                 return OPVC(4, 518, 0, 1) /* vcmpgtub. - v2.03 */
4811         case AVCMPGTUH:
4812                 return OPVC(4, 582, 0, 0) /* vcmpgtuh - v2.03 */
4813         case AVCMPGTUHCC:
4814                 return OPVC(4, 582, 0, 1) /* vcmpgtuh. - v2.03 */
4815         case AVCMPGTUW:
4816                 return OPVC(4, 646, 0, 0) /* vcmpgtuw - v2.03 */
4817         case AVCMPGTUWCC:
4818                 return OPVC(4, 646, 0, 1) /* vcmpgtuw. - v2.03 */
4819         case AVCMPGTUD:
4820                 return OPVC(4, 711, 0, 0) /* vcmpgtud - v2.07 */
4821         case AVCMPGTUDCC:
4822                 return OPVC(4, 711, 0, 1) /* vcmpgtud. v2.07 */
4823         case AVCMPGTSB:
4824                 return OPVC(4, 774, 0, 0) /* vcmpgtsb - v2.03 */
4825         case AVCMPGTSBCC:
4826                 return OPVC(4, 774, 0, 1) /* vcmpgtsb. - v2.03 */
4827         case AVCMPGTSH:
4828                 return OPVC(4, 838, 0, 0) /* vcmpgtsh - v2.03 */
4829         case AVCMPGTSHCC:
4830                 return OPVC(4, 838, 0, 1) /* vcmpgtsh. - v2.03 */
4831         case AVCMPGTSW:
4832                 return OPVC(4, 902, 0, 0) /* vcmpgtsw - v2.03 */
4833         case AVCMPGTSWCC:
4834                 return OPVC(4, 902, 0, 1) /* vcmpgtsw. - v2.03 */
4835         case AVCMPGTSD:
4836                 return OPVC(4, 967, 0, 0) /* vcmpgtsd - v2.07 */
4837         case AVCMPGTSDCC:
4838                 return OPVC(4, 967, 0, 1) /* vcmpgtsd. - v2.07 */
4839
4840         case AVCMPNEZB:
4841                 return OPVC(4, 263, 0, 0) /* vcmpnezb - v3.00 */
4842         case AVCMPNEZBCC:
4843                 return OPVC(4, 263, 0, 1) /* vcmpnezb. - v3.00 */
4844         case AVCMPNEB:
4845                 return OPVC(4, 7, 0, 0) /* vcmpneb - v3.00 */
4846         case AVCMPNEBCC:
4847                 return OPVC(4, 7, 0, 1) /* vcmpneb. - v3.00 */
4848         case AVCMPNEH:
4849                 return OPVC(4, 71, 0, 0) /* vcmpneh - v3.00 */
4850         case AVCMPNEHCC:
4851                 return OPVC(4, 71, 0, 1) /* vcmpneh. - v3.00 */
4852         case AVCMPNEW:
4853                 return OPVC(4, 135, 0, 0) /* vcmpnew - v3.00 */
4854         case AVCMPNEWCC:
4855                 return OPVC(4, 135, 0, 1) /* vcmpnew. - v3.00 */
4856
4857         case AVPERM:
4858                 return OPVX(4, 43, 0, 0) /* vperm - v2.03 */
4859         case AVPERMXOR:
4860                 return OPVX(4, 45, 0, 0) /* vpermxor - v2.03 */
4861         case AVPERMR:
4862                 return OPVX(4, 59, 0, 0) /* vpermr - v3.0 */
4863
4864         case AVSEL:
4865                 return OPVX(4, 42, 0, 0) /* vsel - v2.03 */
4866
4867         case AVCIPHER:
4868                 return OPVX(4, 1288, 0, 0) /* vcipher - v2.07 */
4869         case AVCIPHERLAST:
4870                 return OPVX(4, 1289, 0, 0) /* vcipherlast - v2.07 */
4871         case AVNCIPHER:
4872                 return OPVX(4, 1352, 0, 0) /* vncipher - v2.07 */
4873         case AVNCIPHERLAST:
4874                 return OPVX(4, 1353, 0, 0) /* vncipherlast - v2.07 */
4875         case AVSBOX:
4876                 return OPVX(4, 1480, 0, 0) /* vsbox - v2.07 */
4877         /* End of vector instructions */
4878
4879         /* Vector scalar (VSX) instructions */
4880         /* ISA 2.06 enables these for POWER7. */
4881         case AMFVSRD, AMFVRD, AMFFPRD:
4882                 return OPVXX1(31, 51, 0) /* mfvsrd - v2.07 */
4883         case AMFVSRWZ:
4884                 return OPVXX1(31, 115, 0) /* mfvsrwz - v2.07 */
4885         case AMFVSRLD:
4886                 return OPVXX1(31, 307, 0) /* mfvsrld - v3.00 */
4887
4888         case AMTVSRD, AMTFPRD, AMTVRD:
4889                 return OPVXX1(31, 179, 0) /* mtvsrd - v2.07 */
4890         case AMTVSRWA:
4891                 return OPVXX1(31, 211, 0) /* mtvsrwa - v2.07 */
4892         case AMTVSRWZ:
4893                 return OPVXX1(31, 243, 0) /* mtvsrwz - v2.07 */
4894         case AMTVSRDD:
4895                 return OPVXX1(31, 435, 0) /* mtvsrdd - v3.00 */
4896         case AMTVSRWS:
4897                 return OPVXX1(31, 403, 0) /* mtvsrws - v3.00 */
4898
4899         case AXXLAND:
4900                 return OPVXX3(60, 130, 0) /* xxland - v2.06 */
4901         case AXXLANDC:
4902                 return OPVXX3(60, 138, 0) /* xxlandc - v2.06 */
4903         case AXXLEQV:
4904                 return OPVXX3(60, 186, 0) /* xxleqv - v2.07 */
4905         case AXXLNAND:
4906                 return OPVXX3(60, 178, 0) /* xxlnand - v2.07 */
4907
4908         case AXXLORC:
4909                 return OPVXX3(60, 170, 0) /* xxlorc - v2.07 */
4910         case AXXLNOR:
4911                 return OPVXX3(60, 162, 0) /* xxlnor - v2.06 */
4912         case AXXLOR, AXXLORQ:
4913                 return OPVXX3(60, 146, 0) /* xxlor - v2.06 */
4914         case AXXLXOR:
4915                 return OPVXX3(60, 154, 0) /* xxlxor - v2.06 */
4916
4917         case AXXSEL:
4918                 return OPVXX4(60, 3, 0) /* xxsel - v2.06 */
4919
4920         case AXXMRGHW:
4921                 return OPVXX3(60, 18, 0) /* xxmrghw - v2.06 */
4922         case AXXMRGLW:
4923                 return OPVXX3(60, 50, 0) /* xxmrglw - v2.06 */
4924
4925         case AXXSPLTW:
4926                 return OPVXX2(60, 164, 0) /* xxspltw - v2.06 */
4927
4928         case AXXSPLTIB:
4929                 return OPVCC(60, 360, 0, 0) /* xxspltib - v3.0 */
4930
4931         case AXXPERM:
4932                 return OPVXX3(60, 26, 0) /* xxperm - v2.06 */
4933         case AXXPERMDI:
4934                 return OPVXX3(60, 10, 0) /* xxpermdi - v2.06 */
4935
4936         case AXXSLDWI:
4937                 return OPVXX3(60, 2, 0) /* xxsldwi - v2.06 */
4938
4939         case AXXBRQ:
4940                 return OPVXX2VA(60, 475, 31) /* xxbrq - v3.0 */
4941         case AXXBRD:
4942                 return OPVXX2VA(60, 475, 23) /* xxbrd - v3.0 */
4943         case AXXBRW:
4944                 return OPVXX2VA(60, 475, 15) /* xxbrw - v3.0 */
4945         case AXXBRH:
4946                 return OPVXX2VA(60, 475, 7) /* xxbrh - v3.0 */
4947
4948         case AXSCVDPSP:
4949                 return OPVXX2(60, 265, 0) /* xscvdpsp - v2.06 */
4950         case AXSCVSPDP:
4951                 return OPVXX2(60, 329, 0) /* xscvspdp - v2.06 */
4952         case AXSCVDPSPN:
4953                 return OPVXX2(60, 267, 0) /* xscvdpspn - v2.07 */
4954         case AXSCVSPDPN:
4955                 return OPVXX2(60, 331, 0) /* xscvspdpn - v2.07 */
4956
4957         case AXVCVDPSP:
4958                 return OPVXX2(60, 393, 0) /* xvcvdpsp - v2.06 */
4959         case AXVCVSPDP:
4960                 return OPVXX2(60, 457, 0) /* xvcvspdp - v2.06 */
4961
4962         case AXSCVDPSXDS:
4963                 return OPVXX2(60, 344, 0) /* xscvdpsxds - v2.06 */
4964         case AXSCVDPSXWS:
4965                 return OPVXX2(60, 88, 0) /* xscvdpsxws - v2.06 */
4966         case AXSCVDPUXDS:
4967                 return OPVXX2(60, 328, 0) /* xscvdpuxds - v2.06 */
4968         case AXSCVDPUXWS:
4969                 return OPVXX2(60, 72, 0) /* xscvdpuxws - v2.06 */
4970
4971         case AXSCVSXDDP:
4972                 return OPVXX2(60, 376, 0) /* xscvsxddp - v2.06 */
4973         case AXSCVUXDDP:
4974                 return OPVXX2(60, 360, 0) /* xscvuxddp - v2.06 */
4975         case AXSCVSXDSP:
4976                 return OPVXX2(60, 312, 0) /* xscvsxdsp - v2.06 */
4977         case AXSCVUXDSP:
4978                 return OPVXX2(60, 296, 0) /* xscvuxdsp - v2.06 */
4979
4980         case AXVCVDPSXDS:
4981                 return OPVXX2(60, 472, 0) /* xvcvdpsxds - v2.06 */
4982         case AXVCVDPSXWS:
4983                 return OPVXX2(60, 216, 0) /* xvcvdpsxws - v2.06 */
4984         case AXVCVDPUXDS:
4985                 return OPVXX2(60, 456, 0) /* xvcvdpuxds - v2.06 */
4986         case AXVCVDPUXWS:
4987                 return OPVXX2(60, 200, 0) /* xvcvdpuxws - v2.06 */
4988         case AXVCVSPSXDS:
4989                 return OPVXX2(60, 408, 0) /* xvcvspsxds - v2.07 */
4990         case AXVCVSPSXWS:
4991                 return OPVXX2(60, 152, 0) /* xvcvspsxws - v2.07 */
4992         case AXVCVSPUXDS:
4993                 return OPVXX2(60, 392, 0) /* xvcvspuxds - v2.07 */
4994         case AXVCVSPUXWS:
4995                 return OPVXX2(60, 136, 0) /* xvcvspuxws - v2.07 */
4996
4997         case AXVCVSXDDP:
4998                 return OPVXX2(60, 504, 0) /* xvcvsxddp - v2.06 */
4999         case AXVCVSXWDP:
5000                 return OPVXX2(60, 248, 0) /* xvcvsxwdp - v2.06 */
5001         case AXVCVUXDDP:
5002                 return OPVXX2(60, 488, 0) /* xvcvuxddp - v2.06 */
5003         case AXVCVUXWDP:
5004                 return OPVXX2(60, 232, 0) /* xvcvuxwdp - v2.06 */
5005         case AXVCVSXDSP:
5006                 return OPVXX2(60, 440, 0) /* xvcvsxdsp - v2.06 */
5007         case AXVCVSXWSP:
5008                 return OPVXX2(60, 184, 0) /* xvcvsxwsp - v2.06 */
5009         case AXVCVUXDSP:
5010                 return OPVXX2(60, 424, 0) /* xvcvuxdsp - v2.06 */
5011         case AXVCVUXWSP:
5012                 return OPVXX2(60, 168, 0) /* xvcvuxwsp - v2.06 */
5013         /* End of VSX instructions */
5014
5015         case AMADDHD:
5016                 return OPVX(4, 48, 0, 0) /* maddhd - v3.00 */
5017         case AMADDHDU:
5018                 return OPVX(4, 49, 0, 0) /* maddhdu - v3.00 */
5019         case AMADDLD:
5020                 return OPVX(4, 51, 0, 0) /* maddld - v3.00 */
5021
5022         case AXOR:
5023                 return OPVCC(31, 316, 0, 0)
5024         case AXORCC:
5025                 return OPVCC(31, 316, 0, 1)
5026         }
5027
5028         c.ctxt.Diag("bad r/r, r/r/r or r/r/r/r opcode %v", a)
5029         return 0
5030 }
5031
5032 func (c *ctxt9) opirrr(a obj.As) uint32 {
5033         switch a {
5034         /* Vector (VMX/Altivec) instructions */
5035         /* ISA 2.03 enables these for PPC970. For POWERx processors, these */
5036         /* are enabled starting at POWER6 (ISA 2.05). */
5037         case AVSLDOI:
5038                 return OPVX(4, 44, 0, 0) /* vsldoi - v2.03 */
5039         }
5040
5041         c.ctxt.Diag("bad i/r/r/r opcode %v", a)
5042         return 0
5043 }
5044
5045 func (c *ctxt9) opiirr(a obj.As) uint32 {
5046         switch a {
5047         /* Vector (VMX/Altivec) instructions */
5048         /* ISA 2.07 enables these for POWER8 and beyond. */
5049         case AVSHASIGMAW:
5050                 return OPVX(4, 1666, 0, 0) /* vshasigmaw - v2.07 */
5051         case AVSHASIGMAD:
5052                 return OPVX(4, 1730, 0, 0) /* vshasigmad - v2.07 */
5053         }
5054
5055         c.ctxt.Diag("bad i/i/r/r opcode %v", a)
5056         return 0
5057 }
5058
5059 func (c *ctxt9) opirr(a obj.As) uint32 {
5060         switch a {
5061         case AADD:
5062                 return OPVCC(14, 0, 0, 0)
5063         case AADDC:
5064                 return OPVCC(12, 0, 0, 0)
5065         case AADDCCC:
5066                 return OPVCC(13, 0, 0, 0)
5067         case AADDIS:
5068                 return OPVCC(15, 0, 0, 0) /* ADDIS */
5069
5070         case AANDCC:
5071                 return OPVCC(28, 0, 0, 0)
5072         case AANDISCC:
5073                 return OPVCC(29, 0, 0, 0) /* ANDIS. */
5074
5075         case ABR:
5076                 return OPVCC(18, 0, 0, 0)
5077         case ABL:
5078                 return OPVCC(18, 0, 0, 0) | 1
5079         case obj.ADUFFZERO:
5080                 return OPVCC(18, 0, 0, 0) | 1
5081         case obj.ADUFFCOPY:
5082                 return OPVCC(18, 0, 0, 0) | 1
5083         case ABC:
5084                 return OPVCC(16, 0, 0, 0)
5085         case ABCL:
5086                 return OPVCC(16, 0, 0, 0) | 1
5087
5088         case ABEQ:
5089                 return AOP_RRR(16<<26, BO_BCR, BI_EQ, 0)
5090         case ABGE:
5091                 return AOP_RRR(16<<26, BO_NOTBCR, BI_LT, 0)
5092         case ABGT:
5093                 return AOP_RRR(16<<26, BO_BCR, BI_GT, 0)
5094         case ABLE:
5095                 return AOP_RRR(16<<26, BO_NOTBCR, BI_GT, 0)
5096         case ABLT:
5097                 return AOP_RRR(16<<26, BO_BCR, BI_LT, 0)
5098         case ABNE:
5099                 return AOP_RRR(16<<26, BO_NOTBCR, BI_EQ, 0)
5100         case ABVC:
5101                 return AOP_RRR(16<<26, BO_NOTBCR, BI_FU, 0)
5102         case ABVS:
5103                 return AOP_RRR(16<<26, BO_BCR, BI_FU, 0)
5104         case ABDZ:
5105                 return AOP_RRR(16<<26, BO_NOTBCTR, 0, 0)
5106         case ABDNZ:
5107                 return AOP_RRR(16<<26, BO_BCTR, 0, 0)
5108
5109         case ACMP:
5110                 return OPVCC(11, 0, 0, 0) | 1<<21 /* L=1 */
5111         case ACMPU:
5112                 return OPVCC(10, 0, 0, 0) | 1<<21
5113         case ACMPW:
5114                 return OPVCC(11, 0, 0, 0) /* L=0 */
5115         case ACMPWU:
5116                 return OPVCC(10, 0, 0, 0)
5117         case ACMPEQB:
5118                 return OPVCC(31, 224, 0, 0) /* cmpeqb - v3.00 */
5119
5120         case ALSW:
5121                 return OPVCC(31, 597, 0, 0)
5122
5123         case ACOPY:
5124                 return OPVCC(31, 774, 0, 0) /* copy - v3.00 */
5125         case APASTECC:
5126                 return OPVCC(31, 902, 0, 1) /* paste. - v3.00 */
5127         case ADARN:
5128                 return OPVCC(31, 755, 0, 0) /* darn - v3.00 */
5129
5130         case AMULLW, AMULLD:
5131                 return OPVCC(7, 0, 0, 0) /* mulli works with MULLW or MULLD */
5132
5133         case AOR:
5134                 return OPVCC(24, 0, 0, 0)
5135         case AORIS:
5136                 return OPVCC(25, 0, 0, 0) /* ORIS */
5137
5138         case ARLWMI:
5139                 return OPVCC(20, 0, 0, 0) /* rlwimi */
5140         case ARLWMICC:
5141                 return OPVCC(20, 0, 0, 1)
5142         case ARLDMI:
5143                 return OPMD(30, 3, 0) /* rldimi */
5144         case ARLDMICC:
5145                 return OPMD(30, 3, 1) /* rldimi. */
5146         case ARLDIMI:
5147                 return OPMD(30, 3, 0) /* rldimi */
5148         case ARLDIMICC:
5149                 return OPMD(30, 3, 1) /* rldimi. */
5150         case ARLWNM:
5151                 return OPVCC(21, 0, 0, 0) /* rlwinm */
5152         case ARLWNMCC:
5153                 return OPVCC(21, 0, 0, 1)
5154
5155         case ARLDCL:
5156                 return OPMD(30, 0, 0) /* rldicl */
5157         case ARLDCLCC:
5158                 return OPMD(30, 0, 1) /* rldicl. */
5159         case ARLDCR:
5160                 return OPMD(30, 1, 0) /* rldicr */
5161         case ARLDCRCC:
5162                 return OPMD(30, 1, 1) /* rldicr. */
5163         case ARLDC:
5164                 return OPMD(30, 2, 0) /* rldic */
5165         case ARLDCCC:
5166                 return OPMD(30, 2, 1) /* rldic. */
5167
5168         case ASRAW:
5169                 return OPVCC(31, 824, 0, 0)
5170         case ASRAWCC:
5171                 return OPVCC(31, 824, 0, 1)
5172         case ASRAD:
5173                 return OPVCC(31, (413 << 1), 0, 0)
5174         case ASRADCC:
5175                 return OPVCC(31, (413 << 1), 0, 1)
5176         case AEXTSWSLI:
5177                 return OPVCC(31, 445, 0, 0)
5178         case AEXTSWSLICC:
5179                 return OPVCC(31, 445, 0, 1)
5180
5181         case ASTSW:
5182                 return OPVCC(31, 725, 0, 0)
5183
5184         case ASUBC:
5185                 return OPVCC(8, 0, 0, 0)
5186
5187         case ATW:
5188                 return OPVCC(3, 0, 0, 0)
5189         case ATD:
5190                 return OPVCC(2, 0, 0, 0)
5191
5192         /* Vector (VMX/Altivec) instructions */
5193         /* ISA 2.03 enables these for PPC970. For POWERx processors, these */
5194         /* are enabled starting at POWER6 (ISA 2.05). */
5195         case AVSPLTB:
5196                 return OPVX(4, 524, 0, 0) /* vspltb - v2.03 */
5197         case AVSPLTH:
5198                 return OPVX(4, 588, 0, 0) /* vsplth - v2.03 */
5199         case AVSPLTW:
5200                 return OPVX(4, 652, 0, 0) /* vspltw - v2.03 */
5201
5202         case AVSPLTISB:
5203                 return OPVX(4, 780, 0, 0) /* vspltisb - v2.03 */
5204         case AVSPLTISH:
5205                 return OPVX(4, 844, 0, 0) /* vspltish - v2.03 */
5206         case AVSPLTISW:
5207                 return OPVX(4, 908, 0, 0) /* vspltisw - v2.03 */
5208         /* End of vector instructions */
5209
5210         case AFTDIV:
5211                 return OPVCC(63, 128, 0, 0) /* ftdiv - v2.06 */
5212         case AFTSQRT:
5213                 return OPVCC(63, 160, 0, 0) /* ftsqrt - v2.06 */
5214
5215         case AXOR:
5216                 return OPVCC(26, 0, 0, 0) /* XORIL */
5217         case AXORIS:
5218                 return OPVCC(27, 0, 0, 0) /* XORIS */
5219         }
5220
5221         c.ctxt.Diag("bad opcode i/r or i/r/r %v", a)
5222         return 0
5223 }
5224
5225 /*
5226  * load o(a),d
5227  */
5228 func (c *ctxt9) opload(a obj.As) uint32 {
5229         switch a {
5230         case AMOVD:
5231                 return OPVCC(58, 0, 0, 0) /* ld */
5232         case AMOVDU:
5233                 return OPVCC(58, 0, 0, 1) /* ldu */
5234         case AMOVWZ:
5235                 return OPVCC(32, 0, 0, 0) /* lwz */
5236         case AMOVWZU:
5237                 return OPVCC(33, 0, 0, 0) /* lwzu */
5238         case AMOVW:
5239                 return OPVCC(58, 0, 0, 0) | 1<<1 /* lwa */
5240         case ALXV:
5241                 return OPDQ(61, 1, 0) /* lxv - ISA v3.0 */
5242         case ALXVL:
5243                 return OPVXX1(31, 269, 0) /* lxvl - ISA v3.0 */
5244         case ALXVLL:
5245                 return OPVXX1(31, 301, 0) /* lxvll - ISA v3.0 */
5246         case ALXVX:
5247                 return OPVXX1(31, 268, 0) /* lxvx - ISA v3.0 */
5248
5249                 /* no AMOVWU */
5250         case AMOVB, AMOVBZ:
5251                 return OPVCC(34, 0, 0, 0)
5252                 /* load */
5253
5254         case AMOVBU, AMOVBZU:
5255                 return OPVCC(35, 0, 0, 0)
5256         case AFMOVD:
5257                 return OPVCC(50, 0, 0, 0)
5258         case AFMOVDU:
5259                 return OPVCC(51, 0, 0, 0)
5260         case AFMOVS:
5261                 return OPVCC(48, 0, 0, 0)
5262         case AFMOVSU:
5263                 return OPVCC(49, 0, 0, 0)
5264         case AMOVH:
5265                 return OPVCC(42, 0, 0, 0)
5266         case AMOVHU:
5267                 return OPVCC(43, 0, 0, 0)
5268         case AMOVHZ:
5269                 return OPVCC(40, 0, 0, 0)
5270         case AMOVHZU:
5271                 return OPVCC(41, 0, 0, 0)
5272         case AMOVMW:
5273                 return OPVCC(46, 0, 0, 0) /* lmw */
5274         }
5275
5276         c.ctxt.Diag("bad load opcode %v", a)
5277         return 0
5278 }
5279
5280 /*
5281  * indexed load a(b),d
5282  */
5283 func (c *ctxt9) oploadx(a obj.As) uint32 {
5284         switch a {
5285         case AMOVWZ:
5286                 return OPVCC(31, 23, 0, 0) /* lwzx */
5287         case AMOVWZU:
5288                 return OPVCC(31, 55, 0, 0) /* lwzux */
5289         case AMOVW:
5290                 return OPVCC(31, 341, 0, 0) /* lwax */
5291         case AMOVWU:
5292                 return OPVCC(31, 373, 0, 0) /* lwaux */
5293
5294         case AMOVB, AMOVBZ:
5295                 return OPVCC(31, 87, 0, 0) /* lbzx */
5296
5297         case AMOVBU, AMOVBZU:
5298                 return OPVCC(31, 119, 0, 0) /* lbzux */
5299         case AFMOVD:
5300                 return OPVCC(31, 599, 0, 0) /* lfdx */
5301         case AFMOVDU:
5302                 return OPVCC(31, 631, 0, 0) /*  lfdux */
5303         case AFMOVS:
5304                 return OPVCC(31, 535, 0, 0) /* lfsx */
5305         case AFMOVSU:
5306                 return OPVCC(31, 567, 0, 0) /* lfsux */
5307         case AFMOVSX:
5308                 return OPVCC(31, 855, 0, 0) /* lfiwax - power6, isa 2.05 */
5309         case AFMOVSZ:
5310                 return OPVCC(31, 887, 0, 0) /* lfiwzx - power7, isa 2.06 */
5311         case AMOVH:
5312                 return OPVCC(31, 343, 0, 0) /* lhax */
5313         case AMOVHU:
5314                 return OPVCC(31, 375, 0, 0) /* lhaux */
5315         case AMOVHBR:
5316                 return OPVCC(31, 790, 0, 0) /* lhbrx */
5317         case AMOVWBR:
5318                 return OPVCC(31, 534, 0, 0) /* lwbrx */
5319         case AMOVDBR:
5320                 return OPVCC(31, 532, 0, 0) /* ldbrx */
5321         case AMOVHZ:
5322                 return OPVCC(31, 279, 0, 0) /* lhzx */
5323         case AMOVHZU:
5324                 return OPVCC(31, 311, 0, 0) /* lhzux */
5325         case ALBAR:
5326                 return OPVCC(31, 52, 0, 0) /* lbarx */
5327         case ALHAR:
5328                 return OPVCC(31, 116, 0, 0) /* lharx */
5329         case ALWAR:
5330                 return OPVCC(31, 20, 0, 0) /* lwarx */
5331         case ALDAR:
5332                 return OPVCC(31, 84, 0, 0) /* ldarx */
5333         case ALSW:
5334                 return OPVCC(31, 533, 0, 0) /* lswx */
5335         case AMOVD:
5336                 return OPVCC(31, 21, 0, 0) /* ldx */
5337         case AMOVDU:
5338                 return OPVCC(31, 53, 0, 0) /* ldux */
5339
5340         /* Vector (VMX/Altivec) instructions */
5341         case ALVEBX:
5342                 return OPVCC(31, 7, 0, 0) /* lvebx - v2.03 */
5343         case ALVEHX:
5344                 return OPVCC(31, 39, 0, 0) /* lvehx - v2.03 */
5345         case ALVEWX:
5346                 return OPVCC(31, 71, 0, 0) /* lvewx - v2.03 */
5347         case ALVX:
5348                 return OPVCC(31, 103, 0, 0) /* lvx - v2.03 */
5349         case ALVXL:
5350                 return OPVCC(31, 359, 0, 0) /* lvxl - v2.03 */
5351         case ALVSL:
5352                 return OPVCC(31, 6, 0, 0) /* lvsl - v2.03 */
5353         case ALVSR:
5354                 return OPVCC(31, 38, 0, 0) /* lvsr - v2.03 */
5355                 /* End of vector instructions */
5356
5357         /* Vector scalar (VSX) instructions */
5358         case ALXVX:
5359                 return OPVXX1(31, 268, 0) /* lxvx - ISA v3.0 */
5360         case ALXVD2X:
5361                 return OPVXX1(31, 844, 0) /* lxvd2x - v2.06 */
5362         case ALXVW4X:
5363                 return OPVXX1(31, 780, 0) /* lxvw4x - v2.06 */
5364         case ALXVH8X:
5365                 return OPVXX1(31, 812, 0) /* lxvh8x - v3.00 */
5366         case ALXVB16X:
5367                 return OPVXX1(31, 876, 0) /* lxvb16x - v3.00 */
5368         case ALXVDSX:
5369                 return OPVXX1(31, 332, 0) /* lxvdsx - v2.06 */
5370         case ALXSDX:
5371                 return OPVXX1(31, 588, 0) /* lxsdx - v2.06 */
5372         case ALXSIWAX:
5373                 return OPVXX1(31, 76, 0) /* lxsiwax - v2.07 */
5374         case ALXSIWZX:
5375                 return OPVXX1(31, 12, 0) /* lxsiwzx - v2.07 */
5376         }
5377
5378         c.ctxt.Diag("bad loadx opcode %v", a)
5379         return 0
5380 }
5381
5382 /*
5383  * store s,o(d)
5384  */
5385 func (c *ctxt9) opstore(a obj.As) uint32 {
5386         switch a {
5387         case AMOVB, AMOVBZ:
5388                 return OPVCC(38, 0, 0, 0) /* stb */
5389
5390         case AMOVBU, AMOVBZU:
5391                 return OPVCC(39, 0, 0, 0) /* stbu */
5392         case AFMOVD:
5393                 return OPVCC(54, 0, 0, 0) /* stfd */
5394         case AFMOVDU:
5395                 return OPVCC(55, 0, 0, 0) /* stfdu */
5396         case AFMOVS:
5397                 return OPVCC(52, 0, 0, 0) /* stfs */
5398         case AFMOVSU:
5399                 return OPVCC(53, 0, 0, 0) /* stfsu */
5400
5401         case AMOVHZ, AMOVH:
5402                 return OPVCC(44, 0, 0, 0) /* sth */
5403
5404         case AMOVHZU, AMOVHU:
5405                 return OPVCC(45, 0, 0, 0) /* sthu */
5406         case AMOVMW:
5407                 return OPVCC(47, 0, 0, 0) /* stmw */
5408         case ASTSW:
5409                 return OPVCC(31, 725, 0, 0) /* stswi */
5410
5411         case AMOVWZ, AMOVW:
5412                 return OPVCC(36, 0, 0, 0) /* stw */
5413
5414         case AMOVWZU, AMOVWU:
5415                 return OPVCC(37, 0, 0, 0) /* stwu */
5416         case AMOVD:
5417                 return OPVCC(62, 0, 0, 0) /* std */
5418         case AMOVDU:
5419                 return OPVCC(62, 0, 0, 1) /* stdu */
5420         case ASTXV:
5421                 return OPDQ(61, 5, 0) /* stxv ISA 3.0 */
5422         case ASTXVL:
5423                 return OPVXX1(31, 397, 0) /* stxvl ISA 3.0 */
5424         case ASTXVLL:
5425                 return OPVXX1(31, 429, 0) /* stxvll ISA 3.0 */
5426         case ASTXVX:
5427                 return OPVXX1(31, 396, 0) /* stxvx - ISA v3.0 */
5428
5429         }
5430
5431         c.ctxt.Diag("unknown store opcode %v", a)
5432         return 0
5433 }
5434
5435 /*
5436  * indexed store s,a(b)
5437  */
5438 func (c *ctxt9) opstorex(a obj.As) uint32 {
5439         switch a {
5440         case AMOVB, AMOVBZ:
5441                 return OPVCC(31, 215, 0, 0) /* stbx */
5442
5443         case AMOVBU, AMOVBZU:
5444                 return OPVCC(31, 247, 0, 0) /* stbux */
5445         case AFMOVD:
5446                 return OPVCC(31, 727, 0, 0) /* stfdx */
5447         case AFMOVDU:
5448                 return OPVCC(31, 759, 0, 0) /* stfdux */
5449         case AFMOVS:
5450                 return OPVCC(31, 663, 0, 0) /* stfsx */
5451         case AFMOVSU:
5452                 return OPVCC(31, 695, 0, 0) /* stfsux */
5453         case AFMOVSX:
5454                 return OPVCC(31, 983, 0, 0) /* stfiwx */
5455
5456         case AMOVHZ, AMOVH:
5457                 return OPVCC(31, 407, 0, 0) /* sthx */
5458         case AMOVHBR:
5459                 return OPVCC(31, 918, 0, 0) /* sthbrx */
5460
5461         case AMOVHZU, AMOVHU:
5462                 return OPVCC(31, 439, 0, 0) /* sthux */
5463
5464         case AMOVWZ, AMOVW:
5465                 return OPVCC(31, 151, 0, 0) /* stwx */
5466
5467         case AMOVWZU, AMOVWU:
5468                 return OPVCC(31, 183, 0, 0) /* stwux */
5469         case ASTSW:
5470                 return OPVCC(31, 661, 0, 0) /* stswx */
5471         case AMOVWBR:
5472                 return OPVCC(31, 662, 0, 0) /* stwbrx */
5473         case AMOVDBR:
5474                 return OPVCC(31, 660, 0, 0) /* stdbrx */
5475         case ASTBCCC:
5476                 return OPVCC(31, 694, 0, 1) /* stbcx. */
5477         case ASTHCCC:
5478                 return OPVCC(31, 726, 0, 1) /* sthcx. */
5479         case ASTWCCC:
5480                 return OPVCC(31, 150, 0, 1) /* stwcx. */
5481         case ASTDCCC:
5482                 return OPVCC(31, 214, 0, 1) /* stwdx. */
5483         case AMOVD:
5484                 return OPVCC(31, 149, 0, 0) /* stdx */
5485         case AMOVDU:
5486                 return OPVCC(31, 181, 0, 0) /* stdux */
5487
5488         /* Vector (VMX/Altivec) instructions */
5489         case ASTVEBX:
5490                 return OPVCC(31, 135, 0, 0) /* stvebx - v2.03 */
5491         case ASTVEHX:
5492                 return OPVCC(31, 167, 0, 0) /* stvehx - v2.03 */
5493         case ASTVEWX:
5494                 return OPVCC(31, 199, 0, 0) /* stvewx - v2.03 */
5495         case ASTVX:
5496                 return OPVCC(31, 231, 0, 0) /* stvx - v2.03 */
5497         case ASTVXL:
5498                 return OPVCC(31, 487, 0, 0) /* stvxl - v2.03 */
5499                 /* End of vector instructions */
5500
5501         /* Vector scalar (VSX) instructions */
5502         case ASTXVX:
5503                 return OPVXX1(31, 396, 0) /* stxvx - v3.0 */
5504         case ASTXVD2X:
5505                 return OPVXX1(31, 972, 0) /* stxvd2x - v2.06 */
5506         case ASTXVW4X:
5507                 return OPVXX1(31, 908, 0) /* stxvw4x - v2.06 */
5508         case ASTXVH8X:
5509                 return OPVXX1(31, 940, 0) /* stxvh8x - v3.0 */
5510         case ASTXVB16X:
5511                 return OPVXX1(31, 1004, 0) /* stxvb16x - v3.0 */
5512
5513         case ASTXSDX:
5514                 return OPVXX1(31, 716, 0) /* stxsdx - v2.06 */
5515
5516         case ASTXSIWX:
5517                 return OPVXX1(31, 140, 0) /* stxsiwx - v2.07 */
5518
5519                 /* End of vector scalar instructions */
5520
5521         }
5522
5523         c.ctxt.Diag("unknown storex opcode %v", a)
5524         return 0
5525 }