asm6.go 118 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
// Inferno utils/6l/span.c
// http://code.google.com/p/inferno-os/source/browse/utils/6l/span.c
//
//	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
//	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
//	Portions Copyright © 1997-1999 Vita Nuova Limited
//	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
//	Portions Copyright © 2004,2006 Bruce Ellis
//	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
//	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
//	Portions Copyright © 2009 The Go Authors.  All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package x86

import (
	"cmd/internal/obj"
	"fmt"
	"log"
	"strings"
)

// Instruction layout.

const (
43 44 45 46 47 48 49 50 51 52 53 54 55
	// Loop alignment constants:
	// want to align loop entry to LoopAlign-byte boundary,
	// and willing to insert at most MaxLoopPad bytes of NOP to do so.
	// We define a loop entry as the target of a backward jump.
	//
	// gcc uses MaxLoopPad = 10 for its 'generic x86-64' config,
	// and it aligns all jump targets, not just backward jump targets.
	//
	// As of 6/1/2012, the effect of setting MaxLoopPad = 10 here
	// is very slight but negative, so the alignment is disabled by
	// setting MaxLoopPad = 0. The code is here for reference and
	// for future experiments.
	//
56 57 58 59 60 61 62
	LoopAlign  = 16
	MaxLoopPad = 0
	FuncAlign  = 16
)

type Optab struct {
	as     int16
63
	ytab   []ytab
64 65 66 67
	prefix uint8
	op     [23]uint8
}

68 69
type ytab struct {
	from    uint8
70
	from3   uint8
71 72 73 74 75
	to      uint8
	zcase   uint8
	zoffset uint8
}

76 77 78
type Movtab struct {
	as   int16
	ft   uint8
79
	f3t  uint8
80 81 82 83 84 85
	tt   uint8
	code uint8
	op   [4]uint8
}

const (
86
	Yxxx = iota
87
	Ynone
88 89 90 91 92
	Yi0 // $0
	Yi1 // $1
	Yi8 // $x, x fits in int8
	Yu8 // $x, x fits in uint8
	Yu7 // $x, x in 0..127 (fits in both int8 and uint8)
93 94 95 96 97 98 99 100 101 102
	Ys32
	Yi32
	Yi64
	Yiauto
	Yal
	Ycl
	Yax
	Ycx
	Yrb
	Yrl
103
	Yrl32 // Yrl on 32-bit system
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	Yrf
	Yf0
	Yrx
	Ymb
	Yml
	Ym
	Ybr
	Ycs
	Yss
	Yds
	Yes
	Yfs
	Ygs
	Ygdtr
	Yidtr
	Yldtr
	Ymsw
	Ytask
	Ycr0
	Ycr1
	Ycr2
	Ycr3
	Ycr4
	Ycr5
	Ycr6
	Ycr7
	Ycr8
	Ydr0
	Ydr1
	Ydr2
	Ydr3
	Ydr4
	Ydr5
	Ydr6
	Ydr7
	Ytr0
	Ytr1
	Ytr2
	Ytr3
	Ytr4
	Ytr5
	Ytr6
	Ytr7
	Ymr
	Ymm
	Yxr
	Yxm
	Ytls
152
	Ytextsize
153
	Yindir
154
	Ymax
155 156 157 158
)

const (
	Zxxx = iota
159 160 161 162 163
	Zlit
	Zlitm_r
	Z_rp
	Zbr
	Zcall
164
	Zcallcon
165
	Zcallduff
166
	Zcallind
167 168 169 170 171 172 173 174 175 176
	Zcallindreg
	Zib_
	Zib_rp
	Zibo_m
	Zibo_m_xm
	Zil_
	Zil_rp
	Ziq_rp
	Zilo_m
	Zjmp
177
	Zjmpcon
178 179 180 181 182 183
	Zloop
	Zo_iw
	Zm_o
	Zm_r
	Zm2_r
	Zm_r_xm
184
	Zm_r_xm_vex
185 186 187 188
	Zm_r_i_xm
	Zm_r_3d
	Zm_r_xm_nr
	Zr_m_xm_nr
189
	Zibm_r /* mmx1,mmx2/mem64,imm8 */
190 191 192 193 194 195 196
	Zmb_r
	Zaut_r
	Zo_m
	Zo_m64
	Zpseudo
	Zr_m
	Zr_m_xm
197
	Zr_m_xm_vex
198 199 200 201 202 203 204 205 206 207
	Zrp_
	Z_ib
	Z_il
	Zm_ibo
	Zm_ilo
	Zib_rr
	Zil_rr
	Zclr
	Zbyte
	Zmax
208 209 210
)

const (
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	Px    = 0
	Px1   = 1    // symbolic; exact value doesn't matter
	P32   = 0x32 /* 32-bit only */
	Pe    = 0x66 /* operand escape */
	Pm    = 0x0f /* 2byte opcode escape */
	Pq    = 0xff /* both escapes: 66 0f */
	Pb    = 0xfe /* byte operands */
	Pf2   = 0xf2 /* xmm escape 1: f2 0f */
	Pf3   = 0xf3 /* xmm escape 2: f3 0f */
	Pq3   = 0x67 /* xmm escape 3: 66 48 0f */
	Pvex1 = 0xc5 /* 66 escape, vex encoding */
	Pvex2 = 0xc6 /* f3 escape, vex encoding */
	Pw    = 0x48 /* Rex.w */
	Pw8   = 0x90 // symbolic; exact value doesn't matter
	Py    = 0x80 /* defaults to 64-bit mode */
	Py1   = 0x81 // symbolic; exact value doesn't matter
	Py3   = 0x83 // symbolic; exact value doesn't matter
228 229 230 231 232

	Rxw = 1 << 3 /* =1, 64-bit operand size */
	Rxr = 1 << 2 /* extend modrm reg */
	Rxx = 1 << 1 /* extend sib index */
	Rxb = 1 << 0 /* extend modrm r/m, sib base, or opcode reg */
233 234 235 236
)

var ycover [Ymax * Ymax]uint8

237
var reg [MAXREG]int
238

239
var regrex [MAXREG + 1]int
240

241
var ynone = []ytab{
242
	{Ynone, Ynone, Ynone, Zlit, 1},
243 244 245
}

var ytext = []ytab{
246 247
	{Ymb, Ynone, Ytextsize, Zpseudo, 0},
	{Ymb, Yi32, Ytextsize, Zpseudo, 1},
248 249 250
}

var ynop = []ytab{
251 252 253 254 255 256 257 258 259
	{Ynone, Ynone, Ynone, Zpseudo, 0},
	{Ynone, Ynone, Yiauto, Zpseudo, 0},
	{Ynone, Ynone, Yml, Zpseudo, 0},
	{Ynone, Ynone, Yrf, Zpseudo, 0},
	{Ynone, Ynone, Yxr, Zpseudo, 0},
	{Yiauto, Ynone, Ynone, Zpseudo, 0},
	{Yml, Ynone, Ynone, Zpseudo, 0},
	{Yrf, Ynone, Ynone, Zpseudo, 0},
	{Yxr, Ynone, Ynone, Zpseudo, 1},
260 261 262
}

var yfuncdata = []ytab{
263
	{Yi32, Ynone, Ym, Zpseudo, 0},
264 265 266
}

var ypcdata = []ytab{
267
	{Yi32, Ynone, Yi32, Zpseudo, 0},
268 269 270
}

var yxorb = []ytab{
271 272 273 274
	{Yi32, Ynone, Yal, Zib_, 1},
	{Yi32, Ynone, Ymb, Zibo_m, 2},
	{Yrb, Ynone, Ymb, Zr_m, 1},
	{Ymb, Ynone, Yrb, Zm_r, 1},
275 276 277
}

var yxorl = []ytab{
278 279 280 281 282
	{Yi8, Ynone, Yml, Zibo_m, 2},
	{Yi32, Ynone, Yax, Zil_, 1},
	{Yi32, Ynone, Yml, Zilo_m, 2},
	{Yrl, Ynone, Yml, Zr_m, 1},
	{Yml, Ynone, Yrl, Zm_r, 1},
283 284 285
}

var yaddl = []ytab{
286 287 288 289 290
	{Yi8, Ynone, Yml, Zibo_m, 2},
	{Yi32, Ynone, Yax, Zil_, 1},
	{Yi32, Ynone, Yml, Zilo_m, 2},
	{Yrl, Ynone, Yml, Zr_m, 1},
	{Yml, Ynone, Yrl, Zm_r, 1},
291 292 293
}

var yincb = []ytab{
294
	{Ynone, Ynone, Ymb, Zo_m, 2},
295 296 297
}

var yincw = []ytab{
298
	{Ynone, Ynone, Yml, Zo_m, 2},
299 300 301
}

var yincl = []ytab{
302 303 304 305 306
	{Ynone, Ynone, Yrl, Z_rp, 1},
	{Ynone, Ynone, Yml, Zo_m, 2},
}

var yincq = []ytab{
307
	{Ynone, Ynone, Yml, Zo_m, 2},
308 309 310
}

var ycmpb = []ytab{
311 312 313 314
	{Yal, Ynone, Yi32, Z_ib, 1},
	{Ymb, Ynone, Yi32, Zm_ibo, 2},
	{Ymb, Ynone, Yrb, Zm_r, 1},
	{Yrb, Ynone, Ymb, Zr_m, 1},
315 316 317
}

var ycmpl = []ytab{
318 319 320 321 322
	{Yml, Ynone, Yi8, Zm_ibo, 2},
	{Yax, Ynone, Yi32, Z_il, 1},
	{Yml, Ynone, Yi32, Zm_ilo, 2},
	{Yml, Ynone, Yrl, Zm_r, 1},
	{Yrl, Ynone, Yml, Zr_m, 1},
323 324 325
}

var yshb = []ytab{
326 327 328
	{Yi1, Ynone, Ymb, Zo_m, 2},
	{Yi32, Ynone, Ymb, Zibo_m, 2},
	{Ycx, Ynone, Ymb, Zo_m, 2},
329 330 331
}

var yshl = []ytab{
332 333 334 335
	{Yi1, Ynone, Yml, Zo_m, 2},
	{Yi32, Ynone, Yml, Zibo_m, 2},
	{Ycl, Ynone, Yml, Zo_m, 2},
	{Ycx, Ynone, Yml, Zo_m, 2},
336 337 338
}

var ytestb = []ytab{
339 340 341 342
	{Yi32, Ynone, Yal, Zib_, 1},
	{Yi32, Ynone, Ymb, Zibo_m, 2},
	{Yrb, Ynone, Ymb, Zr_m, 1},
	{Ymb, Ynone, Yrb, Zm_r, 1},
343 344 345
}

var ytestl = []ytab{
346 347 348 349
	{Yi32, Ynone, Yax, Zil_, 1},
	{Yi32, Ynone, Yml, Zilo_m, 2},
	{Yrl, Ynone, Yml, Zr_m, 1},
	{Yml, Ynone, Yrl, Zm_r, 1},
350 351 352
}

var ymovb = []ytab{
353 354 355 356
	{Yrb, Ynone, Ymb, Zr_m, 1},
	{Ymb, Ynone, Yrb, Zm_r, 1},
	{Yi32, Ynone, Yrb, Zib_rp, 1},
	{Yi32, Ynone, Ymb, Zibo_m, 2},
357 358 359
}

var ymbs = []ytab{
360
	{Ymb, Ynone, Ynone, Zm_o, 2},
361 362 363
}

var ybtl = []ytab{
364 365
	{Yi8, Ynone, Yml, Zibo_m, 2},
	{Yrl, Ynone, Yml, Zr_m, 1},
366 367 368
}

var ymovw = []ytab{
369 370 371 372 373 374
	{Yrl, Ynone, Yml, Zr_m, 1},
	{Yml, Ynone, Yrl, Zm_r, 1},
	{Yi0, Ynone, Yrl, Zclr, 1},
	{Yi32, Ynone, Yrl, Zil_rp, 1},
	{Yi32, Ynone, Yml, Zilo_m, 2},
	{Yiauto, Ynone, Yrl, Zaut_r, 2},
375 376 377
}

var ymovl = []ytab{
378 379 380 381 382 383 384 385 386 387
	{Yrl, Ynone, Yml, Zr_m, 1},
	{Yml, Ynone, Yrl, Zm_r, 1},
	{Yi0, Ynone, Yrl, Zclr, 1},
	{Yi32, Ynone, Yrl, Zil_rp, 1},
	{Yi32, Ynone, Yml, Zilo_m, 2},
	{Yml, Ynone, Ymr, Zm_r_xm, 1}, // MMX MOVD
	{Ymr, Ynone, Yml, Zr_m_xm, 1}, // MMX MOVD
	{Yml, Ynone, Yxr, Zm_r_xm, 2}, // XMM MOVD (32 bit)
	{Yxr, Ynone, Yml, Zr_m_xm, 2}, // XMM MOVD (32 bit)
	{Yiauto, Ynone, Yrl, Zaut_r, 2},
388 389 390
}

var yret = []ytab{
391 392
	{Ynone, Ynone, Ynone, Zo_iw, 1},
	{Yi32, Ynone, Ynone, Zo_iw, 1},
393 394 395
}

var ymovq = []ytab{
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	// valid in 32-bit mode
	{Ym, Ynone, Ymr, Zm_r_xm_nr, 1},  // 0x6f MMX MOVQ (shorter encoding)
	{Ymr, Ynone, Ym, Zr_m_xm_nr, 1},  // 0x7f MMX MOVQ
	{Yxr, Ynone, Ymr, Zm_r_xm_nr, 2}, // Pf2, 0xd6 MOVDQ2Q
	{Yxm, Ynone, Yxr, Zm_r_xm_nr, 2}, // Pf3, 0x7e MOVQ xmm1/m64 -> xmm2
	{Yxr, Ynone, Yxm, Zr_m_xm_nr, 2}, // Pe, 0xd6 MOVQ xmm1 -> xmm2/m64

	// valid only in 64-bit mode, usually with 64-bit prefix
	{Yrl, Ynone, Yml, Zr_m, 1},      // 0x89
	{Yml, Ynone, Yrl, Zm_r, 1},      // 0x8b
	{Yi0, Ynone, Yrl, Zclr, 1},      // 0x31
	{Ys32, Ynone, Yrl, Zilo_m, 2},   // 32 bit signed 0xc7,(0)
	{Yi64, Ynone, Yrl, Ziq_rp, 1},   // 0xb8 -- 32/64 bit immediate
	{Yi32, Ynone, Yml, Zilo_m, 2},   // 0xc7,(0)
	{Ymm, Ynone, Ymr, Zm_r_xm, 1},   // 0x6e MMX MOVD
	{Ymr, Ynone, Ymm, Zr_m_xm, 1},   // 0x7e MMX MOVD
	{Yml, Ynone, Yxr, Zm_r_xm, 2},   // Pe, 0x6e MOVD xmm load
	{Yxr, Ynone, Yml, Zr_m_xm, 2},   // Pe, 0x7e MOVD xmm store
	{Yiauto, Ynone, Yrl, Zaut_r, 1}, // 0 built-in LEAQ
415 416 417
}

var ym_rl = []ytab{
418
	{Ym, Ynone, Yrl, Zm_r, 1},
419 420 421
}

var yrl_m = []ytab{
422
	{Yrl, Ynone, Ym, Zr_m, 1},
423 424 425
}

var ymb_rl = []ytab{
426
	{Ymb, Ynone, Yrl, Zmb_r, 1},
427 428 429
}

var yml_rl = []ytab{
430
	{Yml, Ynone, Yrl, Zm_r, 1},
431 432 433
}

var yrl_ml = []ytab{
434
	{Yrl, Ynone, Yml, Zr_m, 1},
435 436 437
}

var yml_mb = []ytab{
438 439
	{Yrb, Ynone, Ymb, Zr_m, 1},
	{Ymb, Ynone, Yrb, Zm_r, 1},
440 441 442
}

var yrb_mb = []ytab{
443
	{Yrb, Ynone, Ymb, Zr_m, 1},
444 445 446
}

var yxchg = []ytab{
447 448 449 450
	{Yax, Ynone, Yrl, Z_rp, 1},
	{Yrl, Ynone, Yax, Zrp_, 1},
	{Yrl, Ynone, Yml, Zr_m, 1},
	{Yml, Ynone, Yrl, Zm_r, 1},
451 452 453
}

var ydivl = []ytab{
454
	{Yml, Ynone, Ynone, Zm_o, 2},
455 456 457
}

var ydivb = []ytab{
458
	{Ymb, Ynone, Ynone, Zm_o, 2},
459 460 461
}

var yimul = []ytab{
462 463 464 465
	{Yml, Ynone, Ynone, Zm_o, 2},
	{Yi8, Ynone, Yrl, Zib_rr, 1},
	{Yi32, Ynone, Yrl, Zil_rr, 1},
	{Yml, Ynone, Yrl, Zm_r, 2},
466 467 468
}

var yimul3 = []ytab{
469
	{Yi8, Yml, Yrl, Zibm_r, 2},
470 471 472
}

var ybyte = []ytab{
473
	{Yi64, Ynone, Ynone, Zbyte, 1},
474 475 476
}

var yin = []ytab{
477 478
	{Yi32, Ynone, Ynone, Zib_, 1},
	{Ynone, Ynone, Ynone, Zlit, 1},
479 480 481
}

var yint = []ytab{
482
	{Yi32, Ynone, Ynone, Zib_, 1},
483 484 485
}

var ypushl = []ytab{
486 487 488 489
	{Yrl, Ynone, Ynone, Zrp_, 1},
	{Ym, Ynone, Ynone, Zm_o, 2},
	{Yi8, Ynone, Ynone, Zib_, 1},
	{Yi32, Ynone, Ynone, Zil_, 1},
490 491 492
}

var ypopl = []ytab{
493 494
	{Ynone, Ynone, Yrl, Z_rp, 1},
	{Ynone, Ynone, Ym, Zo_m, 2},
495 496 497
}

var ybswap = []ytab{
498
	{Ynone, Ynone, Yrl, Z_rp, 2},
499 500 501
}

var yscond = []ytab{
502
	{Ynone, Ynone, Ymb, Zo_m, 2},
503 504 505
}

var yjcond = []ytab{
506 507 508
	{Ynone, Ynone, Ybr, Zbr, 0},
	{Yi0, Ynone, Ybr, Zbr, 0},
	{Yi1, Ynone, Ybr, Zbr, 1},
509 510 511
}

var yloop = []ytab{
512
	{Ynone, Ynone, Ybr, Zloop, 1},
513 514 515
}

var ycall = []ytab{
516 517
	{Ynone, Ynone, Yml, Zcallindreg, 0},
	{Yrx, Ynone, Yrx, Zcallindreg, 2},
518 519 520
	{Ynone, Ynone, Yindir, Zcallind, 2},
	{Ynone, Ynone, Ybr, Zcall, 0},
	{Ynone, Ynone, Yi32, Zcallcon, 1},
521 522 523
}

var yduff = []ytab{
524
	{Ynone, Ynone, Yi32, Zcallduff, 1},
525 526 527
}

var yjmp = []ytab{
528
	{Ynone, Ynone, Yml, Zo_m64, 2},
529 530
	{Ynone, Ynone, Ybr, Zjmp, 0},
	{Ynone, Ynone, Yi32, Zjmpcon, 1},
531 532 533
}

var yfmvd = []ytab{
534 535 536 537
	{Ym, Ynone, Yf0, Zm_o, 2},
	{Yf0, Ynone, Ym, Zo_m, 2},
	{Yrf, Ynone, Yf0, Zm_o, 2},
	{Yf0, Ynone, Yrf, Zo_m, 2},
538 539 540
}

var yfmvdp = []ytab{
541 542
	{Yf0, Ynone, Ym, Zo_m, 2},
	{Yf0, Ynone, Yrf, Zo_m, 2},
543 544 545
}

var yfmvf = []ytab{
546 547
	{Ym, Ynone, Yf0, Zm_o, 2},
	{Yf0, Ynone, Ym, Zo_m, 2},
548 549 550
}

var yfmvx = []ytab{
551
	{Ym, Ynone, Yf0, Zm_o, 2},
552 553 554
}

var yfmvp = []ytab{
555
	{Yf0, Ynone, Ym, Zo_m, 2},
556 557
}

558 559 560 561
var yfcmv = []ytab{
	{Yrf, Ynone, Yf0, Zm_o, 2},
}

562
var yfadd = []ytab{
563 564 565
	{Ym, Ynone, Yf0, Zm_o, 2},
	{Yrf, Ynone, Yf0, Zm_o, 2},
	{Yf0, Ynone, Yrf, Zo_m, 2},
566 567 568
}

var yfaddp = []ytab{
569
	{Yf0, Ynone, Yrf, Zo_m, 2},
570 571 572
}

var yfxch = []ytab{
573 574
	{Yf0, Ynone, Yrf, Zo_m, 2},
	{Yrf, Ynone, Yf0, Zm_o, 2},
575 576 577
}

var ycompp = []ytab{
578
	{Yf0, Ynone, Yrf, Zo_m, 2}, /* botch is really f0,f1 */
579 580 581
}

var ystsw = []ytab{
582 583
	{Ynone, Ynone, Ym, Zo_m, 2},
	{Ynone, Ynone, Yax, Zlit, 1},
584 585 586
}

var ystcw = []ytab{
587 588
	{Ynone, Ynone, Ym, Zo_m, 2},
	{Ym, Ynone, Ynone, Zm_o, 2},
589 590 591
}

var ysvrs = []ytab{
592 593
	{Ynone, Ynone, Ym, Zo_m, 2},
	{Ym, Ynone, Ynone, Zm_o, 2},
594 595 596
}

var ymm = []ytab{
597 598
	{Ymm, Ynone, Ymr, Zm_r_xm, 1},
	{Yxm, Ynone, Yxr, Zm_r_xm, 2},
599 600 601
}

var yxm = []ytab{
602
	{Yxm, Ynone, Yxr, Zm_r_xm, 1},
603 604 605
}

var yxcvm1 = []ytab{
606 607
	{Yxm, Ynone, Yxr, Zm_r_xm, 2},
	{Yxm, Ynone, Ymr, Zm_r_xm, 2},
608 609 610
}

var yxcvm2 = []ytab{
611 612
	{Yxm, Ynone, Yxr, Zm_r_xm, 2},
	{Ymm, Ynone, Yxr, Zm_r_xm, 2},
613 614 615
}

/*
616
var yxmq = []ytab{
617
	{Yxm, Ynone, Yxr, Zm_r_xm, 2},
618
}
619 620
*/

621
var yxr = []ytab{
622
	{Yxr, Ynone, Yxr, Zm_r_xm, 1},
623 624 625
}

var yxr_ml = []ytab{
626
	{Yxr, Ynone, Yml, Zr_m_xm, 1},
627 628
}

629 630 631 632
var yxr_ml_vex = []ytab{
	{Yxr, Ynone, Yml, Zr_m_xm_vex, 1},
}

633
var ymr = []ytab{
634
	{Ymr, Ynone, Ymr, Zm_r, 1},
635 636 637
}

var ymr_ml = []ytab{
638
	{Ymr, Ynone, Yml, Zr_m_xm, 1},
639 640 641
}

var yxcmp = []ytab{
642
	{Yxm, Ynone, Yxr, Zm_r_xm, 1},
643 644 645
}

var yxcmpi = []ytab{
646
	{Yxm, Yxr, Yi8, Zm_r_i_xm, 2},
647 648
}

649 650 651 652 653
var yxmov_vex = []ytab{
	{Yxm, Ynone, Yxr, Zm_r_xm_vex, 1},
	{Yxr, Ynone, Yxm, Zr_m_xm_vex, 1},
}

654
var yxmov = []ytab{
655 656
	{Yxm, Ynone, Yxr, Zm_r_xm, 1},
	{Yxr, Ynone, Yxm, Zr_m_xm, 1},
657 658 659
}

var yxcvfl = []ytab{
660
	{Yxm, Ynone, Yrl, Zm_r_xm, 1},
661 662 663
}

var yxcvlf = []ytab{
664
	{Yml, Ynone, Yxr, Zm_r_xm, 1},
665 666 667
}

var yxcvfq = []ytab{
668
	{Yxm, Ynone, Yrl, Zm_r_xm, 2},
669 670 671
}

var yxcvqf = []ytab{
672
	{Yml, Ynone, Yxr, Zm_r_xm, 2},
673 674 675
}

var yps = []ytab{
676 677 678 679
	{Ymm, Ynone, Ymr, Zm_r_xm, 1},
	{Yi8, Ynone, Ymr, Zibo_m_xm, 2},
	{Yxm, Ynone, Yxr, Zm_r_xm, 2},
	{Yi8, Ynone, Yxr, Zibo_m_xm, 3},
680 681 682
}

var yxrrl = []ytab{
683
	{Yxr, Ynone, Yrl, Zm_r, 1},
684 685 686
}

var ymfp = []ytab{
687
	{Ymm, Ynone, Ymr, Zm_r_3d, 1},
688 689 690
}

var ymrxr = []ytab{
691 692
	{Ymr, Ynone, Yxr, Zm_r, 1},
	{Yxm, Ynone, Yxr, Zm_r_xm, 1},
693
}
694

695
var ymshuf = []ytab{
696
	{Yi8, Ymm, Ymr, Zibm_r, 2},
697 698
}

699
var ymshufb = []ytab{
700
	{Yxm, Ynone, Yxr, Zm2_r, 2},
701 702
}

703
var yxshuf = []ytab{
704
	{Yu8, Yxm, Yxr, Zibm_r, 2},
705 706 707
}

var yextrw = []ytab{
708
	{Yu8, Yxr, Yrl, Zibm_r, 2},
709 710 711
}

var yinsrw = []ytab{
712
	{Yu8, Yml, Yxr, Zibm_r, 2},
713 714 715
}

var yinsr = []ytab{
716
	{Yu8, Ymm, Yxr, Zibm_r, 3},
717 718 719
}

var ypsdq = []ytab{
720
	{Yi8, Ynone, Yxr, Zibo_m, 2},
721 722 723
}

var ymskb = []ytab{
724 725
	{Yxr, Ynone, Yrl, Zm_r_xm, 2},
	{Ymr, Ynone, Yrl, Zm_r_xm, 1},
726 727 728
}

var ycrc32l = []ytab{
729
	{Yml, Ynone, Yrl, Zlitm_r, 0},
730 731 732
}

var yprefetch = []ytab{
733
	{Ym, Ynone, Ynone, Zm_o, 2},
734 735 736
}

var yaes = []ytab{
737
	{Yxm, Ynone, Yxr, Zlitm_r, 2},
738 739 740
}

var yaes2 = []ytab{
741
	{Yu8, Yxm, Yxr, Zibm_r, 2},
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
}

/*
 * You are doasm, holding in your hand a Prog* with p->as set to, say, ACRC32,
 * and p->from and p->to as operands (Addr*).  The linker scans optab to find
 * the entry with the given p->as and then looks through the ytable for that
 * instruction (the second field in the optab struct) for a line whose first
 * two values match the Ytypes of the p->from and p->to operands.  The function
 * oclass in span.c computes the specific Ytype of an operand and then the set
 * of more general Ytypes that it satisfies is implied by the ycover table, set
 * up in instinit.  For example, oclass distinguishes the constants 0 and 1
 * from the more general 8-bit constants, but instinit says
 *
 *        ycover[Yi0*Ymax + Ys32] = 1;
 *        ycover[Yi1*Ymax + Ys32] = 1;
 *        ycover[Yi8*Ymax + Ys32] = 1;
 *
 * which means that Yi0, Yi1, and Yi8 all count as Ys32 (signed 32)
 * if that's what an instruction can handle.
 *
 * In parallel with the scan through the ytable for the appropriate line, there
 * is a z pointer that starts out pointing at the strange magic byte list in
 * the Optab struct.  With each step past a non-matching ytable line, z
 * advances by the 4th entry in the line.  When a matching line is found, that
 * z pointer has the extra data to use in laying down the instruction bytes.
 * The actual bytes laid down are a function of the 3rd entry in the line (that
 * is, the Ztype) and the z bytes.
 *
 * For example, let's look at AADDL.  The optab line says:
 *        { AADDL,        yaddl,  Px, 0x83,(00),0x05,0x81,(00),0x01,0x03 },
 *
 * and yaddl says
 *        uchar   yaddl[] =
 *        {
 *                Yi8,    Yml,    Zibo_m, 2,
 *                Yi32,   Yax,    Zil_,   1,
 *                Yi32,   Yml,    Zilo_m, 2,
 *                Yrl,    Yml,    Zr_m,   1,
 *                Yml,    Yrl,    Zm_r,   1,
 *                0
 *        };
 *
 * so there are 5 possible types of ADDL instruction that can be laid down, and
 * possible states used to lay them down (Ztype and z pointer, assuming z
 * points at {0x83,(00),0x05,0x81,(00),0x01,0x03}) are:
 *
 *        Yi8, Yml -> Zibo_m, z (0x83, 00)
 *        Yi32, Yax -> Zil_, z+2 (0x05)
 *        Yi32, Yml -> Zilo_m, z+2+1 (0x81, 0x00)
 *        Yrl, Yml -> Zr_m, z+2+1+2 (0x01)
 *        Yml, Yrl -> Zm_r, z+2+1+2+1 (0x03)
 *
 * The Pconstant in the optab line controls the prefix bytes to emit.  That's
 * relatively straightforward as this program goes.
 *
 * The switch on t[2] in doasm implements the various Z cases.  Zibo_m, for
 * example, is an opcode byte (z[0]) then an asmando (which is some kind of
 * encoded addressing mode for the Yml arg), and then a single immediate byte.
 * Zilo_m is the same but a long (32-bit) immediate.
 */
var optab =
/*	as, ytab, andproto, opcode */
[]Optab{
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
	{obj.AXXX, nil, 0, [23]uint8{}},
	{AAAA, ynone, P32, [23]uint8{0x37}},
	{AAAD, ynone, P32, [23]uint8{0xd5, 0x0a}},
	{AAAM, ynone, P32, [23]uint8{0xd4, 0x0a}},
	{AAAS, ynone, P32, [23]uint8{0x3f}},
	{AADCB, yxorb, Pb, [23]uint8{0x14, 0x80, 02, 0x10, 0x10}},
	{AADCL, yxorl, Px, [23]uint8{0x83, 02, 0x15, 0x81, 02, 0x11, 0x13}},
	{AADCQ, yxorl, Pw, [23]uint8{0x83, 02, 0x15, 0x81, 02, 0x11, 0x13}},
	{AADCW, yxorl, Pe, [23]uint8{0x83, 02, 0x15, 0x81, 02, 0x11, 0x13}},
	{AADDB, yxorb, Pb, [23]uint8{0x04, 0x80, 00, 0x00, 0x02}},
	{AADDL, yaddl, Px, [23]uint8{0x83, 00, 0x05, 0x81, 00, 0x01, 0x03}},
	{AADDPD, yxm, Pq, [23]uint8{0x58}},
	{AADDPS, yxm, Pm, [23]uint8{0x58}},
	{AADDQ, yaddl, Pw, [23]uint8{0x83, 00, 0x05, 0x81, 00, 0x01, 0x03}},
	{AADDSD, yxm, Pf2, [23]uint8{0x58}},
	{AADDSS, yxm, Pf3, [23]uint8{0x58}},
	{AADDW, yaddl, Pe, [23]uint8{0x83, 00, 0x05, 0x81, 00, 0x01, 0x03}},
	{AADJSP, nil, 0, [23]uint8{}},
	{AANDB, yxorb, Pb, [23]uint8{0x24, 0x80, 04, 0x20, 0x22}},
	{AANDL, yxorl, Px, [23]uint8{0x83, 04, 0x25, 0x81, 04, 0x21, 0x23}},
	{AANDNPD, yxm, Pq, [23]uint8{0x55}},
	{AANDNPS, yxm, Pm, [23]uint8{0x55}},
	{AANDPD, yxm, Pq, [23]uint8{0x54}},
	{AANDPS, yxm, Pq, [23]uint8{0x54}},
	{AANDQ, yxorl, Pw, [23]uint8{0x83, 04, 0x25, 0x81, 04, 0x21, 0x23}},
	{AANDW, yxorl, Pe, [23]uint8{0x83, 04, 0x25, 0x81, 04, 0x21, 0x23}},
	{AARPL, yrl_ml, P32, [23]uint8{0x63}},
	{ABOUNDL, yrl_m, P32, [23]uint8{0x62}},
	{ABOUNDW, yrl_m, Pe, [23]uint8{0x62}},
	{ABSFL, yml_rl, Pm, [23]uint8{0xbc}},
	{ABSFQ, yml_rl, Pw, [23]uint8{0x0f, 0xbc}},
	{ABSFW, yml_rl, Pq, [23]uint8{0xbc}},
	{ABSRL, yml_rl, Pm, [23]uint8{0xbd}},
	{ABSRQ, yml_rl, Pw, [23]uint8{0x0f, 0xbd}},
	{ABSRW, yml_rl, Pq, [23]uint8{0xbd}},
	{ABSWAPL, ybswap, Px, [23]uint8{0x0f, 0xc8}},
	{ABSWAPQ, ybswap, Pw, [23]uint8{0x0f, 0xc8}},
	{ABTCL, ybtl, Pm, [23]uint8{0xba, 07, 0xbb}},
	{ABTCQ, ybtl, Pw, [23]uint8{0x0f, 0xba, 07, 0x0f, 0xbb}},
	{ABTCW, ybtl, Pq, [23]uint8{0xba, 07, 0xbb}},
	{ABTL, ybtl, Pm, [23]uint8{0xba, 04, 0xa3}},
	{ABTQ, ybtl, Pw, [23]uint8{0x0f, 0xba, 04, 0x0f, 0xa3}},
	{ABTRL, ybtl, Pm, [23]uint8{0xba, 06, 0xb3}},
	{ABTRQ, ybtl, Pw, [23]uint8{0x0f, 0xba, 06, 0x0f, 0xb3}},
	{ABTRW, ybtl, Pq, [23]uint8{0xba, 06, 0xb3}},
	{ABTSL, ybtl, Pm, [23]uint8{0xba, 05, 0xab}},
	{ABTSQ, ybtl, Pw, [23]uint8{0x0f, 0xba, 05, 0x0f, 0xab}},
	{ABTSW, ybtl, Pq, [23]uint8{0xba, 05, 0xab}},
	{ABTW, ybtl, Pq, [23]uint8{0xba, 04, 0xa3}},
	{ABYTE, ybyte, Px, [23]uint8{1}},
	{obj.ACALL, ycall, Px, [23]uint8{0xff, 02, 0xff, 0x15, 0xe8}},
	{ACDQ, ynone, Px, [23]uint8{0x99}},
	{ACLC, ynone, Px, [23]uint8{0xf8}},
	{ACLD, ynone, Px, [23]uint8{0xfc}},
	{ACLI, ynone, Px, [23]uint8{0xfa}},
	{ACLTS, ynone, Pm, [23]uint8{0x06}},
	{ACMC, ynone, Px, [23]uint8{0xf5}},
	{ACMOVLCC, yml_rl, Pm, [23]uint8{0x43}},
	{ACMOVLCS, yml_rl, Pm, [23]uint8{0x42}},
	{ACMOVLEQ, yml_rl, Pm, [23]uint8{0x44}},
	{ACMOVLGE, yml_rl, Pm, [23]uint8{0x4d}},
	{ACMOVLGT, yml_rl, Pm, [23]uint8{0x4f}},
	{ACMOVLHI, yml_rl, Pm, [23]uint8{0x47}},
	{ACMOVLLE, yml_rl, Pm, [23]uint8{0x4e}},
	{ACMOVLLS, yml_rl, Pm, [23]uint8{0x46}},
	{ACMOVLLT, yml_rl, Pm, [23]uint8{0x4c}},
	{ACMOVLMI, yml_rl, Pm, [23]uint8{0x48}},
	{ACMOVLNE, yml_rl, Pm, [23]uint8{0x45}},
	{ACMOVLOC, yml_rl, Pm, [23]uint8{0x41}},
	{ACMOVLOS, yml_rl, Pm, [23]uint8{0x40}},
	{ACMOVLPC, yml_rl, Pm, [23]uint8{0x4b}},
	{ACMOVLPL, yml_rl, Pm, [23]uint8{0x49}},
	{ACMOVLPS, yml_rl, Pm, [23]uint8{0x4a}},
	{ACMOVQCC, yml_rl, Pw, [23]uint8{0x0f, 0x43}},
	{ACMOVQCS, yml_rl, Pw, [23]uint8{0x0f, 0x42}},
	{ACMOVQEQ, yml_rl, Pw, [23]uint8{0x0f, 0x44}},
	{ACMOVQGE, yml_rl, Pw, [23]uint8{0x0f, 0x4d}},
	{ACMOVQGT, yml_rl, Pw, [23]uint8{0x0f, 0x4f}},
	{ACMOVQHI, yml_rl, Pw, [23]uint8{0x0f, 0x47}},
	{ACMOVQLE, yml_rl, Pw, [23]uint8{0x0f, 0x4e}},
	{ACMOVQLS, yml_rl, Pw, [23]uint8{0x0f, 0x46}},
	{ACMOVQLT, yml_rl, Pw, [23]uint8{0x0f, 0x4c}},
	{ACMOVQMI, yml_rl, Pw, [23]uint8{0x0f, 0x48}},
	{ACMOVQNE, yml_rl, Pw, [23]uint8{0x0f, 0x45}},
	{ACMOVQOC, yml_rl, Pw, [23]uint8{0x0f, 0x41}},
	{ACMOVQOS, yml_rl, Pw, [23]uint8{0x0f, 0x40}},
	{ACMOVQPC, yml_rl, Pw, [23]uint8{0x0f, 0x4b}},
	{ACMOVQPL, yml_rl, Pw, [23]uint8{0x0f, 0x49}},
	{ACMOVQPS, yml_rl, Pw, [23]uint8{0x0f, 0x4a}},
	{ACMOVWCC, yml_rl, Pq, [23]uint8{0x43}},
	{ACMOVWCS, yml_rl, Pq, [23]uint8{0x42}},
	{ACMOVWEQ, yml_rl, Pq, [23]uint8{0x44}},
	{ACMOVWGE, yml_rl, Pq, [23]uint8{0x4d}},
	{ACMOVWGT, yml_rl, Pq, [23]uint8{0x4f}},
	{ACMOVWHI, yml_rl, Pq, [23]uint8{0x47}},
	{ACMOVWLE, yml_rl, Pq, [23]uint8{0x4e}},
	{ACMOVWLS, yml_rl, Pq, [23]uint8{0x46}},
	{ACMOVWLT, yml_rl, Pq, [23]uint8{0x4c}},
	{ACMOVWMI, yml_rl, Pq, [23]uint8{0x48}},
	{ACMOVWNE, yml_rl, Pq, [23]uint8{0x45}},
	{ACMOVWOC, yml_rl, Pq, [23]uint8{0x41}},
	{ACMOVWOS, yml_rl, Pq, [23]uint8{0x40}},
	{ACMOVWPC, yml_rl, Pq, [23]uint8{0x4b}},
	{ACMOVWPL, yml_rl, Pq, [23]uint8{0x49}},
	{ACMOVWPS, yml_rl, Pq, [23]uint8{0x4a}},
	{ACMPB, ycmpb, Pb, [23]uint8{0x3c, 0x80, 07, 0x38, 0x3a}},
	{ACMPL, ycmpl, Px, [23]uint8{0x83, 07, 0x3d, 0x81, 07, 0x39, 0x3b}},
	{ACMPPD, yxcmpi, Px, [23]uint8{Pe, 0xc2}},
	{ACMPPS, yxcmpi, Pm, [23]uint8{0xc2, 0}},
	{ACMPQ, ycmpl, Pw, [23]uint8{0x83, 07, 0x3d, 0x81, 07, 0x39, 0x3b}},
	{ACMPSB, ynone, Pb, [23]uint8{0xa6}},
	{ACMPSD, yxcmpi, Px, [23]uint8{Pf2, 0xc2}},
	{ACMPSL, ynone, Px, [23]uint8{0xa7}},
	{ACMPSQ, ynone, Pw, [23]uint8{0xa7}},
	{ACMPSS, yxcmpi, Px, [23]uint8{Pf3, 0xc2}},
	{ACMPSW, ynone, Pe, [23]uint8{0xa7}},
	{ACMPW, ycmpl, Pe, [23]uint8{0x83, 07, 0x3d, 0x81, 07, 0x39, 0x3b}},
	{ACOMISD, yxcmp, Pe, [23]uint8{0x2f}},
	{ACOMISS, yxcmp, Pm, [23]uint8{0x2f}},
	{ACPUID, ynone, Pm, [23]uint8{0xa2}},
	{ACVTPL2PD, yxcvm2, Px, [23]uint8{Pf3, 0xe6, Pe, 0x2a}},
	{ACVTPL2PS, yxcvm2, Pm, [23]uint8{0x5b, 0, 0x2a, 0}},
	{ACVTPD2PL, yxcvm1, Px, [23]uint8{Pf2, 0xe6, Pe, 0x2d}},
	{ACVTPD2PS, yxm, Pe, [23]uint8{0x5a}},
	{ACVTPS2PL, yxcvm1, Px, [23]uint8{Pe, 0x5b, Pm, 0x2d}},
	{ACVTPS2PD, yxm, Pm, [23]uint8{0x5a}},
	{API2FW, ymfp, Px, [23]uint8{0x0c}},
	{ACVTSD2SL, yxcvfl, Pf2, [23]uint8{0x2d}},
	{ACVTSD2SQ, yxcvfq, Pw, [23]uint8{Pf2, 0x2d}},
	{ACVTSD2SS, yxm, Pf2, [23]uint8{0x5a}},
	{ACVTSL2SD, yxcvlf, Pf2, [23]uint8{0x2a}},
	{ACVTSQ2SD, yxcvqf, Pw, [23]uint8{Pf2, 0x2a}},
	{ACVTSL2SS, yxcvlf, Pf3, [23]uint8{0x2a}},
	{ACVTSQ2SS, yxcvqf, Pw, [23]uint8{Pf3, 0x2a}},
	{ACVTSS2SD, yxm, Pf3, [23]uint8{0x5a}},
	{ACVTSS2SL, yxcvfl, Pf3, [23]uint8{0x2d}},
	{ACVTSS2SQ, yxcvfq, Pw, [23]uint8{Pf3, 0x2d}},
	{ACVTTPD2PL, yxcvm1, Px, [23]uint8{Pe, 0xe6, Pe, 0x2c}},
	{ACVTTPS2PL, yxcvm1, Px, [23]uint8{Pf3, 0x5b, Pm, 0x2c}},
	{ACVTTSD2SL, yxcvfl, Pf2, [23]uint8{0x2c}},
	{ACVTTSD2SQ, yxcvfq, Pw, [23]uint8{Pf2, 0x2c}},
	{ACVTTSS2SL, yxcvfl, Pf3, [23]uint8{0x2c}},
	{ACVTTSS2SQ, yxcvfq, Pw, [23]uint8{Pf3, 0x2c}},
	{ACWD, ynone, Pe, [23]uint8{0x99}},
	{ACQO, ynone, Pw, [23]uint8{0x99}},
	{ADAA, ynone, P32, [23]uint8{0x27}},
	{ADAS, ynone, P32, [23]uint8{0x2f}},
	{obj.ADATA, nil, 0, [23]uint8{}},
	{ADECB, yincb, Pb, [23]uint8{0xfe, 01}},
	{ADECL, yincl, Px1, [23]uint8{0x48, 0xff, 01}},
	{ADECQ, yincq, Pw, [23]uint8{0xff, 01}},
	{ADECW, yincw, Pe, [23]uint8{0xff, 01}},
	{ADIVB, ydivb, Pb, [23]uint8{0xf6, 06}},
	{ADIVL, ydivl, Px, [23]uint8{0xf7, 06}},
	{ADIVPD, yxm, Pe, [23]uint8{0x5e}},
	{ADIVPS, yxm, Pm, [23]uint8{0x5e}},
	{ADIVQ, ydivl, Pw, [23]uint8{0xf7, 06}},
	{ADIVSD, yxm, Pf2, [23]uint8{0x5e}},
	{ADIVSS, yxm, Pf3, [23]uint8{0x5e}},
	{ADIVW, ydivl, Pe, [23]uint8{0xf7, 06}},
	{AEMMS, ynone, Pm, [23]uint8{0x77}},
	{AENTER, nil, 0, [23]uint8{}}, /* botch */
	{AFXRSTOR, ysvrs, Pm, [23]uint8{0xae, 01, 0xae, 01}},
	{AFXSAVE, ysvrs, Pm, [23]uint8{0xae, 00, 0xae, 00}},
	{AFXRSTOR64, ysvrs, Pw, [23]uint8{0x0f, 0xae, 01, 0x0f, 0xae, 01}},
	{AFXSAVE64, ysvrs, Pw, [23]uint8{0x0f, 0xae, 00, 0x0f, 0xae, 00}},
	{obj.AGLOBL, nil, 0, [23]uint8{}},
	{AHLT, ynone, Px, [23]uint8{0xf4}},
	{AIDIVB, ydivb, Pb, [23]uint8{0xf6, 07}},
	{AIDIVL, ydivl, Px, [23]uint8{0xf7, 07}},
	{AIDIVQ, ydivl, Pw, [23]uint8{0xf7, 07}},
	{AIDIVW, ydivl, Pe, [23]uint8{0xf7, 07}},
	{AIMULB, ydivb, Pb, [23]uint8{0xf6, 05}},
	{AIMULL, yimul, Px, [23]uint8{0xf7, 05, 0x6b, 0x69, Pm, 0xaf}},
	{AIMULQ, yimul, Pw, [23]uint8{0xf7, 05, 0x6b, 0x69, Pm, 0xaf}},
	{AIMULW, yimul, Pe, [23]uint8{0xf7, 05, 0x6b, 0x69, Pm, 0xaf}},
	{AIMUL3Q, yimul3, Pw, [23]uint8{0x6b, 00}},
	{AINB, yin, Pb, [23]uint8{0xe4, 0xec}},
	{AINCB, yincb, Pb, [23]uint8{0xfe, 00}},
	{AINCL, yincl, Px1, [23]uint8{0x40, 0xff, 00}},
	{AINCQ, yincq, Pw, [23]uint8{0xff, 00}},
	{AINCW, yincw, Pe, [23]uint8{0xff, 00}},
	{AINL, yin, Px, [23]uint8{0xe5, 0xed}},
	{AINSB, ynone, Pb, [23]uint8{0x6c}},
	{AINSL, ynone, Px, [23]uint8{0x6d}},
	{AINSW, ynone, Pe, [23]uint8{0x6d}},
	{AINT, yint, Px, [23]uint8{0xcd}},
	{AINTO, ynone, P32, [23]uint8{0xce}},
	{AINW, yin, Pe, [23]uint8{0xe5, 0xed}},
	{AIRETL, ynone, Px, [23]uint8{0xcf}},
	{AIRETQ, ynone, Pw, [23]uint8{0xcf}},
	{AIRETW, ynone, Pe, [23]uint8{0xcf}},
	{AJCC, yjcond, Px, [23]uint8{0x73, 0x83, 00}},
	{AJCS, yjcond, Px, [23]uint8{0x72, 0x82}},
	{AJCXZL, yloop, Px, [23]uint8{0xe3}},
	{AJCXZW, yloop, Px, [23]uint8{0xe3}},
	{AJCXZQ, yloop, Px, [23]uint8{0xe3}},
	{AJEQ, yjcond, Px, [23]uint8{0x74, 0x84}},
	{AJGE, yjcond, Px, [23]uint8{0x7d, 0x8d}},
	{AJGT, yjcond, Px, [23]uint8{0x7f, 0x8f}},
	{AJHI, yjcond, Px, [23]uint8{0x77, 0x87}},
	{AJLE, yjcond, Px, [23]uint8{0x7e, 0x8e}},
	{AJLS, yjcond, Px, [23]uint8{0x76, 0x86}},
	{AJLT, yjcond, Px, [23]uint8{0x7c, 0x8c}},
	{AJMI, yjcond, Px, [23]uint8{0x78, 0x88}},
	{obj.AJMP, yjmp, Px, [23]uint8{0xff, 04, 0xeb, 0xe9}},
	{AJNE, yjcond, Px, [23]uint8{0x75, 0x85}},
	{AJOC, yjcond, Px, [23]uint8{0x71, 0x81, 00}},
	{AJOS, yjcond, Px, [23]uint8{0x70, 0x80, 00}},
	{AJPC, yjcond, Px, [23]uint8{0x7b, 0x8b}},
	{AJPL, yjcond, Px, [23]uint8{0x79, 0x89}},
	{AJPS, yjcond, Px, [23]uint8{0x7a, 0x8a}},
	{ALAHF, ynone, Px, [23]uint8{0x9f}},
	{ALARL, yml_rl, Pm, [23]uint8{0x02}},
	{ALARW, yml_rl, Pq, [23]uint8{0x02}},
	{ALDMXCSR, ysvrs, Pm, [23]uint8{0xae, 02, 0xae, 02}},
	{ALEAL, ym_rl, Px, [23]uint8{0x8d}},
	{ALEAQ, ym_rl, Pw, [23]uint8{0x8d}},
	{ALEAVEL, ynone, P32, [23]uint8{0xc9}},
	{ALEAVEQ, ynone, Py, [23]uint8{0xc9}},
	{ALEAVEW, ynone, Pe, [23]uint8{0xc9}},
	{ALEAW, ym_rl, Pe, [23]uint8{0x8d}},
	{ALOCK, ynone, Px, [23]uint8{0xf0}},
	{ALODSB, ynone, Pb, [23]uint8{0xac}},
	{ALODSL, ynone, Px, [23]uint8{0xad}},
	{ALODSQ, ynone, Pw, [23]uint8{0xad}},
	{ALODSW, ynone, Pe, [23]uint8{0xad}},
	{ALONG, ybyte, Px, [23]uint8{4}},
	{ALOOP, yloop, Px, [23]uint8{0xe2}},
	{ALOOPEQ, yloop, Px, [23]uint8{0xe1}},
	{ALOOPNE, yloop, Px, [23]uint8{0xe0}},
	{ALSLL, yml_rl, Pm, [23]uint8{0x03}},
	{ALSLW, yml_rl, Pq, [23]uint8{0x03}},
	{AMASKMOVOU, yxr, Pe, [23]uint8{0xf7}},
	{AMASKMOVQ, ymr, Pm, [23]uint8{0xf7}},
	{AMAXPD, yxm, Pe, [23]uint8{0x5f}},
	{AMAXPS, yxm, Pm, [23]uint8{0x5f}},
	{AMAXSD, yxm, Pf2, [23]uint8{0x5f}},
	{AMAXSS, yxm, Pf3, [23]uint8{0x5f}},
	{AMINPD, yxm, Pe, [23]uint8{0x5d}},
	{AMINPS, yxm, Pm, [23]uint8{0x5d}},
	{AMINSD, yxm, Pf2, [23]uint8{0x5d}},
	{AMINSS, yxm, Pf3, [23]uint8{0x5d}},
	{AMOVAPD, yxmov, Pe, [23]uint8{0x28, 0x29}},
	{AMOVAPS, yxmov, Pm, [23]uint8{0x28, 0x29}},
	{AMOVB, ymovb, Pb, [23]uint8{0x88, 0x8a, 0xb0, 0xc6, 00}},
	{AMOVBLSX, ymb_rl, Pm, [23]uint8{0xbe}},
	{AMOVBLZX, ymb_rl, Pm, [23]uint8{0xb6}},
	{AMOVBQSX, ymb_rl, Pw, [23]uint8{0x0f, 0xbe}},
	{AMOVBQZX, ymb_rl, Pm, [23]uint8{0xb6}},
	{AMOVBWSX, ymb_rl, Pq, [23]uint8{0xbe}},
	{AMOVBWZX, ymb_rl, Pq, [23]uint8{0xb6}},
	{AMOVO, yxmov, Pe, [23]uint8{0x6f, 0x7f}},
	{AMOVOU, yxmov, Pf3, [23]uint8{0x6f, 0x7f}},
	{AMOVHLPS, yxr, Pm, [23]uint8{0x12}},
	{AMOVHPD, yxmov, Pe, [23]uint8{0x16, 0x17}},
	{AMOVHPS, yxmov, Pm, [23]uint8{0x16, 0x17}},
	{AMOVL, ymovl, Px, [23]uint8{0x89, 0x8b, 0x31, 0xb8, 0xc7, 00, 0x6e, 0x7e, Pe, 0x6e, Pe, 0x7e, 0}},
	{AMOVLHPS, yxr, Pm, [23]uint8{0x16}},
	{AMOVLPD, yxmov, Pe, [23]uint8{0x12, 0x13}},
	{AMOVLPS, yxmov, Pm, [23]uint8{0x12, 0x13}},
	{AMOVLQSX, yml_rl, Pw, [23]uint8{0x63}},
	{AMOVLQZX, yml_rl, Px, [23]uint8{0x8b}},
	{AMOVMSKPD, yxrrl, Pq, [23]uint8{0x50}},
	{AMOVMSKPS, yxrrl, Pm, [23]uint8{0x50}},
	{AMOVNTO, yxr_ml, Pe, [23]uint8{0xe7}},
	{AMOVNTPD, yxr_ml, Pe, [23]uint8{0x2b}},
	{AMOVNTPS, yxr_ml, Pm, [23]uint8{0x2b}},
	{AMOVNTQ, ymr_ml, Pm, [23]uint8{0xe7}},
	{AMOVQ, ymovq, Pw8, [23]uint8{0x6f, 0x7f, Pf2, 0xd6, Pf3, 0x7e, Pe, 0xd6, 0x89, 0x8b, 0x31, 0xc7, 00, 0xb8, 0xc7, 00, 0x6e, 0x7e, Pe, 0x6e, Pe, 0x7e, 0}},
	{AMOVQOZX, ymrxr, Pf3, [23]uint8{0xd6, 0x7e}},
	{AMOVSB, ynone, Pb, [23]uint8{0xa4}},
	{AMOVSD, yxmov, Pf2, [23]uint8{0x10, 0x11}},
	{AMOVSL, ynone, Px, [23]uint8{0xa5}},
	{AMOVSQ, ynone, Pw, [23]uint8{0xa5}},
	{AMOVSS, yxmov, Pf3, [23]uint8{0x10, 0x11}},
	{AMOVSW, ynone, Pe, [23]uint8{0xa5}},
	{AMOVUPD, yxmov, Pe, [23]uint8{0x10, 0x11}},
	{AMOVUPS, yxmov, Pm, [23]uint8{0x10, 0x11}},
	{AMOVW, ymovw, Pe, [23]uint8{0x89, 0x8b, 0x31, 0xb8, 0xc7, 00, 0}},
	{AMOVWLSX, yml_rl, Pm, [23]uint8{0xbf}},
	{AMOVWLZX, yml_rl, Pm, [23]uint8{0xb7}},
	{AMOVWQSX, yml_rl, Pw, [23]uint8{0x0f, 0xbf}},
	{AMOVWQZX, yml_rl, Pw, [23]uint8{0x0f, 0xb7}},
	{AMULB, ydivb, Pb, [23]uint8{0xf6, 04}},
	{AMULL, ydivl, Px, [23]uint8{0xf7, 04}},
	{AMULPD, yxm, Pe, [23]uint8{0x59}},
	{AMULPS, yxm, Ym, [23]uint8{0x59}},
	{AMULQ, ydivl, Pw, [23]uint8{0xf7, 04}},
	{AMULSD, yxm, Pf2, [23]uint8{0x59}},
	{AMULSS, yxm, Pf3, [23]uint8{0x59}},
	{AMULW, ydivl, Pe, [23]uint8{0xf7, 04}},
	{ANEGB, yscond, Pb, [23]uint8{0xf6, 03}},
	{ANEGL, yscond, Px, [23]uint8{0xf7, 03}},
	{ANEGQ, yscond, Pw, [23]uint8{0xf7, 03}},
	{ANEGW, yscond, Pe, [23]uint8{0xf7, 03}},
	{obj.ANOP, ynop, Px, [23]uint8{0, 0}},
	{ANOTB, yscond, Pb, [23]uint8{0xf6, 02}},
	{ANOTL, yscond, Px, [23]uint8{0xf7, 02}}, // TODO(rsc): yscond is wrong here.
	{ANOTQ, yscond, Pw, [23]uint8{0xf7, 02}},
	{ANOTW, yscond, Pe, [23]uint8{0xf7, 02}},
	{AORB, yxorb, Pb, [23]uint8{0x0c, 0x80, 01, 0x08, 0x0a}},
	{AORL, yxorl, Px, [23]uint8{0x83, 01, 0x0d, 0x81, 01, 0x09, 0x0b}},
	{AORPD, yxm, Pq, [23]uint8{0x56}},
	{AORPS, yxm, Pm, [23]uint8{0x56}},
	{AORQ, yxorl, Pw, [23]uint8{0x83, 01, 0x0d, 0x81, 01, 0x09, 0x0b}},
	{AORW, yxorl, Pe, [23]uint8{0x83, 01, 0x0d, 0x81, 01, 0x09, 0x0b}},
	{AOUTB, yin, Pb, [23]uint8{0xe6, 0xee}},
	{AOUTL, yin, Px, [23]uint8{0xe7, 0xef}},
	{AOUTSB, ynone, Pb, [23]uint8{0x6e}},
	{AOUTSL, ynone, Px, [23]uint8{0x6f}},
	{AOUTSW, ynone, Pe, [23]uint8{0x6f}},
	{AOUTW, yin, Pe, [23]uint8{0xe7, 0xef}},
	{APACKSSLW, ymm, Py1, [23]uint8{0x6b, Pe, 0x6b}},
	{APACKSSWB, ymm, Py1, [23]uint8{0x63, Pe, 0x63}},
	{APACKUSWB, ymm, Py1, [23]uint8{0x67, Pe, 0x67}},
	{APADDB, ymm, Py1, [23]uint8{0xfc, Pe, 0xfc}},
	{APADDL, ymm, Py1, [23]uint8{0xfe, Pe, 0xfe}},
	{APADDQ, yxm, Pe, [23]uint8{0xd4}},
	{APADDSB, ymm, Py1, [23]uint8{0xec, Pe, 0xec}},
	{APADDSW, ymm, Py1, [23]uint8{0xed, Pe, 0xed}},
	{APADDUSB, ymm, Py1, [23]uint8{0xdc, Pe, 0xdc}},
	{APADDUSW, ymm, Py1, [23]uint8{0xdd, Pe, 0xdd}},
	{APADDW, ymm, Py1, [23]uint8{0xfd, Pe, 0xfd}},
	{APAND, ymm, Py1, [23]uint8{0xdb, Pe, 0xdb}},
	{APANDN, ymm, Py1, [23]uint8{0xdf, Pe, 0xdf}},
	{APAUSE, ynone, Px, [23]uint8{0xf3, 0x90}},
	{APAVGB, ymm, Py1, [23]uint8{0xe0, Pe, 0xe0}},
	{APAVGW, ymm, Py1, [23]uint8{0xe3, Pe, 0xe3}},
	{APCMPEQB, ymm, Py1, [23]uint8{0x74, Pe, 0x74}},
	{APCMPEQL, ymm, Py1, [23]uint8{0x76, Pe, 0x76}},
	{APCMPEQW, ymm, Py1, [23]uint8{0x75, Pe, 0x75}},
	{APCMPGTB, ymm, Py1, [23]uint8{0x64, Pe, 0x64}},
	{APCMPGTL, ymm, Py1, [23]uint8{0x66, Pe, 0x66}},
	{APCMPGTW, ymm, Py1, [23]uint8{0x65, Pe, 0x65}},
	{APEXTRW, yextrw, Pq, [23]uint8{0xc5, 00}},
	{APF2IL, ymfp, Px, [23]uint8{0x1d}},
	{APF2IW, ymfp, Px, [23]uint8{0x1c}},
	{API2FL, ymfp, Px, [23]uint8{0x0d}},
	{APFACC, ymfp, Px, [23]uint8{0xae}},
	{APFADD, ymfp, Px, [23]uint8{0x9e}},
	{APFCMPEQ, ymfp, Px, [23]uint8{0xb0}},
	{APFCMPGE, ymfp, Px, [23]uint8{0x90}},
	{APFCMPGT, ymfp, Px, [23]uint8{0xa0}},
	{APFMAX, ymfp, Px, [23]uint8{0xa4}},
	{APFMIN, ymfp, Px, [23]uint8{0x94}},
	{APFMUL, ymfp, Px, [23]uint8{0xb4}},
	{APFNACC, ymfp, Px, [23]uint8{0x8a}},
	{APFPNACC, ymfp, Px, [23]uint8{0x8e}},
	{APFRCP, ymfp, Px, [23]uint8{0x96}},
	{APFRCPIT1, ymfp, Px, [23]uint8{0xa6}},
	{APFRCPI2T, ymfp, Px, [23]uint8{0xb6}},
	{APFRSQIT1, ymfp, Px, [23]uint8{0xa7}},
	{APFRSQRT, ymfp, Px, [23]uint8{0x97}},
	{APFSUB, ymfp, Px, [23]uint8{0x9a}},
	{APFSUBR, ymfp, Px, [23]uint8{0xaa}},
	{APINSRW, yinsrw, Pq, [23]uint8{0xc4, 00}},
	{APINSRD, yinsr, Pq, [23]uint8{0x3a, 0x22, 00}},
	{APINSRQ, yinsr, Pq3, [23]uint8{0x3a, 0x22, 00}},
	{APMADDWL, ymm, Py1, [23]uint8{0xf5, Pe, 0xf5}},
	{APMAXSW, yxm, Pe, [23]uint8{0xee}},
	{APMAXUB, yxm, Pe, [23]uint8{0xde}},
	{APMINSW, yxm, Pe, [23]uint8{0xea}},
	{APMINUB, yxm, Pe, [23]uint8{0xda}},
	{APMOVMSKB, ymskb, Px, [23]uint8{Pe, 0xd7, 0xd7}},
	{APMULHRW, ymfp, Px, [23]uint8{0xb7}},
	{APMULHUW, ymm, Py1, [23]uint8{0xe4, Pe, 0xe4}},
	{APMULHW, ymm, Py1, [23]uint8{0xe5, Pe, 0xe5}},
	{APMULLW, ymm, Py1, [23]uint8{0xd5, Pe, 0xd5}},
	{APMULULQ, ymm, Py1, [23]uint8{0xf4, Pe, 0xf4}},
	{APOPAL, ynone, P32, [23]uint8{0x61}},
	{APOPAW, ynone, Pe, [23]uint8{0x61}},
	{APOPFL, ynone, P32, [23]uint8{0x9d}},
	{APOPFQ, ynone, Py, [23]uint8{0x9d}},
	{APOPFW, ynone, Pe, [23]uint8{0x9d}},
	{APOPL, ypopl, P32, [23]uint8{0x58, 0x8f, 00}},
	{APOPQ, ypopl, Py, [23]uint8{0x58, 0x8f, 00}},
	{APOPW, ypopl, Pe, [23]uint8{0x58, 0x8f, 00}},
	{APOR, ymm, Py1, [23]uint8{0xeb, Pe, 0xeb}},
	{APSADBW, yxm, Pq, [23]uint8{0xf6}},
	{APSHUFHW, yxshuf, Pf3, [23]uint8{0x70, 00}},
	{APSHUFL, yxshuf, Pq, [23]uint8{0x70, 00}},
	{APSHUFLW, yxshuf, Pf2, [23]uint8{0x70, 00}},
	{APSHUFW, ymshuf, Pm, [23]uint8{0x70, 00}},
	{APSHUFB, ymshufb, Pq, [23]uint8{0x38, 0x00}},
	{APSLLO, ypsdq, Pq, [23]uint8{0x73, 07}},
	{APSLLL, yps, Py3, [23]uint8{0xf2, 0x72, 06, Pe, 0xf2, Pe, 0x72, 06}},
	{APSLLQ, yps, Py3, [23]uint8{0xf3, 0x73, 06, Pe, 0xf3, Pe, 0x73, 06}},
	{APSLLW, yps, Py3, [23]uint8{0xf1, 0x71, 06, Pe, 0xf1, Pe, 0x71, 06}},
	{APSRAL, yps, Py3, [23]uint8{0xe2, 0x72, 04, Pe, 0xe2, Pe, 0x72, 04}},
	{APSRAW, yps, Py3, [23]uint8{0xe1, 0x71, 04, Pe, 0xe1, Pe, 0x71, 04}},
	{APSRLO, ypsdq, Pq, [23]uint8{0x73, 03}},
	{APSRLL, yps, Py3, [23]uint8{0xd2, 0x72, 02, Pe, 0xd2, Pe, 0x72, 02}},
	{APSRLQ, yps, Py3, [23]uint8{0xd3, 0x73, 02, Pe, 0xd3, Pe, 0x73, 02}},
	{APSRLW, yps, Py3, [23]uint8{0xd1, 0x71, 02, Pe, 0xe1, Pe, 0x71, 02}},
	{APSUBB, yxm, Pe, [23]uint8{0xf8}},
	{APSUBL, yxm, Pe, [23]uint8{0xfa}},
	{APSUBQ, yxm, Pe, [23]uint8{0xfb}},
	{APSUBSB, yxm, Pe, [23]uint8{0xe8}},
	{APSUBSW, yxm, Pe, [23]uint8{0xe9}},
	{APSUBUSB, yxm, Pe, [23]uint8{0xd8}},
	{APSUBUSW, yxm, Pe, [23]uint8{0xd9}},
	{APSUBW, yxm, Pe, [23]uint8{0xf9}},
	{APSWAPL, ymfp, Px, [23]uint8{0xbb}},
	{APUNPCKHBW, ymm, Py1, [23]uint8{0x68, Pe, 0x68}},
	{APUNPCKHLQ, ymm, Py1, [23]uint8{0x6a, Pe, 0x6a}},
	{APUNPCKHQDQ, yxm, Pe, [23]uint8{0x6d}},
	{APUNPCKHWL, ymm, Py1, [23]uint8{0x69, Pe, 0x69}},
	{APUNPCKLBW, ymm, Py1, [23]uint8{0x60, Pe, 0x60}},
	{APUNPCKLLQ, ymm, Py1, [23]uint8{0x62, Pe, 0x62}},
	{APUNPCKLQDQ, yxm, Pe, [23]uint8{0x6c}},
	{APUNPCKLWL, ymm, Py1, [23]uint8{0x61, Pe, 0x61}},
	{APUSHAL, ynone, P32, [23]uint8{0x60}},
	{APUSHAW, ynone, Pe, [23]uint8{0x60}},
	{APUSHFL, ynone, P32, [23]uint8{0x9c}},
	{APUSHFQ, ynone, Py, [23]uint8{0x9c}},
	{APUSHFW, ynone, Pe, [23]uint8{0x9c}},
	{APUSHL, ypushl, P32, [23]uint8{0x50, 0xff, 06, 0x6a, 0x68}},
	{APUSHQ, ypushl, Py, [23]uint8{0x50, 0xff, 06, 0x6a, 0x68}},
	{APUSHW, ypushl, Pe, [23]uint8{0x50, 0xff, 06, 0x6a, 0x68}},
	{APXOR, ymm, Py1, [23]uint8{0xef, Pe, 0xef}},
	{AQUAD, ybyte, Px, [23]uint8{8}},
	{ARCLB, yshb, Pb, [23]uint8{0xd0, 02, 0xc0, 02, 0xd2, 02}},
	{ARCLL, yshl, Px, [23]uint8{0xd1, 02, 0xc1, 02, 0xd3, 02, 0xd3, 02}},
	{ARCLQ, yshl, Pw, [23]uint8{0xd1, 02, 0xc1, 02, 0xd3, 02, 0xd3, 02}},
	{ARCLW, yshl, Pe, [23]uint8{0xd1, 02, 0xc1, 02, 0xd3, 02, 0xd3, 02}},
	{ARCPPS, yxm, Pm, [23]uint8{0x53}},
	{ARCPSS, yxm, Pf3, [23]uint8{0x53}},
	{ARCRB, yshb, Pb, [23]uint8{0xd0, 03, 0xc0, 03, 0xd2, 03}},
	{ARCRL, yshl, Px, [23]uint8{0xd1, 03, 0xc1, 03, 0xd3, 03, 0xd3, 03}},
	{ARCRQ, yshl, Pw, [23]uint8{0xd1, 03, 0xc1, 03, 0xd3, 03, 0xd3, 03}},
	{ARCRW, yshl, Pe, [23]uint8{0xd1, 03, 0xc1, 03, 0xd3, 03, 0xd3, 03}},
	{AREP, ynone, Px, [23]uint8{0xf3}},
	{AREPN, ynone, Px, [23]uint8{0xf2}},
	{obj.ARET, ynone, Px, [23]uint8{0xc3}},
	{ARETFW, yret, Pe, [23]uint8{0xcb, 0xca}},
	{ARETFL, yret, Px, [23]uint8{0xcb, 0xca}},
	{ARETFQ, yret, Pw, [23]uint8{0xcb, 0xca}},
	{AROLB, yshb, Pb, [23]uint8{0xd0, 00, 0xc0, 00, 0xd2, 00}},
	{AROLL, yshl, Px, [23]uint8{0xd1, 00, 0xc1, 00, 0xd3, 00, 0xd3, 00}},
	{AROLQ, yshl, Pw, [23]uint8{0xd1, 00, 0xc1, 00, 0xd3, 00, 0xd3, 00}},
	{AROLW, yshl, Pe, [23]uint8{0xd1, 00, 0xc1, 00, 0xd3, 00, 0xd3, 00}},
	{ARORB, yshb, Pb, [23]uint8{0xd0, 01, 0xc0, 01, 0xd2, 01}},
	{ARORL, yshl, Px, [23]uint8{0xd1, 01, 0xc1, 01, 0xd3, 01, 0xd3, 01}},
	{ARORQ, yshl, Pw, [23]uint8{0xd1, 01, 0xc1, 01, 0xd3, 01, 0xd3, 01}},
	{ARORW, yshl, Pe, [23]uint8{0xd1, 01, 0xc1, 01, 0xd3, 01, 0xd3, 01}},
	{ARSQRTPS, yxm, Pm, [23]uint8{0x52}},
	{ARSQRTSS, yxm, Pf3, [23]uint8{0x52}},
	{ASAHF, ynone, Px1, [23]uint8{0x9e, 00, 0x86, 0xe0, 0x50, 0x9d}}, /* XCHGB AH,AL; PUSH AX; POPFL */
	{ASALB, yshb, Pb, [23]uint8{0xd0, 04, 0xc0, 04, 0xd2, 04}},
	{ASALL, yshl, Px, [23]uint8{0xd1, 04, 0xc1, 04, 0xd3, 04, 0xd3, 04}},
	{ASALQ, yshl, Pw, [23]uint8{0xd1, 04, 0xc1, 04, 0xd3, 04, 0xd3, 04}},
	{ASALW, yshl, Pe, [23]uint8{0xd1, 04, 0xc1, 04, 0xd3, 04, 0xd3, 04}},
	{ASARB, yshb, Pb, [23]uint8{0xd0, 07, 0xc0, 07, 0xd2, 07}},
	{ASARL, yshl, Px, [23]uint8{0xd1, 07, 0xc1, 07, 0xd3, 07, 0xd3, 07}},
	{ASARQ, yshl, Pw, [23]uint8{0xd1, 07, 0xc1, 07, 0xd3, 07, 0xd3, 07}},
	{ASARW, yshl, Pe, [23]uint8{0xd1, 07, 0xc1, 07, 0xd3, 07, 0xd3, 07}},
	{ASBBB, yxorb, Pb, [23]uint8{0x1c, 0x80, 03, 0x18, 0x1a}},
	{ASBBL, yxorl, Px, [23]uint8{0x83, 03, 0x1d, 0x81, 03, 0x19, 0x1b}},
	{ASBBQ, yxorl, Pw, [23]uint8{0x83, 03, 0x1d, 0x81, 03, 0x19, 0x1b}},
	{ASBBW, yxorl, Pe, [23]uint8{0x83, 03, 0x1d, 0x81, 03, 0x19, 0x1b}},
	{ASCASB, ynone, Pb, [23]uint8{0xae}},
	{ASCASL, ynone, Px, [23]uint8{0xaf}},
	{ASCASQ, ynone, Pw, [23]uint8{0xaf}},
	{ASCASW, ynone, Pe, [23]uint8{0xaf}},
	{ASETCC, yscond, Pb, [23]uint8{0x0f, 0x93, 00}},
	{ASETCS, yscond, Pb, [23]uint8{0x0f, 0x92, 00}},
	{ASETEQ, yscond, Pb, [23]uint8{0x0f, 0x94, 00}},
	{ASETGE, yscond, Pb, [23]uint8{0x0f, 0x9d, 00}},
	{ASETGT, yscond, Pb, [23]uint8{0x0f, 0x9f, 00}},
	{ASETHI, yscond, Pb, [23]uint8{0x0f, 0x97, 00}},
	{ASETLE, yscond, Pb, [23]uint8{0x0f, 0x9e, 00}},
	{ASETLS, yscond, Pb, [23]uint8{0x0f, 0x96, 00}},
	{ASETLT, yscond, Pb, [23]uint8{0x0f, 0x9c, 00}},
	{ASETMI, yscond, Pb, [23]uint8{0x0f, 0x98, 00}},
	{ASETNE, yscond, Pb, [23]uint8{0x0f, 0x95, 00}},
	{ASETOC, yscond, Pb, [23]uint8{0x0f, 0x91, 00}},
	{ASETOS, yscond, Pb, [23]uint8{0x0f, 0x90, 00}},
	{ASETPC, yscond, Pb, [23]uint8{0x0f, 0x9b, 00}},
	{ASETPL, yscond, Pb, [23]uint8{0x0f, 0x99, 00}},
	{ASETPS, yscond, Pb, [23]uint8{0x0f, 0x9a, 00}},
	{ASHLB, yshb, Pb, [23]uint8{0xd0, 04, 0xc0, 04, 0xd2, 04}},
	{ASHLL, yshl, Px, [23]uint8{0xd1, 04, 0xc1, 04, 0xd3, 04, 0xd3, 04}},
	{ASHLQ, yshl, Pw, [23]uint8{0xd1, 04, 0xc1, 04, 0xd3, 04, 0xd3, 04}},
	{ASHLW, yshl, Pe, [23]uint8{0xd1, 04, 0xc1, 04, 0xd3, 04, 0xd3, 04}},
	{ASHRB, yshb, Pb, [23]uint8{0xd0, 05, 0xc0, 05, 0xd2, 05}},
	{ASHRL, yshl, Px, [23]uint8{0xd1, 05, 0xc1, 05, 0xd3, 05, 0xd3, 05}},
	{ASHRQ, yshl, Pw, [23]uint8{0xd1, 05, 0xc1, 05, 0xd3, 05, 0xd3, 05}},
	{ASHRW, yshl, Pe, [23]uint8{0xd1, 05, 0xc1, 05, 0xd3, 05, 0xd3, 05}},
	{ASHUFPD, yxshuf, Pq, [23]uint8{0xc6, 00}},
	{ASHUFPS, yxshuf, Pm, [23]uint8{0xc6, 00}},
	{ASQRTPD, yxm, Pe, [23]uint8{0x51}},
	{ASQRTPS, yxm, Pm, [23]uint8{0x51}},
	{ASQRTSD, yxm, Pf2, [23]uint8{0x51}},
	{ASQRTSS, yxm, Pf3, [23]uint8{0x51}},
	{ASTC, ynone, Px, [23]uint8{0xf9}},
	{ASTD, ynone, Px, [23]uint8{0xfd}},
	{ASTI, ynone, Px, [23]uint8{0xfb}},
	{ASTMXCSR, ysvrs, Pm, [23]uint8{0xae, 03, 0xae, 03}},
	{ASTOSB, ynone, Pb, [23]uint8{0xaa}},
	{ASTOSL, ynone, Px, [23]uint8{0xab}},
	{ASTOSQ, ynone, Pw, [23]uint8{0xab}},
	{ASTOSW, ynone, Pe, [23]uint8{0xab}},
	{ASUBB, yxorb, Pb, [23]uint8{0x2c, 0x80, 05, 0x28, 0x2a}},
	{ASUBL, yaddl, Px, [23]uint8{0x83, 05, 0x2d, 0x81, 05, 0x29, 0x2b}},
	{ASUBPD, yxm, Pe, [23]uint8{0x5c}},
	{ASUBPS, yxm, Pm, [23]uint8{0x5c}},
	{ASUBQ, yaddl, Pw, [23]uint8{0x83, 05, 0x2d, 0x81, 05, 0x29, 0x2b}},
	{ASUBSD, yxm, Pf2, [23]uint8{0x5c}},
	{ASUBSS, yxm, Pf3, [23]uint8{0x5c}},
	{ASUBW, yaddl, Pe, [23]uint8{0x83, 05, 0x2d, 0x81, 05, 0x29, 0x2b}},
	{ASWAPGS, ynone, Pm, [23]uint8{0x01, 0xf8}},
	{ASYSCALL, ynone, Px, [23]uint8{0x0f, 0x05}}, /* fast syscall */
	{ATESTB, ytestb, Pb, [23]uint8{0xa8, 0xf6, 00, 0x84, 0x84}},
	{ATESTL, ytestl, Px, [23]uint8{0xa9, 0xf7, 00, 0x85, 0x85}},
	{ATESTQ, ytestl, Pw, [23]uint8{0xa9, 0xf7, 00, 0x85, 0x85}},
	{ATESTW, ytestl, Pe, [23]uint8{0xa9, 0xf7, 00, 0x85, 0x85}},
	{obj.ATEXT, ytext, Px, [23]uint8{}},
	{AUCOMISD, yxcmp, Pe, [23]uint8{0x2e}},
	{AUCOMISS, yxcmp, Pm, [23]uint8{0x2e}},
	{AUNPCKHPD, yxm, Pe, [23]uint8{0x15}},
	{AUNPCKHPS, yxm, Pm, [23]uint8{0x15}},
	{AUNPCKLPD, yxm, Pe, [23]uint8{0x14}},
	{AUNPCKLPS, yxm, Pm, [23]uint8{0x14}},
	{AVERR, ydivl, Pm, [23]uint8{0x00, 04}},
	{AVERW, ydivl, Pm, [23]uint8{0x00, 05}},
	{AWAIT, ynone, Px, [23]uint8{0x9b}},
	{AWORD, ybyte, Px, [23]uint8{2}},
	{AXCHGB, yml_mb, Pb, [23]uint8{0x86, 0x86}},
	{AXCHGL, yxchg, Px, [23]uint8{0x90, 0x90, 0x87, 0x87}},
	{AXCHGQ, yxchg, Pw, [23]uint8{0x90, 0x90, 0x87, 0x87}},
	{AXCHGW, yxchg, Pe, [23]uint8{0x90, 0x90, 0x87, 0x87}},
	{AXLAT, ynone, Px, [23]uint8{0xd7}},
	{AXORB, yxorb, Pb, [23]uint8{0x34, 0x80, 06, 0x30, 0x32}},
	{AXORL, yxorl, Px, [23]uint8{0x83, 06, 0x35, 0x81, 06, 0x31, 0x33}},
	{AXORPD, yxm, Pe, [23]uint8{0x57}},
	{AXORPS, yxm, Pm, [23]uint8{0x57}},
	{AXORQ, yxorl, Pw, [23]uint8{0x83, 06, 0x35, 0x81, 06, 0x31, 0x33}},
	{AXORW, yxorl, Pe, [23]uint8{0x83, 06, 0x35, 0x81, 06, 0x31, 0x33}},
	{AFMOVB, yfmvx, Px, [23]uint8{0xdf, 04}},
	{AFMOVBP, yfmvp, Px, [23]uint8{0xdf, 06}},
	{AFMOVD, yfmvd, Px, [23]uint8{0xdd, 00, 0xdd, 02, 0xd9, 00, 0xdd, 02}},
	{AFMOVDP, yfmvdp, Px, [23]uint8{0xdd, 03, 0xdd, 03}},
	{AFMOVF, yfmvf, Px, [23]uint8{0xd9, 00, 0xd9, 02}},
	{AFMOVFP, yfmvp, Px, [23]uint8{0xd9, 03}},
	{AFMOVL, yfmvf, Px, [23]uint8{0xdb, 00, 0xdb, 02}},
	{AFMOVLP, yfmvp, Px, [23]uint8{0xdb, 03}},
	{AFMOVV, yfmvx, Px, [23]uint8{0xdf, 05}},
	{AFMOVVP, yfmvp, Px, [23]uint8{0xdf, 07}},
	{AFMOVW, yfmvf, Px, [23]uint8{0xdf, 00, 0xdf, 02}},
	{AFMOVWP, yfmvp, Px, [23]uint8{0xdf, 03}},
	{AFMOVX, yfmvx, Px, [23]uint8{0xdb, 05}},
	{AFMOVXP, yfmvp, Px, [23]uint8{0xdb, 07}},
	{AFCMOVCC, yfcmv, Px, [23]uint8{0xdb, 00}},
	{AFCMOVCS, yfcmv, Px, [23]uint8{0xda, 00}},
	{AFCMOVEQ, yfcmv, Px, [23]uint8{0xda, 01}},
	{AFCMOVHI, yfcmv, Px, [23]uint8{0xdb, 02}},
	{AFCMOVLS, yfcmv, Px, [23]uint8{0xda, 02}},
	{AFCMOVNE, yfcmv, Px, [23]uint8{0xdb, 01}},
	{AFCMOVNU, yfcmv, Px, [23]uint8{0xdb, 03}},
	{AFCMOVUN, yfcmv, Px, [23]uint8{0xda, 03}},
	{AFCOMB, nil, 0, [23]uint8{}},
	{AFCOMBP, nil, 0, [23]uint8{}},
	{AFCOMD, yfadd, Px, [23]uint8{0xdc, 02, 0xd8, 02, 0xdc, 02}},  /* botch */
	{AFCOMDP, yfadd, Px, [23]uint8{0xdc, 03, 0xd8, 03, 0xdc, 03}}, /* botch */
	{AFCOMDPP, ycompp, Px, [23]uint8{0xde, 03}},
	{AFCOMF, yfmvx, Px, [23]uint8{0xd8, 02}},
	{AFCOMFP, yfmvx, Px, [23]uint8{0xd8, 03}},
	{AFCOMI, yfmvx, Px, [23]uint8{0xdb, 06}},
	{AFCOMIP, yfmvx, Px, [23]uint8{0xdf, 06}},
	{AFCOML, yfmvx, Px, [23]uint8{0xda, 02}},
	{AFCOMLP, yfmvx, Px, [23]uint8{0xda, 03}},
	{AFCOMW, yfmvx, Px, [23]uint8{0xde, 02}},
	{AFCOMWP, yfmvx, Px, [23]uint8{0xde, 03}},
	{AFUCOM, ycompp, Px, [23]uint8{0xdd, 04}},
	{AFUCOMI, ycompp, Px, [23]uint8{0xdb, 05}},
	{AFUCOMIP, ycompp, Px, [23]uint8{0xdf, 05}},
	{AFUCOMP, ycompp, Px, [23]uint8{0xdd, 05}},
	{AFUCOMPP, ycompp, Px, [23]uint8{0xda, 13}},
	{AFADDDP, yfaddp, Px, [23]uint8{0xde, 00}},
	{AFADDW, yfmvx, Px, [23]uint8{0xde, 00}},
	{AFADDL, yfmvx, Px, [23]uint8{0xda, 00}},
	{AFADDF, yfmvx, Px, [23]uint8{0xd8, 00}},
	{AFADDD, yfadd, Px, [23]uint8{0xdc, 00, 0xd8, 00, 0xdc, 00}},
	{AFMULDP, yfaddp, Px, [23]uint8{0xde, 01}},
	{AFMULW, yfmvx, Px, [23]uint8{0xde, 01}},
	{AFMULL, yfmvx, Px, [23]uint8{0xda, 01}},
	{AFMULF, yfmvx, Px, [23]uint8{0xd8, 01}},
	{AFMULD, yfadd, Px, [23]uint8{0xdc, 01, 0xd8, 01, 0xdc, 01}},
	{AFSUBDP, yfaddp, Px, [23]uint8{0xde, 05}},
	{AFSUBW, yfmvx, Px, [23]uint8{0xde, 04}},
	{AFSUBL, yfmvx, Px, [23]uint8{0xda, 04}},
	{AFSUBF, yfmvx, Px, [23]uint8{0xd8, 04}},
	{AFSUBD, yfadd, Px, [23]uint8{0xdc, 04, 0xd8, 04, 0xdc, 05}},
	{AFSUBRDP, yfaddp, Px, [23]uint8{0xde, 04}},
	{AFSUBRW, yfmvx, Px, [23]uint8{0xde, 05}},
	{AFSUBRL, yfmvx, Px, [23]uint8{0xda, 05}},
	{AFSUBRF, yfmvx, Px, [23]uint8{0xd8, 05}},
	{AFSUBRD, yfadd, Px, [23]uint8{0xdc, 05, 0xd8, 05, 0xdc, 04}},
	{AFDIVDP, yfaddp, Px, [23]uint8{0xde, 07}},
	{AFDIVW, yfmvx, Px, [23]uint8{0xde, 06}},
	{AFDIVL, yfmvx, Px, [23]uint8{0xda, 06}},
	{AFDIVF, yfmvx, Px, [23]uint8{0xd8, 06}},
	{AFDIVD, yfadd, Px, [23]uint8{0xdc, 06, 0xd8, 06, 0xdc, 07}},
	{AFDIVRDP, yfaddp, Px, [23]uint8{0xde, 06}},
	{AFDIVRW, yfmvx, Px, [23]uint8{0xde, 07}},
	{AFDIVRL, yfmvx, Px, [23]uint8{0xda, 07}},
	{AFDIVRF, yfmvx, Px, [23]uint8{0xd8, 07}},
	{AFDIVRD, yfadd, Px, [23]uint8{0xdc, 07, 0xd8, 07, 0xdc, 06}},
	{AFXCHD, yfxch, Px, [23]uint8{0xd9, 01, 0xd9, 01}},
	{AFFREE, nil, 0, [23]uint8{}},
	{AFLDCW, ystcw, Px, [23]uint8{0xd9, 05, 0xd9, 05}},
	{AFLDENV, ystcw, Px, [23]uint8{0xd9, 04, 0xd9, 04}},
	{AFRSTOR, ysvrs, Px, [23]uint8{0xdd, 04, 0xdd, 04}},
	{AFSAVE, ysvrs, Px, [23]uint8{0xdd, 06, 0xdd, 06}},
	{AFSTCW, ystcw, Px, [23]uint8{0xd9, 07, 0xd9, 07}},
	{AFSTENV, ystcw, Px, [23]uint8{0xd9, 06, 0xd9, 06}},
	{AFSTSW, ystsw, Px, [23]uint8{0xdd, 07, 0xdf, 0xe0}},
	{AF2XM1, ynone, Px, [23]uint8{0xd9, 0xf0}},
	{AFABS, ynone, Px, [23]uint8{0xd9, 0xe1}},
	{AFCHS, ynone, Px, [23]uint8{0xd9, 0xe0}},
	{AFCLEX, ynone, Px, [23]uint8{0xdb, 0xe2}},
	{AFCOS, ynone, Px, [23]uint8{0xd9, 0xff}},
	{AFDECSTP, ynone, Px, [23]uint8{0xd9, 0xf6}},
	{AFINCSTP, ynone, Px, [23]uint8{0xd9, 0xf7}},
	{AFINIT, ynone, Px, [23]uint8{0xdb, 0xe3}},
	{AFLD1, ynone, Px, [23]uint8{0xd9, 0xe8}},
	{AFLDL2E, ynone, Px, [23]uint8{0xd9, 0xea}},
	{AFLDL2T, ynone, Px, [23]uint8{0xd9, 0xe9}},
	{AFLDLG2, ynone, Px, [23]uint8{0xd9, 0xec}},
	{AFLDLN2, ynone, Px, [23]uint8{0xd9, 0xed}},
	{AFLDPI, ynone, Px, [23]uint8{0xd9, 0xeb}},
	{AFLDZ, ynone, Px, [23]uint8{0xd9, 0xee}},
	{AFNOP, ynone, Px, [23]uint8{0xd9, 0xd0}},
	{AFPATAN, ynone, Px, [23]uint8{0xd9, 0xf3}},
	{AFPREM, ynone, Px, [23]uint8{0xd9, 0xf8}},
	{AFPREM1, ynone, Px, [23]uint8{0xd9, 0xf5}},
	{AFPTAN, ynone, Px, [23]uint8{0xd9, 0xf2}},
	{AFRNDINT, ynone, Px, [23]uint8{0xd9, 0xfc}},
	{AFSCALE, ynone, Px, [23]uint8{0xd9, 0xfd}},
	{AFSIN, ynone, Px, [23]uint8{0xd9, 0xfe}},
	{AFSINCOS, ynone, Px, [23]uint8{0xd9, 0xfb}},
	{AFSQRT, ynone, Px, [23]uint8{0xd9, 0xfa}},
	{AFTST, ynone, Px, [23]uint8{0xd9, 0xe4}},
	{AFXAM, ynone, Px, [23]uint8{0xd9, 0xe5}},
	{AFXTRACT, ynone, Px, [23]uint8{0xd9, 0xf4}},
	{AFYL2X, ynone, Px, [23]uint8{0xd9, 0xf1}},
	{AFYL2XP1, ynone, Px, [23]uint8{0xd9, 0xf9}},
	{ACMPXCHGB, yrb_mb, Pb, [23]uint8{0x0f, 0xb0}},
	{ACMPXCHGL, yrl_ml, Px, [23]uint8{0x0f, 0xb1}},
	{ACMPXCHGW, yrl_ml, Pe, [23]uint8{0x0f, 0xb1}},
	{ACMPXCHGQ, yrl_ml, Pw, [23]uint8{0x0f, 0xb1}},
	{ACMPXCHG8B, yscond, Pm, [23]uint8{0xc7, 01}},
	{AINVD, ynone, Pm, [23]uint8{0x08}},
	{AINVLPG, ymbs, Pm, [23]uint8{0x01, 07}},
	{ALFENCE, ynone, Pm, [23]uint8{0xae, 0xe8}},
	{AMFENCE, ynone, Pm, [23]uint8{0xae, 0xf0}},
	{AMOVNTIL, yrl_ml, Pm, [23]uint8{0xc3}},
	{AMOVNTIQ, yrl_ml, Pw, [23]uint8{0x0f, 0xc3}},
	{ARDMSR, ynone, Pm, [23]uint8{0x32}},
	{ARDPMC, ynone, Pm, [23]uint8{0x33}},
	{ARDTSC, ynone, Pm, [23]uint8{0x31}},
	{ARSM, ynone, Pm, [23]uint8{0xaa}},
	{ASFENCE, ynone, Pm, [23]uint8{0xae, 0xf8}},
	{ASYSRET, ynone, Pm, [23]uint8{0x07}},
	{AWBINVD, ynone, Pm, [23]uint8{0x09}},
	{AWRMSR, ynone, Pm, [23]uint8{0x30}},
	{AXADDB, yrb_mb, Pb, [23]uint8{0x0f, 0xc0}},
	{AXADDL, yrl_ml, Px, [23]uint8{0x0f, 0xc1}},
	{AXADDQ, yrl_ml, Pw, [23]uint8{0x0f, 0xc1}},
	{AXADDW, yrl_ml, Pe, [23]uint8{0x0f, 0xc1}},
	{ACRC32B, ycrc32l, Px, [23]uint8{0xf2, 0x0f, 0x38, 0xf0, 0}},
	{ACRC32Q, ycrc32l, Pw, [23]uint8{0xf2, 0x0f, 0x38, 0xf1, 0}},
	{APREFETCHT0, yprefetch, Pm, [23]uint8{0x18, 01}},
	{APREFETCHT1, yprefetch, Pm, [23]uint8{0x18, 02}},
	{APREFETCHT2, yprefetch, Pm, [23]uint8{0x18, 03}},
	{APREFETCHNTA, yprefetch, Pm, [23]uint8{0x18, 00}},
	{AMOVQL, yrl_ml, Px, [23]uint8{0x89}},
	{obj.AUNDEF, ynone, Px, [23]uint8{0x0f, 0x0b}},
	{AAESENC, yaes, Pq, [23]uint8{0x38, 0xdc, 0}},
	{AAESENCLAST, yaes, Pq, [23]uint8{0x38, 0xdd, 0}},
	{AAESDEC, yaes, Pq, [23]uint8{0x38, 0xde, 0}},
	{AAESDECLAST, yaes, Pq, [23]uint8{0x38, 0xdf, 0}},
	{AAESIMC, yaes, Pq, [23]uint8{0x38, 0xdb, 0}},
	{AAESKEYGENASSIST, yaes2, Pq, [23]uint8{0x3a, 0xdf, 0}},
1490 1491 1492 1493
	{AROUNDPD, yaes2, Pq, [23]uint8{0x3a, 0x09, 0}},
	{AROUNDPS, yaes2, Pq, [23]uint8{0x3a, 0x08, 0}},
	{AROUNDSD, yaes2, Pq, [23]uint8{0x3a, 0x0b, 0}},
	{AROUNDSS, yaes2, Pq, [23]uint8{0x3a, 0x0a, 0}},
1494 1495
	{APSHUFD, yxshuf, Pq, [23]uint8{0x70, 0}},
	{APCLMULQDQ, yxshuf, Pq, [23]uint8{0x3a, 0x44, 0}},
1496 1497 1498 1499
	{AVZEROUPPER, ynone, Px, [23]uint8{0xc5, 0xf8, 0x77}},
	{AMOVHDU, yxmov_vex, Pvex2, [23]uint8{0x6f, 0x7f}},
	{AMOVNTHD, yxr_ml_vex, Pvex1, [23]uint8{0xe7}},
	{AMOVHDA, yxmov_vex, Pvex1, [23]uint8{0x6f, 0x7f}},
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
	{obj.AUSEFIELD, ynop, Px, [23]uint8{0, 0}},
	{obj.ATYPE, nil, 0, [23]uint8{}},
	{obj.AFUNCDATA, yfuncdata, Px, [23]uint8{0, 0}},
	{obj.APCDATA, ypcdata, Px, [23]uint8{0, 0}},
	{obj.ACHECKNIL, nil, 0, [23]uint8{}},
	{obj.AVARDEF, nil, 0, [23]uint8{}},
	{obj.AVARKILL, nil, 0, [23]uint8{}},
	{obj.ADUFFCOPY, yduff, Px, [23]uint8{0xe8}},
	{obj.ADUFFZERO, yduff, Px, [23]uint8{0xe8}},
	{obj.AEND, nil, 0, [23]uint8{}},
	{0, nil, 0, [23]uint8{}},
1511 1512
}

1513
var opindex [(ALAST + 1) & obj.AMask]*Optab
1514 1515 1516 1517 1518 1519

// isextern reports whether s describes an external symbol that must avoid pc-relative addressing.
// This happens on systems like Solaris that call .so functions instead of system calls.
// It does not seem to be necessary for any other systems. This is probably working
// around a Solaris-specific bug that should be fixed differently, but we don't know
// what that bug is. And this does fix it.
1520
func isextern(s *obj.LSym) bool {
1521
	// All the Solaris dynamic imports from libc.so begin with "libc_".
1522
	return strings.HasPrefix(s.Name, "libc_")
1523 1524 1525 1526 1527 1528
}

// single-instruction no-ops of various lengths.
// constructed by hand and disassembled with gdb to verify.
// see http://www.agner.org/optimize/optimizing_assembly.pdf for discussion.
var nop = [][16]uint8{
1529 1530 1531 1532 1533 1534 1535 1536 1537
	{0x90},
	{0x66, 0x90},
	{0x0F, 0x1F, 0x00},
	{0x0F, 0x1F, 0x40, 0x00},
	{0x0F, 0x1F, 0x44, 0x00, 0x00},
	{0x66, 0x0F, 0x1F, 0x44, 0x00, 0x00},
	{0x0F, 0x1F, 0x80, 0x00, 0x00, 0x00, 0x00},
	{0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
	{0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
}

// Native Client rejects the repeated 0x66 prefix.
// {0x66, 0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
func fillnop(p []byte, n int) {
	var m int

	for n > 0 {
		m = n
		if m > len(nop) {
			m = len(nop)
		}
		copy(p[:m], nop[m-1][:m])
		p = p[m:]
		n -= m
	}
}

func naclpad(ctxt *obj.Link, s *obj.LSym, c int32, pad int32) int32 {
	obj.Symgrow(ctxt, s, int64(c)+int64(pad))
	fillnop(s.P[c:], int(pad))
	return c + pad
}

func spadjop(ctxt *obj.Link, p *obj.Prog, l int, q int) int {
	if p.Mode != 64 || ctxt.Arch.Ptrsize == 4 {
		return l
	}
	return q
}

func span6(ctxt *obj.Link, s *obj.LSym) {
	ctxt.Cursym = s

	if s.P != nil {
		return
	}

	if ycover[0] == 0 {
		instinit()
	}

Russ Cox's avatar
Russ Cox committed
1580 1581
	var v int32
	for p := ctxt.Cursym.Text; p != nil; p = p.Link {
1582
		if p.To.Type == obj.TYPE_BRANCH {
1583 1584 1585 1586 1587
			if p.Pcond == nil {
				p.Pcond = p
			}
		}
		if p.As == AADJSP {
1588 1589
			p.To.Type = obj.TYPE_REG
			p.To.Reg = REG_SP
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
			v = int32(-p.From.Offset)
			p.From.Offset = int64(v)
			p.As = int16(spadjop(ctxt, p, AADDL, AADDQ))
			if v < 0 {
				p.As = int16(spadjop(ctxt, p, ASUBL, ASUBQ))
				v = -v
				p.From.Offset = int64(v)
			}

			if v == 0 {
1600
				p.As = obj.ANOP
1601 1602 1603 1604
			}
		}
	}

Russ Cox's avatar
Russ Cox committed
1605 1606
	var q *obj.Prog
	for p := s.Text; p != nil; p = p.Link {
1607 1608 1609 1610 1611 1612 1613 1614
		p.Back = 2 // use short branches first time through
		q = p.Pcond
		if q != nil && (q.Back&2 != 0) {
			p.Back |= 1 // backward jump
			q.Back |= 4 // loop head
		}

		if p.As == AADJSP {
1615 1616
			p.To.Type = obj.TYPE_REG
			p.To.Reg = REG_SP
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
			v = int32(-p.From.Offset)
			p.From.Offset = int64(v)
			p.As = int16(spadjop(ctxt, p, AADDL, AADDQ))
			if v < 0 {
				p.As = int16(spadjop(ctxt, p, ASUBL, ASUBQ))
				v = -v
				p.From.Offset = int64(v)
			}

			if v == 0 {
1627
				p.As = obj.ANOP
1628 1629 1630 1631
			}
		}
	}

Russ Cox's avatar
Russ Cox committed
1632 1633 1634 1635 1636 1637 1638
	n := 0
	var bp []byte
	var c int32
	var i int
	var loop int32
	var m int
	var p *obj.Prog
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
	for {
		loop = 0
		for i = 0; i < len(s.R); i++ {
			s.R[i] = obj.Reloc{}
		}
		s.R = s.R[:0]
		s.P = s.P[:0]
		c = 0
		for p = s.Text; p != nil; p = p.Link {
			if ctxt.Headtype == obj.Hnacl && p.Isize > 0 {
				var deferreturn *obj.LSym

				if deferreturn == nil {
					deferreturn = obj.Linklookup(ctxt, "runtime.deferreturn", 0)
				}

				// pad everything to avoid crossing 32-byte boundary
				if c>>5 != (c+int32(p.Isize)-1)>>5 {
					c = naclpad(ctxt, s, c, -c&31)
				}

				// pad call deferreturn to start at 32-byte boundary
				// so that subtracting 5 in jmpdefer will jump back
				// to that boundary and rerun the call.
1663
				if p.As == obj.ACALL && p.To.Sym == deferreturn {
1664 1665 1666 1667
					c = naclpad(ctxt, s, c, -c&31)
				}

				// pad call to end at 32-byte boundary
1668
				if p.As == obj.ACALL {
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
					c = naclpad(ctxt, s, c, -(c+int32(p.Isize))&31)
				}

				// the linker treats REP and STOSQ as different instructions
				// but in fact the REP is a prefix on the STOSQ.
				// make sure REP has room for 2 more bytes, so that
				// padding will not be inserted before the next instruction.
				if (p.As == AREP || p.As == AREPN) && c>>5 != (c+3-1)>>5 {
					c = naclpad(ctxt, s, c, -c&31)
				}

				// same for LOCK.
				// various instructions follow; the longest is 4 bytes.
				// give ourselves 8 bytes so as to avoid surprises.
				if p.As == ALOCK && c>>5 != (c+8-1)>>5 {
					c = naclpad(ctxt, s, c, -c&31)
				}
			}

			if (p.Back&4 != 0) && c&(LoopAlign-1) != 0 {
				// pad with NOPs
				v = -c & (LoopAlign - 1)

				if v <= MaxLoopPad {
					obj.Symgrow(ctxt, s, int64(c)+int64(v))
					fillnop(s.P[c:], int(v))
					c += v
				}
			}

			p.Pc = int64(c)

			// process forward jumps to p
1702
			for q = p.Rel; q != nil; q = q.Forwd {
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
				v = int32(p.Pc - (q.Pc + int64(q.Mark)))
				if q.Back&2 != 0 { // short
					if v > 127 {
						loop++
						q.Back ^= 2
					}

					if q.As == AJCXZL {
						s.P[q.Pc+2] = byte(v)
					} else {
						s.P[q.Pc+1] = byte(v)
					}
				} else {
					bp = s.P[q.Pc+int64(q.Mark)-4:]
					bp[0] = byte(v)
					bp = bp[1:]
					bp[0] = byte(v >> 8)
					bp = bp[1:]
					bp[0] = byte(v >> 16)
					bp = bp[1:]
					bp[0] = byte(v >> 24)
				}
			}

1727
			p.Rel = nil
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747

			p.Pc = int64(c)
			asmins(ctxt, p)
			m = -cap(ctxt.Andptr) + cap(ctxt.And[:])
			if int(p.Isize) != m {
				p.Isize = uint8(m)
				loop++
			}

			obj.Symgrow(ctxt, s, p.Pc+int64(m))
			copy(s.P[p.Pc:][:m], ctxt.And[:m])
			p.Mark = uint16(m)
			c += int32(m)
		}

		n++
		if n > 20 {
			ctxt.Diag("span must be looping")
			log.Fatalf("loop")
		}
1748
		if loop == 0 {
1749 1750 1751 1752 1753 1754 1755 1756
			break
		}
	}

	if ctxt.Headtype == obj.Hnacl {
		c = naclpad(ctxt, s, c, -c&31)
	}

1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
	// Pad functions with trap instruction, to catch invalid jumps
	if c&(FuncAlign-1) != 0 {
		v = -c & (FuncAlign - 1)
		obj.Symgrow(ctxt, s, int64(c)+int64(v))
		for i := c; i < c+v; i++ {
			// 0xCC is INT $3 - breakpoint instruction
			s.P[i] = uint8(0xCC)
		}
		c += v
	}
1767 1768 1769 1770
	s.Size = int64(c)

	if false { /* debug['a'] > 1 */
		fmt.Printf("span1 %s %d (%d tries)\n %.6x", s.Name, s.Size, n, 0)
Russ Cox's avatar
Russ Cox committed
1771
		var i int
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
		for i = 0; i < len(s.P); i++ {
			fmt.Printf(" %.2x", s.P[i])
			if i%16 == 15 {
				fmt.Printf("\n  %.6x", uint(i+1))
			}
		}

		if i%16 != 0 {
			fmt.Printf("\n")
		}

Russ Cox's avatar
Russ Cox committed
1783 1784
		for i := 0; i < len(s.R); i++ {
			r := &s.R[i]
1785 1786 1787 1788 1789 1790 1791 1792
			fmt.Printf(" rel %#.4x/%d %s%+d\n", uint32(r.Off), r.Siz, r.Sym.Name, r.Add)
		}
	}
}

func instinit() {
	var c int

Russ Cox's avatar
Russ Cox committed
1793
	for i := 1; optab[i].as != 0; i++ {
1794
		c = int(optab[i].as)
1795 1796
		if opindex[c&obj.AMask] != nil {
			log.Fatalf("phase error in optab: %d (%v)", i, obj.Aconv(c))
1797
		}
1798
		opindex[c&obj.AMask] = &optab[i]
1799 1800
	}

Russ Cox's avatar
Russ Cox committed
1801
	for i := 0; i < Ymax; i++ {
1802 1803 1804 1805 1806
		ycover[i*Ymax+i] = 1
	}

	ycover[Yi0*Ymax+Yi8] = 1
	ycover[Yi1*Ymax+Yi8] = 1
1807 1808 1809 1810 1811 1812 1813 1814
	ycover[Yu7*Ymax+Yi8] = 1

	ycover[Yi0*Ymax+Yu7] = 1
	ycover[Yi1*Ymax+Yu7] = 1

	ycover[Yi0*Ymax+Yu8] = 1
	ycover[Yi1*Ymax+Yu8] = 1
	ycover[Yu7*Ymax+Yu8] = 1
1815 1816 1817

	ycover[Yi0*Ymax+Ys32] = 1
	ycover[Yi1*Ymax+Ys32] = 1
1818 1819
	ycover[Yu7*Ymax+Ys32] = 1
	ycover[Yu8*Ymax+Ys32] = 1
1820 1821 1822 1823
	ycover[Yi8*Ymax+Ys32] = 1

	ycover[Yi0*Ymax+Yi32] = 1
	ycover[Yi1*Ymax+Yi32] = 1
1824 1825
	ycover[Yu7*Ymax+Yi32] = 1
	ycover[Yu8*Ymax+Yi32] = 1
1826 1827 1828 1829 1830
	ycover[Yi8*Ymax+Yi32] = 1
	ycover[Ys32*Ymax+Yi32] = 1

	ycover[Yi0*Ymax+Yi64] = 1
	ycover[Yi1*Ymax+Yi64] = 1
1831 1832
	ycover[Yu7*Ymax+Yi64] = 1
	ycover[Yu8*Ymax+Yi64] = 1
1833 1834 1835 1836 1837 1838 1839 1840 1841
	ycover[Yi8*Ymax+Yi64] = 1
	ycover[Ys32*Ymax+Yi64] = 1
	ycover[Yi32*Ymax+Yi64] = 1

	ycover[Yal*Ymax+Yrb] = 1
	ycover[Ycl*Ymax+Yrb] = 1
	ycover[Yax*Ymax+Yrb] = 1
	ycover[Ycx*Ymax+Yrb] = 1
	ycover[Yrx*Ymax+Yrb] = 1
1842
	ycover[Yrl*Ymax+Yrb] = 1 // but not Yrl32
1843 1844 1845 1846 1847 1848 1849 1850 1851

	ycover[Ycl*Ymax+Ycx] = 1

	ycover[Yax*Ymax+Yrx] = 1
	ycover[Ycx*Ymax+Yrx] = 1

	ycover[Yax*Ymax+Yrl] = 1
	ycover[Ycx*Ymax+Yrl] = 1
	ycover[Yrx*Ymax+Yrl] = 1
1852
	ycover[Yrl32*Ymax+Yrl] = 1
1853 1854 1855 1856 1857 1858 1859 1860 1861

	ycover[Yf0*Ymax+Yrf] = 1

	ycover[Yal*Ymax+Ymb] = 1
	ycover[Ycl*Ymax+Ymb] = 1
	ycover[Yax*Ymax+Ymb] = 1
	ycover[Ycx*Ymax+Ymb] = 1
	ycover[Yrx*Ymax+Ymb] = 1
	ycover[Yrb*Ymax+Ymb] = 1
1862
	ycover[Yrl*Ymax+Ymb] = 1 // but not Yrl32
1863 1864 1865 1866 1867 1868
	ycover[Ym*Ymax+Ymb] = 1

	ycover[Yax*Ymax+Yml] = 1
	ycover[Ycx*Ymax+Yml] = 1
	ycover[Yrx*Ymax+Yml] = 1
	ycover[Yrl*Ymax+Yml] = 1
1869
	ycover[Yrl32*Ymax+Yml] = 1
1870 1871 1872 1873 1874 1875
	ycover[Ym*Ymax+Yml] = 1

	ycover[Yax*Ymax+Ymm] = 1
	ycover[Ycx*Ymax+Ymm] = 1
	ycover[Yrx*Ymax+Ymm] = 1
	ycover[Yrl*Ymax+Ymm] = 1
1876
	ycover[Yrl32*Ymax+Ymm] = 1
1877 1878 1879 1880 1881 1882
	ycover[Ym*Ymax+Ymm] = 1
	ycover[Ymr*Ymax+Ymm] = 1

	ycover[Ym*Ymax+Yxm] = 1
	ycover[Yxr*Ymax+Yxm] = 1

Russ Cox's avatar
Russ Cox committed
1883
	for i := 0; i < MAXREG; i++ {
1884
		reg[i] = -1
1885 1886 1887
		if i >= REG_AL && i <= REG_R15B {
			reg[i] = (i - REG_AL) & 7
			if i >= REG_SPB && i <= REG_DIB {
1888 1889
				regrex[i] = 0x40
			}
1890
			if i >= REG_R8B && i <= REG_R15B {
1891 1892 1893 1894
				regrex[i] = Rxr | Rxx | Rxb
			}
		}

1895 1896
		if i >= REG_AH && i <= REG_BH {
			reg[i] = 4 + ((i - REG_AH) & 7)
1897
		}
1898 1899 1900
		if i >= REG_AX && i <= REG_R15 {
			reg[i] = (i - REG_AX) & 7
			if i >= REG_R8 {
1901 1902 1903 1904
				regrex[i] = Rxr | Rxx | Rxb
			}
		}

1905 1906
		if i >= REG_F0 && i <= REG_F0+7 {
			reg[i] = (i - REG_F0) & 7
1907
		}
1908 1909
		if i >= REG_M0 && i <= REG_M0+7 {
			reg[i] = (i - REG_M0) & 7
1910
		}
1911 1912 1913
		if i >= REG_X0 && i <= REG_X0+15 {
			reg[i] = (i - REG_X0) & 7
			if i >= REG_X0+8 {
1914 1915 1916 1917
				regrex[i] = Rxr | Rxx | Rxb
			}
		}

1918
		if i >= REG_CR+8 && i <= REG_CR+15 {
1919 1920 1921 1922 1923
			regrex[i] = Rxr
		}
	}
}

1924
func prefixof(ctxt *obj.Link, p *obj.Prog, a *obj.Addr) int {
1925 1926 1927
	if a.Reg < REG_CS && a.Index < REG_CS { // fast path
		return 0
	}
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
	if a.Type == obj.TYPE_MEM && a.Name == obj.NAME_NONE {
		switch a.Reg {
		case REG_CS:
			return 0x2e

		case REG_DS:
			return 0x3e

		case REG_ES:
			return 0x26

		case REG_FS:
			return 0x64

		case REG_GS:
			return 0x65

		case REG_TLS:
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
			// NOTE: Systems listed here should be only systems that
			// support direct TLS references like 8(TLS) implemented as
			// direct references from FS or GS. Systems that require
			// the initial-exec model, where you load the TLS base into
			// a register and then index from that register, do not reach
			// this code and should not be listed.
			if p.Mode == 32 {
				switch ctxt.Headtype {
				default:
					log.Fatalf("unknown TLS base register for %s", obj.Headstr(ctxt.Headtype))

				case obj.Hdarwin,
					obj.Hdragonfly,
					obj.Hfreebsd,
					obj.Hnetbsd,
					obj.Hopenbsd:
					return 0x65 // GS
				}
			}

1966 1967 1968 1969
			switch ctxt.Headtype {
			default:
				log.Fatalf("unknown TLS base register for %s", obj.Headstr(ctxt.Headtype))

1970 1971 1972 1973 1974 1975 1976
			case obj.Hlinux:
				if ctxt.Flag_shared != 0 {
					log.Fatalf("unknown TLS base register for linux with -shared")
				} else {
					return 0x64 // FS
				}

1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
			case obj.Hdragonfly,
				obj.Hfreebsd,
				obj.Hnetbsd,
				obj.Hopenbsd,
				obj.Hsolaris:
				return 0x64 // FS

			case obj.Hdarwin:
				return 0x65 // GS
			}
1987 1988 1989
		}
	}

1990 1991 1992 1993
	if p.Mode == 32 {
		return 0
	}

1994
	switch a.Index {
1995
	case REG_CS:
1996 1997
		return 0x2e

1998
	case REG_DS:
1999 2000
		return 0x3e

2001
	case REG_ES:
2002 2003
		return 0x26

2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
	case REG_TLS:
		if ctxt.Flag_shared != 0 {
			// When building for inclusion into a shared library, an instruction of the form
			//     MOV 0(CX)(TLS*1), AX
			// becomes
			//     mov %fs:(%rcx), %rax
			// which assumes that the correct TLS offset has been loaded into %rcx (today
			// there is only one TLS variable -- g -- so this is OK). When not building for
			// a shared library the instruction does not require a prefix.
			if a.Offset != 0 {
				log.Fatalf("cannot handle non-0 offsets to TLS")
			}
			return 0x64
		}

2019
	case REG_FS:
2020 2021
		return 0x64

2022
	case REG_GS:
2023 2024 2025 2026 2027 2028
		return 0x65
	}

	return 0
}

2029 2030 2031 2032 2033 2034 2035
func oclass(ctxt *obj.Link, p *obj.Prog, a *obj.Addr) int {
	switch a.Type {
	case obj.TYPE_NONE:
		return Ynone

	case obj.TYPE_BRANCH:
		return Ybr
2036

2037 2038 2039 2040 2041 2042
	case obj.TYPE_INDIR:
		if a.Name != obj.NAME_NONE && a.Reg == REG_NONE && a.Index == REG_NONE && a.Scale == 0 {
			return Yindir
		}
		return Yxxx

2043 2044 2045 2046 2047 2048
	case obj.TYPE_MEM:
		return Ym

	case obj.TYPE_ADDR:
		switch a.Name {
		case obj.NAME_EXTERN,
2049
			obj.NAME_GOTREF,
2050
			obj.NAME_STATIC:
2051
			if a.Sym != nil && isextern(a.Sym) || p.Mode == 32 {
2052
				return Yi32
2053
			}
2054
			return Yiauto // use pc-relative addressing
2055

2056 2057 2058
		case obj.NAME_AUTO,
			obj.NAME_PARAM:
			return Yiauto
2059 2060
		}

2061 2062 2063 2064 2065 2066 2067 2068
		// TODO(rsc): DUFFZERO/DUFFCOPY encoding forgot to set a->index
		// and got Yi32 in an earlier version of this code.
		// Keep doing that until we fix yduff etc.
		if a.Sym != nil && strings.HasPrefix(a.Sym.Name, "runtime.duff") {
			return Yi32
		}

		if a.Sym != nil || a.Name != obj.NAME_NONE {
2069
			ctxt.Diag("unexpected addr: %v", obj.Dconv(p, a))
2070 2071 2072 2073 2074 2075 2076
		}
		fallthrough

		// fall through

	case obj.TYPE_CONST:
		if a.Sym != nil {
2077
			ctxt.Diag("TYPE_CONST with symbol: %v", obj.Dconv(p, a))
2078 2079
		}

Russ Cox's avatar
Russ Cox committed
2080
		v := a.Offset
2081 2082 2083
		if p.Mode == 32 {
			v = int64(int32(v))
		}
2084 2085 2086 2087 2088 2089
		if v == 0 {
			return Yi0
		}
		if v == 1 {
			return Yi1
		}
2090 2091 2092 2093 2094 2095
		if v >= 0 && v <= 127 {
			return Yu7
		}
		if v >= 0 && v <= 255 {
			return Yu8
		}
2096 2097 2098
		if v >= -128 && v <= 127 {
			return Yi8
		}
2099 2100 2101
		if p.Mode == 32 {
			return Yi32
		}
Russ Cox's avatar
Russ Cox committed
2102
		l := int32(v)
2103 2104 2105 2106 2107 2108 2109 2110 2111 2112
		if int64(l) == v {
			return Ys32 /* can sign extend */
		}
		if v>>32 == 0 {
			return Yi32 /* unsigned */
		}
		return Yi64

	case obj.TYPE_TEXTSIZE:
		return Ytextsize
2113 2114
	}

2115
	if a.Type != obj.TYPE_REG {
2116
		ctxt.Diag("unexpected addr1: type=%d %v", a.Type, obj.Dconv(p, a))
2117 2118 2119 2120 2121
		return Yxxx
	}

	switch a.Reg {
	case REG_AL:
2122 2123
		return Yal

2124
	case REG_AX:
2125 2126 2127
		return Yax

		/*
2128
			case REG_SPB:
2129
		*/
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
	case REG_BPB,
		REG_SIB,
		REG_DIB,
		REG_R8B,
		REG_R9B,
		REG_R10B,
		REG_R11B,
		REG_R12B,
		REG_R13B,
		REG_R14B,
		REG_R15B:
2141 2142 2143 2144 2145
		if ctxt.Asmode != 64 {
			return Yxxx
		}
		fallthrough

2146 2147 2148 2149 2150 2151
	case REG_DL,
		REG_BL,
		REG_AH,
		REG_CH,
		REG_DH,
		REG_BH:
2152 2153
		return Yrb

2154
	case REG_CL:
2155 2156
		return Ycl

2157
	case REG_CX:
2158 2159
		return Ycx

2160
	case REG_DX, REG_BX:
2161 2162
		return Yrx

2163 2164 2165 2166 2167 2168 2169 2170
	case REG_R8, /* not really Yrl */
		REG_R9,
		REG_R10,
		REG_R11,
		REG_R12,
		REG_R13,
		REG_R14,
		REG_R15:
2171 2172 2173 2174 2175
		if ctxt.Asmode != 64 {
			return Yxxx
		}
		fallthrough

2176
	case REG_SP, REG_BP, REG_SI, REG_DI:
2177 2178 2179
		if p.Mode == 32 {
			return Yrl32
		}
2180 2181
		return Yrl

2182
	case REG_F0 + 0:
2183 2184
		return Yf0

2185 2186 2187 2188 2189 2190 2191
	case REG_F0 + 1,
		REG_F0 + 2,
		REG_F0 + 3,
		REG_F0 + 4,
		REG_F0 + 5,
		REG_F0 + 6,
		REG_F0 + 7:
2192 2193
		return Yrf

2194 2195 2196 2197 2198 2199 2200 2201
	case REG_M0 + 0,
		REG_M0 + 1,
		REG_M0 + 2,
		REG_M0 + 3,
		REG_M0 + 4,
		REG_M0 + 5,
		REG_M0 + 6,
		REG_M0 + 7:
2202 2203
		return Ymr

2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219
	case REG_X0 + 0,
		REG_X0 + 1,
		REG_X0 + 2,
		REG_X0 + 3,
		REG_X0 + 4,
		REG_X0 + 5,
		REG_X0 + 6,
		REG_X0 + 7,
		REG_X0 + 8,
		REG_X0 + 9,
		REG_X0 + 10,
		REG_X0 + 11,
		REG_X0 + 12,
		REG_X0 + 13,
		REG_X0 + 14,
		REG_X0 + 15:
2220 2221
		return Yxr

2222
	case REG_CS:
2223
		return Ycs
2224
	case REG_SS:
2225
		return Yss
2226
	case REG_DS:
2227
		return Yds
2228
	case REG_ES:
2229
		return Yes
2230
	case REG_FS:
2231
		return Yfs
2232
	case REG_GS:
2233
		return Ygs
2234
	case REG_TLS:
2235 2236
		return Ytls

2237
	case REG_GDTR:
2238
		return Ygdtr
2239
	case REG_IDTR:
2240
		return Yidtr
2241
	case REG_LDTR:
2242
		return Yldtr
2243
	case REG_MSW:
2244
		return Ymsw
2245
	case REG_TASK:
2246 2247
		return Ytask

2248
	case REG_CR + 0:
2249
		return Ycr0
2250
	case REG_CR + 1:
2251
		return Ycr1
2252
	case REG_CR + 2:
2253
		return Ycr2
2254
	case REG_CR + 3:
2255
		return Ycr3
2256
	case REG_CR + 4:
2257
		return Ycr4
2258
	case REG_CR + 5:
2259
		return Ycr5
2260
	case REG_CR + 6:
2261
		return Ycr6
2262
	case REG_CR + 7:
2263
		return Ycr7
2264
	case REG_CR + 8:
2265 2266
		return Ycr8

2267
	case REG_DR + 0:
2268
		return Ydr0
2269
	case REG_DR + 1:
2270
		return Ydr1
2271
	case REG_DR + 2:
2272
		return Ydr2
2273
	case REG_DR + 3:
2274
		return Ydr3
2275
	case REG_DR + 4:
2276
		return Ydr4
2277
	case REG_DR + 5:
2278
		return Ydr5
2279
	case REG_DR + 6:
2280
		return Ydr6
2281
	case REG_DR + 7:
2282 2283
		return Ydr7

2284
	case REG_TR + 0:
2285
		return Ytr0
2286
	case REG_TR + 1:
2287
		return Ytr1
2288
	case REG_TR + 2:
2289
		return Ytr2
2290
	case REG_TR + 3:
2291
		return Ytr3
2292
	case REG_TR + 4:
2293
		return Ytr4
2294
	case REG_TR + 5:
2295
		return Ytr5
2296
	case REG_TR + 6:
2297
		return Ytr6
2298
	case REG_TR + 7:
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
		return Ytr7
	}

	return Yxxx
}

func asmidx(ctxt *obj.Link, scale int, index int, base int) {
	var i int

	switch index {
	default:
		goto bad

2312
	case REG_NONE:
2313 2314 2315
		i = 4 << 3
		goto bas

2316 2317 2318 2319 2320 2321 2322 2323
	case REG_R8,
		REG_R9,
		REG_R10,
		REG_R11,
		REG_R12,
		REG_R13,
		REG_R14,
		REG_R15:
2324 2325 2326 2327 2328
		if ctxt.Asmode != 64 {
			goto bad
		}
		fallthrough

2329 2330 2331 2332 2333 2334 2335
	case REG_AX,
		REG_CX,
		REG_DX,
		REG_BX,
		REG_BP,
		REG_SI,
		REG_DI:
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360
		i = reg[index] << 3
	}

	switch scale {
	default:
		goto bad

	case 1:
		break

	case 2:
		i |= 1 << 6

	case 4:
		i |= 2 << 6

	case 8:
		i |= 3 << 6
	}

bas:
	switch base {
	default:
		goto bad

2361
	case REG_NONE: /* must be mod=00 */
2362 2363
		i |= 5

2364 2365 2366 2367 2368 2369 2370 2371
	case REG_R8,
		REG_R9,
		REG_R10,
		REG_R11,
		REG_R12,
		REG_R13,
		REG_R14,
		REG_R15:
2372 2373 2374 2375 2376
		if ctxt.Asmode != 64 {
			goto bad
		}
		fallthrough

2377 2378 2379 2380 2381 2382 2383 2384
	case REG_AX,
		REG_CX,
		REG_DX,
		REG_BX,
		REG_SP,
		REG_BP,
		REG_SI,
		REG_DI:
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409
		i |= reg[base]
	}

	ctxt.Andptr[0] = byte(i)
	ctxt.Andptr = ctxt.Andptr[1:]
	return

bad:
	ctxt.Diag("asmidx: bad address %d/%d/%d", scale, index, base)
	ctxt.Andptr[0] = 0
	ctxt.Andptr = ctxt.Andptr[1:]
	return
}

func put4(ctxt *obj.Link, v int32) {
	ctxt.Andptr[0] = byte(v)
	ctxt.Andptr[1] = byte(v >> 8)
	ctxt.Andptr[2] = byte(v >> 16)
	ctxt.Andptr[3] = byte(v >> 24)
	ctxt.Andptr = ctxt.Andptr[4:]
}

func relput4(ctxt *obj.Link, p *obj.Prog, a *obj.Addr) {
	var rel obj.Reloc

Russ Cox's avatar
Russ Cox committed
2410
	v := vaddr(ctxt, p, a, &rel)
2411 2412 2413 2414
	if rel.Siz != 0 {
		if rel.Siz != 4 {
			ctxt.Diag("bad reloc")
		}
Russ Cox's avatar
Russ Cox committed
2415
		r := obj.Addrel(ctxt.Cursym)
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
		*r = rel
		r.Off = int32(p.Pc + int64(-cap(ctxt.Andptr)+cap(ctxt.And[:])))
	}

	put4(ctxt, int32(v))
}

func put8(ctxt *obj.Link, v int64) {
	ctxt.Andptr[0] = byte(v)
	ctxt.Andptr[1] = byte(v >> 8)
	ctxt.Andptr[2] = byte(v >> 16)
	ctxt.Andptr[3] = byte(v >> 24)
	ctxt.Andptr[4] = byte(v >> 32)
	ctxt.Andptr[5] = byte(v >> 40)
	ctxt.Andptr[6] = byte(v >> 48)
	ctxt.Andptr[7] = byte(v >> 56)
	ctxt.Andptr = ctxt.Andptr[8:]
}

/*
static void
relput8(Prog *p, Addr *a)
{
	vlong v;
	Reloc rel, *r;

	v = vaddr(ctxt, p, a, &rel);
	if(rel.siz != 0) {
		r = addrel(ctxt->cursym);
		*r = rel;
		r->siz = 8;
		r->off = p->pc + ctxt->andptr - ctxt->and;
	}
	put8(ctxt, v);
}
*/
func vaddr(ctxt *obj.Link, p *obj.Prog, a *obj.Addr, r *obj.Reloc) int64 {
	if r != nil {
		*r = obj.Reloc{}
	}

2457 2458
	switch a.Name {
	case obj.NAME_STATIC,
2459
		obj.NAME_GOTREF,
2460
		obj.NAME_EXTERN:
Russ Cox's avatar
Russ Cox committed
2461
		s := a.Sym
2462
		if r == nil {
2463
			ctxt.Diag("need reloc for %v", obj.Dconv(p, a))
2464 2465 2466
			log.Fatalf("reloc")
		}

2467 2468 2469 2470
		if a.Name == obj.NAME_GOTREF {
			r.Siz = 4
			r.Type = obj.R_GOTPCREL
		} else if isextern(s) || p.Mode != 64 {
2471
			r.Siz = 4
2472
			r.Type = obj.R_ADDR
2473 2474
		} else {
			r.Siz = 4
2475
			r.Type = obj.R_PCREL
2476 2477 2478 2479
		}

		r.Off = -1 // caller must fill in
		r.Sym = s
2480
		r.Add = a.Offset
2481

2482 2483 2484 2485
		return 0
	}

	if (a.Type == obj.TYPE_MEM || a.Type == obj.TYPE_ADDR) && a.Reg == REG_TLS {
2486
		if r == nil {
2487
			ctxt.Diag("need reloc for %v", obj.Dconv(p, a))
2488 2489 2490
			log.Fatalf("reloc")
		}

2491
		r.Type = obj.R_TLS_LE
2492 2493
		r.Siz = 4
		r.Off = -1 // caller must fill in
2494 2495
		r.Add = a.Offset
		return 0
2496 2497
	}

2498
	return a.Offset
2499 2500 2501
}

func asmandsz(ctxt *obj.Link, p *obj.Prog, a *obj.Addr, r int, rex int, m64 int) {
2502
	var base int
2503 2504 2505
	var rel obj.Reloc

	rex &= 0x40 | Rxr
Russ Cox's avatar
Russ Cox committed
2506
	v := int32(a.Offset)
2507 2508
	rel.Siz = 0

2509 2510 2511 2512 2513 2514 2515 2516 2517
	switch a.Type {
	case obj.TYPE_ADDR:
		if a.Name == obj.NAME_NONE {
			ctxt.Diag("unexpected TYPE_ADDR with NAME_NONE")
		}
		if a.Index == REG_TLS {
			ctxt.Diag("unexpected TYPE_ADDR with index==REG_TLS")
		}
		goto bad
2518

2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536
	case obj.TYPE_REG:
		if a.Reg < REG_AL || REG_X0+15 < a.Reg {
			goto bad
		}
		if v != 0 {
			goto bad
		}
		ctxt.Andptr[0] = byte(3<<6 | reg[a.Reg]<<0 | r<<3)
		ctxt.Andptr = ctxt.Andptr[1:]
		ctxt.Rexflag |= regrex[a.Reg]&(0x40|Rxb) | rex
		return
	}

	if a.Type != obj.TYPE_MEM {
		goto bad
	}

	if a.Index != REG_NONE && a.Index != REG_TLS {
Russ Cox's avatar
Russ Cox committed
2537
		base := int(a.Reg)
2538 2539
		switch a.Name {
		case obj.NAME_EXTERN,
2540
			obj.NAME_GOTREF,
2541
			obj.NAME_STATIC:
2542
			if !isextern(a.Sym) && p.Mode == 64 {
2543
				goto bad
2544
			}
2545 2546
			base = REG_NONE
			v = int32(vaddr(ctxt, p, a, &rel))
2547

2548 2549 2550
		case obj.NAME_AUTO,
			obj.NAME_PARAM:
			base = REG_SP
2551
		}
2552 2553 2554

		ctxt.Rexflag |= regrex[int(a.Index)]&Rxx | regrex[base]&Rxb | rex
		if base == REG_NONE {
2555 2556
			ctxt.Andptr[0] = byte(0<<6 | 4<<0 | r<<3)
			ctxt.Andptr = ctxt.Andptr[1:]
2557
			asmidx(ctxt, int(a.Scale), int(a.Index), base)
2558 2559 2560
			goto putrelv
		}

2561
		if v == 0 && rel.Siz == 0 && base != REG_BP && base != REG_R13 {
2562 2563
			ctxt.Andptr[0] = byte(0<<6 | 4<<0 | r<<3)
			ctxt.Andptr = ctxt.Andptr[1:]
2564
			asmidx(ctxt, int(a.Scale), int(a.Index), base)
2565 2566 2567 2568 2569 2570
			return
		}

		if v >= -128 && v < 128 && rel.Siz == 0 {
			ctxt.Andptr[0] = byte(1<<6 | 4<<0 | r<<3)
			ctxt.Andptr = ctxt.Andptr[1:]
2571
			asmidx(ctxt, int(a.Scale), int(a.Index), base)
2572 2573 2574 2575 2576 2577 2578
			ctxt.Andptr[0] = byte(v)
			ctxt.Andptr = ctxt.Andptr[1:]
			return
		}

		ctxt.Andptr[0] = byte(2<<6 | 4<<0 | r<<3)
		ctxt.Andptr = ctxt.Andptr[1:]
2579
		asmidx(ctxt, int(a.Scale), int(a.Index), base)
2580 2581 2582
		goto putrelv
	}

2583 2584 2585
	base = int(a.Reg)
	switch a.Name {
	case obj.NAME_STATIC,
2586
		obj.NAME_GOTREF,
2587 2588 2589
		obj.NAME_EXTERN:
		if a.Sym == nil {
			ctxt.Diag("bad addr: %v", p)
2590
		}
2591 2592
		base = REG_NONE
		v = int32(vaddr(ctxt, p, a, &rel))
2593

2594 2595 2596
	case obj.NAME_AUTO,
		obj.NAME_PARAM:
		base = REG_SP
2597
	}
2598 2599

	if base == REG_TLS {
2600 2601 2602
		v = int32(vaddr(ctxt, p, a, &rel))
	}

2603 2604
	ctxt.Rexflag |= regrex[base]&Rxb | rex
	if base == REG_NONE || (REG_CS <= base && base <= REG_GS) || base == REG_TLS {
2605 2606 2607 2608
		if (a.Sym == nil || !isextern(a.Sym)) && base == REG_NONE && (a.Name == obj.NAME_STATIC || a.Name == obj.NAME_EXTERN || a.Name == obj.NAME_GOTREF) || p.Mode != 64 {
			if a.Name == obj.NAME_GOTREF && (a.Offset != 0 || a.Index != 0 || a.Scale != 0) {
				ctxt.Diag("%v has offset against gotref", p)
			}
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
			ctxt.Andptr[0] = byte(0<<6 | 5<<0 | r<<3)
			ctxt.Andptr = ctxt.Andptr[1:]
			goto putrelv
		}

		/* temporary */
		ctxt.Andptr[0] = byte(0<<6 | 4<<0 | r<<3)
		ctxt.Andptr = ctxt.Andptr[1:] /* sib present */
		ctxt.Andptr[0] = 0<<6 | 4<<3 | 5<<0
		ctxt.Andptr = ctxt.Andptr[1:] /* DS:d32 */
		goto putrelv
	}

2622
	if base == REG_SP || base == REG_R12 {
2623
		if v == 0 {
2624
			ctxt.Andptr[0] = byte(0<<6 | reg[base]<<0 | r<<3)
2625
			ctxt.Andptr = ctxt.Andptr[1:]
2626
			asmidx(ctxt, int(a.Scale), REG_NONE, base)
2627 2628 2629 2630
			return
		}

		if v >= -128 && v < 128 {
2631
			ctxt.Andptr[0] = byte(1<<6 | reg[base]<<0 | r<<3)
2632
			ctxt.Andptr = ctxt.Andptr[1:]
2633
			asmidx(ctxt, int(a.Scale), REG_NONE, base)
2634 2635 2636 2637 2638
			ctxt.Andptr[0] = byte(v)
			ctxt.Andptr = ctxt.Andptr[1:]
			return
		}

2639
		ctxt.Andptr[0] = byte(2<<6 | reg[base]<<0 | r<<3)
2640
		ctxt.Andptr = ctxt.Andptr[1:]
2641
		asmidx(ctxt, int(a.Scale), REG_NONE, base)
2642 2643 2644
		goto putrelv
	}

2645
	if REG_AX <= base && base <= REG_R15 {
2646
		if a.Index == REG_TLS && ctxt.Flag_shared == 0 {
2647
			rel = obj.Reloc{}
2648
			rel.Type = obj.R_TLS_LE
2649 2650 2651 2652 2653 2654
			rel.Siz = 4
			rel.Sym = nil
			rel.Add = int64(v)
			v = 0
		}

2655 2656
		if v == 0 && rel.Siz == 0 && base != REG_BP && base != REG_R13 {
			ctxt.Andptr[0] = byte(0<<6 | reg[base]<<0 | r<<3)
2657 2658 2659 2660 2661
			ctxt.Andptr = ctxt.Andptr[1:]
			return
		}

		if v >= -128 && v < 128 && rel.Siz == 0 {
2662
			ctxt.Andptr[0] = byte(1<<6 | reg[base]<<0 | r<<3)
2663 2664 2665 2666 2667
			ctxt.Andptr[1] = byte(v)
			ctxt.Andptr = ctxt.Andptr[2:]
			return
		}

2668
		ctxt.Andptr[0] = byte(2<<6 | reg[base]<<0 | r<<3)
2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
		ctxt.Andptr = ctxt.Andptr[1:]
		goto putrelv
	}

	goto bad

putrelv:
	if rel.Siz != 0 {
		if rel.Siz != 4 {
			ctxt.Diag("bad rel")
			goto bad
		}

Russ Cox's avatar
Russ Cox committed
2682
		r := obj.Addrel(ctxt.Cursym)
2683 2684 2685 2686 2687 2688 2689 2690
		*r = rel
		r.Off = int32(ctxt.Curp.Pc + int64(-cap(ctxt.Andptr)+cap(ctxt.And[:])))
	}

	put4(ctxt, v)
	return

bad:
2691
	ctxt.Diag("asmand: bad address %v", obj.Dconv(p, a))
2692 2693 2694 2695
	return
}

func asmand(ctxt *obj.Link, p *obj.Prog, a *obj.Addr, ra *obj.Addr) {
2696
	asmandsz(ctxt, p, a, reg[ra.Reg], regrex[ra.Reg], 0)
2697 2698 2699 2700 2701 2702 2703
}

func asmando(ctxt *obj.Link, p *obj.Prog, a *obj.Addr, o int) {
	asmandsz(ctxt, p, a, o, 0, 0)
}

func bytereg(a *obj.Addr, t *uint8) {
2704 2705
	if a.Type == obj.TYPE_REG && a.Index == REG_NONE && (REG_AX <= a.Reg && a.Reg <= REG_R15) {
		a.Reg += REG_AL - REG_AX
2706 2707 2708 2709
		*t = 0
	}
}

2710 2711 2712 2713 2714 2715 2716
func unbytereg(a *obj.Addr, t *uint8) {
	if a.Type == obj.TYPE_REG && a.Index == REG_NONE && (REG_AL <= a.Reg && a.Reg <= REG_R15B) {
		a.Reg += REG_AX - REG_AL
		*t = 0
	}
}

2717 2718 2719 2720 2721 2722
const (
	E = 0xff
)

var ymovtab = []Movtab{
	/* push */
2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
	{APUSHL, Ycs, Ynone, Ynone, 0, [4]uint8{0x0e, E, 0, 0}},
	{APUSHL, Yss, Ynone, Ynone, 0, [4]uint8{0x16, E, 0, 0}},
	{APUSHL, Yds, Ynone, Ynone, 0, [4]uint8{0x1e, E, 0, 0}},
	{APUSHL, Yes, Ynone, Ynone, 0, [4]uint8{0x06, E, 0, 0}},
	{APUSHL, Yfs, Ynone, Ynone, 0, [4]uint8{0x0f, 0xa0, E, 0}},
	{APUSHL, Ygs, Ynone, Ynone, 0, [4]uint8{0x0f, 0xa8, E, 0}},
	{APUSHQ, Yfs, Ynone, Ynone, 0, [4]uint8{0x0f, 0xa0, E, 0}},
	{APUSHQ, Ygs, Ynone, Ynone, 0, [4]uint8{0x0f, 0xa8, E, 0}},
	{APUSHW, Ycs, Ynone, Ynone, 0, [4]uint8{Pe, 0x0e, E, 0}},
	{APUSHW, Yss, Ynone, Ynone, 0, [4]uint8{Pe, 0x16, E, 0}},
	{APUSHW, Yds, Ynone, Ynone, 0, [4]uint8{Pe, 0x1e, E, 0}},
	{APUSHW, Yes, Ynone, Ynone, 0, [4]uint8{Pe, 0x06, E, 0}},
	{APUSHW, Yfs, Ynone, Ynone, 0, [4]uint8{Pe, 0x0f, 0xa0, E}},
	{APUSHW, Ygs, Ynone, Ynone, 0, [4]uint8{Pe, 0x0f, 0xa8, E}},
2737 2738

	/* pop */
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750
	{APOPL, Ynone, Ynone, Yds, 0, [4]uint8{0x1f, E, 0, 0}},
	{APOPL, Ynone, Ynone, Yes, 0, [4]uint8{0x07, E, 0, 0}},
	{APOPL, Ynone, Ynone, Yss, 0, [4]uint8{0x17, E, 0, 0}},
	{APOPL, Ynone, Ynone, Yfs, 0, [4]uint8{0x0f, 0xa1, E, 0}},
	{APOPL, Ynone, Ynone, Ygs, 0, [4]uint8{0x0f, 0xa9, E, 0}},
	{APOPQ, Ynone, Ynone, Yfs, 0, [4]uint8{0x0f, 0xa1, E, 0}},
	{APOPQ, Ynone, Ynone, Ygs, 0, [4]uint8{0x0f, 0xa9, E, 0}},
	{APOPW, Ynone, Ynone, Yds, 0, [4]uint8{Pe, 0x1f, E, 0}},
	{APOPW, Ynone, Ynone, Yes, 0, [4]uint8{Pe, 0x07, E, 0}},
	{APOPW, Ynone, Ynone, Yss, 0, [4]uint8{Pe, 0x17, E, 0}},
	{APOPW, Ynone, Ynone, Yfs, 0, [4]uint8{Pe, 0x0f, 0xa1, E}},
	{APOPW, Ynone, Ynone, Ygs, 0, [4]uint8{Pe, 0x0f, 0xa9, E}},
2751 2752

	/* mov seg */
2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764
	{AMOVW, Yes, Ynone, Yml, 1, [4]uint8{0x8c, 0, 0, 0}},
	{AMOVW, Ycs, Ynone, Yml, 1, [4]uint8{0x8c, 1, 0, 0}},
	{AMOVW, Yss, Ynone, Yml, 1, [4]uint8{0x8c, 2, 0, 0}},
	{AMOVW, Yds, Ynone, Yml, 1, [4]uint8{0x8c, 3, 0, 0}},
	{AMOVW, Yfs, Ynone, Yml, 1, [4]uint8{0x8c, 4, 0, 0}},
	{AMOVW, Ygs, Ynone, Yml, 1, [4]uint8{0x8c, 5, 0, 0}},
	{AMOVW, Yml, Ynone, Yes, 2, [4]uint8{0x8e, 0, 0, 0}},
	{AMOVW, Yml, Ynone, Ycs, 2, [4]uint8{0x8e, 1, 0, 0}},
	{AMOVW, Yml, Ynone, Yss, 2, [4]uint8{0x8e, 2, 0, 0}},
	{AMOVW, Yml, Ynone, Yds, 2, [4]uint8{0x8e, 3, 0, 0}},
	{AMOVW, Yml, Ynone, Yfs, 2, [4]uint8{0x8e, 4, 0, 0}},
	{AMOVW, Yml, Ynone, Ygs, 2, [4]uint8{0x8e, 5, 0, 0}},
2765 2766

	/* mov cr */
2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786
	{AMOVL, Ycr0, Ynone, Yml, 3, [4]uint8{0x0f, 0x20, 0, 0}},
	{AMOVL, Ycr2, Ynone, Yml, 3, [4]uint8{0x0f, 0x20, 2, 0}},
	{AMOVL, Ycr3, Ynone, Yml, 3, [4]uint8{0x0f, 0x20, 3, 0}},
	{AMOVL, Ycr4, Ynone, Yml, 3, [4]uint8{0x0f, 0x20, 4, 0}},
	{AMOVL, Ycr8, Ynone, Yml, 3, [4]uint8{0x0f, 0x20, 8, 0}},
	{AMOVQ, Ycr0, Ynone, Yml, 3, [4]uint8{0x0f, 0x20, 0, 0}},
	{AMOVQ, Ycr2, Ynone, Yml, 3, [4]uint8{0x0f, 0x20, 2, 0}},
	{AMOVQ, Ycr3, Ynone, Yml, 3, [4]uint8{0x0f, 0x20, 3, 0}},
	{AMOVQ, Ycr4, Ynone, Yml, 3, [4]uint8{0x0f, 0x20, 4, 0}},
	{AMOVQ, Ycr8, Ynone, Yml, 3, [4]uint8{0x0f, 0x20, 8, 0}},
	{AMOVL, Yml, Ynone, Ycr0, 4, [4]uint8{0x0f, 0x22, 0, 0}},
	{AMOVL, Yml, Ynone, Ycr2, 4, [4]uint8{0x0f, 0x22, 2, 0}},
	{AMOVL, Yml, Ynone, Ycr3, 4, [4]uint8{0x0f, 0x22, 3, 0}},
	{AMOVL, Yml, Ynone, Ycr4, 4, [4]uint8{0x0f, 0x22, 4, 0}},
	{AMOVL, Yml, Ynone, Ycr8, 4, [4]uint8{0x0f, 0x22, 8, 0}},
	{AMOVQ, Yml, Ynone, Ycr0, 4, [4]uint8{0x0f, 0x22, 0, 0}},
	{AMOVQ, Yml, Ynone, Ycr2, 4, [4]uint8{0x0f, 0x22, 2, 0}},
	{AMOVQ, Yml, Ynone, Ycr3, 4, [4]uint8{0x0f, 0x22, 3, 0}},
	{AMOVQ, Yml, Ynone, Ycr4, 4, [4]uint8{0x0f, 0x22, 4, 0}},
	{AMOVQ, Yml, Ynone, Ycr8, 4, [4]uint8{0x0f, 0x22, 8, 0}},
2787 2788

	/* mov dr */
2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800
	{AMOVL, Ydr0, Ynone, Yml, 3, [4]uint8{0x0f, 0x21, 0, 0}},
	{AMOVL, Ydr6, Ynone, Yml, 3, [4]uint8{0x0f, 0x21, 6, 0}},
	{AMOVL, Ydr7, Ynone, Yml, 3, [4]uint8{0x0f, 0x21, 7, 0}},
	{AMOVQ, Ydr0, Ynone, Yml, 3, [4]uint8{0x0f, 0x21, 0, 0}},
	{AMOVQ, Ydr6, Ynone, Yml, 3, [4]uint8{0x0f, 0x21, 6, 0}},
	{AMOVQ, Ydr7, Ynone, Yml, 3, [4]uint8{0x0f, 0x21, 7, 0}},
	{AMOVL, Yml, Ynone, Ydr0, 4, [4]uint8{0x0f, 0x23, 0, 0}},
	{AMOVL, Yml, Ynone, Ydr6, 4, [4]uint8{0x0f, 0x23, 6, 0}},
	{AMOVL, Yml, Ynone, Ydr7, 4, [4]uint8{0x0f, 0x23, 7, 0}},
	{AMOVQ, Yml, Ynone, Ydr0, 4, [4]uint8{0x0f, 0x23, 0, 0}},
	{AMOVQ, Yml, Ynone, Ydr6, 4, [4]uint8{0x0f, 0x23, 6, 0}},
	{AMOVQ, Yml, Ynone, Ydr7, 4, [4]uint8{0x0f, 0x23, 7, 0}},
2801 2802

	/* mov tr */
2803 2804 2805 2806
	{AMOVL, Ytr6, Ynone, Yml, 3, [4]uint8{0x0f, 0x24, 6, 0}},
	{AMOVL, Ytr7, Ynone, Yml, 3, [4]uint8{0x0f, 0x24, 7, 0}},
	{AMOVL, Yml, Ynone, Ytr6, 4, [4]uint8{0x0f, 0x26, 6, E}},
	{AMOVL, Yml, Ynone, Ytr7, 4, [4]uint8{0x0f, 0x26, 7, E}},
2807 2808

	/* lgdt, sgdt, lidt, sidt */
2809 2810 2811 2812 2813 2814 2815 2816
	{AMOVL, Ym, Ynone, Ygdtr, 4, [4]uint8{0x0f, 0x01, 2, 0}},
	{AMOVL, Ygdtr, Ynone, Ym, 3, [4]uint8{0x0f, 0x01, 0, 0}},
	{AMOVL, Ym, Ynone, Yidtr, 4, [4]uint8{0x0f, 0x01, 3, 0}},
	{AMOVL, Yidtr, Ynone, Ym, 3, [4]uint8{0x0f, 0x01, 1, 0}},
	{AMOVQ, Ym, Ynone, Ygdtr, 4, [4]uint8{0x0f, 0x01, 2, 0}},
	{AMOVQ, Ygdtr, Ynone, Ym, 3, [4]uint8{0x0f, 0x01, 0, 0}},
	{AMOVQ, Ym, Ynone, Yidtr, 4, [4]uint8{0x0f, 0x01, 3, 0}},
	{AMOVQ, Yidtr, Ynone, Ym, 3, [4]uint8{0x0f, 0x01, 1, 0}},
2817 2818

	/* lldt, sldt */
2819 2820
	{AMOVW, Yml, Ynone, Yldtr, 4, [4]uint8{0x0f, 0x00, 2, 0}},
	{AMOVW, Yldtr, Ynone, Yml, 3, [4]uint8{0x0f, 0x00, 0, 0}},
2821 2822

	/* lmsw, smsw */
2823 2824
	{AMOVW, Yml, Ynone, Ymsw, 4, [4]uint8{0x0f, 0x01, 6, 0}},
	{AMOVW, Ymsw, Ynone, Yml, 3, [4]uint8{0x0f, 0x01, 4, 0}},
2825 2826

	/* ltr, str */
2827 2828
	{AMOVW, Yml, Ynone, Ytask, 4, [4]uint8{0x0f, 0x00, 3, 0}},
	{AMOVW, Ytask, Ynone, Yml, 3, [4]uint8{0x0f, 0x00, 1, 0}},
2829

2830
	/* load full pointer - unsupported
2831 2832
	Movtab{AMOVL, Yml, Ycol, 5, [4]uint8{0, 0, 0, 0}},
	Movtab{AMOVW, Yml, Ycol, 5, [4]uint8{Pe, 0, 0, 0}},
2833
	*/
2834 2835

	/* double shift */
2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853
	{ASHLL, Yi8, Yrl, Yml, 6, [4]uint8{0xa4, 0xa5, 0, 0}},
	{ASHLL, Ycl, Yrl, Yml, 6, [4]uint8{0xa4, 0xa5, 0, 0}},
	{ASHLL, Ycx, Yrl, Yml, 6, [4]uint8{0xa4, 0xa5, 0, 0}},
	{ASHRL, Yi8, Yrl, Yml, 6, [4]uint8{0xac, 0xad, 0, 0}},
	{ASHRL, Ycl, Yrl, Yml, 6, [4]uint8{0xac, 0xad, 0, 0}},
	{ASHRL, Ycx, Yrl, Yml, 6, [4]uint8{0xac, 0xad, 0, 0}},
	{ASHLQ, Yi8, Yrl, Yml, 6, [4]uint8{Pw, 0xa4, 0xa5, 0}},
	{ASHLQ, Ycl, Yrl, Yml, 6, [4]uint8{Pw, 0xa4, 0xa5, 0}},
	{ASHLQ, Ycx, Yrl, Yml, 6, [4]uint8{Pw, 0xa4, 0xa5, 0}},
	{ASHRQ, Yi8, Yrl, Yml, 6, [4]uint8{Pw, 0xac, 0xad, 0}},
	{ASHRQ, Ycl, Yrl, Yml, 6, [4]uint8{Pw, 0xac, 0xad, 0}},
	{ASHRQ, Ycx, Yrl, Yml, 6, [4]uint8{Pw, 0xac, 0xad, 0}},
	{ASHLW, Yi8, Yrl, Yml, 6, [4]uint8{Pe, 0xa4, 0xa5, 0}},
	{ASHLW, Ycl, Yrl, Yml, 6, [4]uint8{Pe, 0xa4, 0xa5, 0}},
	{ASHLW, Ycx, Yrl, Yml, 6, [4]uint8{Pe, 0xa4, 0xa5, 0}},
	{ASHRW, Yi8, Yrl, Yml, 6, [4]uint8{Pe, 0xac, 0xad, 0}},
	{ASHRW, Ycl, Yrl, Yml, 6, [4]uint8{Pe, 0xac, 0xad, 0}},
	{ASHRW, Ycx, Yrl, Yml, 6, [4]uint8{Pe, 0xac, 0xad, 0}},
2854 2855

	/* load TLS base */
2856 2857 2858
	{AMOVL, Ytls, Ynone, Yrl, 7, [4]uint8{0, 0, 0, 0}},
	{AMOVQ, Ytls, Ynone, Yrl, 7, [4]uint8{0, 0, 0, 0}},
	{0, 0, 0, 0, 0, [4]uint8{}},
2859 2860
}

2861
func isax(a *obj.Addr) bool {
2862
	switch a.Reg {
2863
	case REG_AX, REG_AL, REG_AH:
2864
		return true
2865 2866
	}

2867
	if a.Index == REG_AX {
2868
		return true
2869
	}
2870
	return false
2871 2872 2873
}

func subreg(p *obj.Prog, from int, to int) {
2874
	if false { /* debug['Q'] */
2875 2876 2877
		fmt.Printf("\n%v\ts/%v/%v/\n", p, Rconv(from), Rconv(to))
	}

2878 2879 2880
	if int(p.From.Reg) == from {
		p.From.Reg = int16(to)
		p.Ft = 0
2881
	}
2882 2883 2884 2885

	if int(p.To.Reg) == from {
		p.To.Reg = int16(to)
		p.Tt = 0
2886 2887 2888
	}

	if int(p.From.Index) == from {
2889 2890
		p.From.Index = int16(to)
		p.Ft = 0
2891 2892
	}

2893 2894 2895
	if int(p.To.Index) == from {
		p.To.Index = int16(to)
		p.Tt = 0
2896 2897
	}

2898
	if false { /* debug['Q'] */
2899 2900 2901 2902 2903 2904
		fmt.Printf("%v\n", p)
	}
}

func mediaop(ctxt *obj.Link, o *Optab, op int, osize int, z int) int {
	switch op {
2905
	case Pm, Pe, Pf2, Pf3:
2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930
		if osize != 1 {
			if op != Pm {
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
			}
			ctxt.Andptr[0] = Pm
			ctxt.Andptr = ctxt.Andptr[1:]
			z++
			op = int(o.op[z])
			break
		}
		fallthrough

	default:
		if -cap(ctxt.Andptr) == -cap(ctxt.And) || ctxt.And[-cap(ctxt.Andptr)+cap(ctxt.And[:])-1] != Pm {
			ctxt.Andptr[0] = Pm
			ctxt.Andptr = ctxt.Andptr[1:]
		}
	}

	ctxt.Andptr[0] = byte(op)
	ctxt.Andptr = ctxt.Andptr[1:]
	return z
}

2931 2932 2933 2934 2935 2936 2937 2938 2939
var bpduff1 = []byte{
	0x48, 0x89, 0x6c, 0x24, 0xf0, // MOVQ BP, -16(SP)
	0x48, 0x8d, 0x6c, 0x24, 0xf0, // LEAQ -16(SP), BP
}

var bpduff2 = []byte{
	0x48, 0x8b, 0x6d, 0x00, // MOVQ 0(BP), BP
}

2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983
func vexprefix(ctxt *obj.Link, to *obj.Addr, from *obj.Addr, pref uint8) {
	rexR := regrex[to.Reg]
	rexB := regrex[from.Reg]
	rexX := regrex[from.Index]
	var prefBit uint8
	if pref == Pvex1 {
		prefBit = 1
	} else if pref == Pvex2 {
		prefBit = 2
	} // TODO add Pvex0,Pvex3

	if rexX == 0 && rexB == 0 { // 2-byte vex prefix
		ctxt.Andptr[0] = 0xc5
		ctxt.Andptr = ctxt.Andptr[1:]

		if rexR != 0 {
			ctxt.Andptr[0] = 0x7c
		} else {
			ctxt.Andptr[0] = 0xfc
		}
		ctxt.Andptr[0] |= prefBit
		ctxt.Andptr = ctxt.Andptr[1:]
	} else {
		ctxt.Andptr[0] = 0xc4
		ctxt.Andptr = ctxt.Andptr[1:]

		ctxt.Andptr[0] = 0x1 // TODO handle different prefix
		if rexR == 0 {
			ctxt.Andptr[0] |= 0x80
		}
		if rexX == 0 {
			ctxt.Andptr[0] |= 0x40
		}
		if rexB == 0 {
			ctxt.Andptr[0] |= 0x20
		}
		ctxt.Andptr = ctxt.Andptr[1:]

		ctxt.Andptr[0] = 0x7c
		ctxt.Andptr[0] |= prefBit
		ctxt.Andptr = ctxt.Andptr[1:]
	}
}

2984 2985 2986
func doasm(ctxt *obj.Link, p *obj.Prog) {
	ctxt.Curp = p // TODO

2987
	o := opindex[p.As&obj.AMask]
2988 2989 2990 2991 2992 2993

	if o == nil {
		ctxt.Diag("asmins: missing op %v", p)
		return
	}

2994
	pre := prefixof(ctxt, p, &p.From)
2995 2996 2997 2998
	if pre != 0 {
		ctxt.Andptr[0] = byte(pre)
		ctxt.Andptr = ctxt.Andptr[1:]
	}
2999
	pre = prefixof(ctxt, p, &p.To)
3000 3001 3002 3003 3004
	if pre != 0 {
		ctxt.Andptr[0] = byte(pre)
		ctxt.Andptr = ctxt.Andptr[1:]
	}

3005 3006 3007 3008 3009
	// TODO(rsc): This special case is for SHRQ $3, AX:DX,
	// which encodes as SHRQ $32(DX*0), AX.
	// Similarly SHRQ CX, AX:DX is really SHRQ CX(DX*0), AX.
	// Change encoding generated by assemblers and compilers and remove.
	if (p.From.Type == obj.TYPE_CONST || p.From.Type == obj.TYPE_REG) && p.From.Index != REG_NONE && p.From.Scale == 0 {
3010
		p.From3 = new(obj.Addr)
3011 3012 3013 3014 3015 3016 3017 3018 3019
		p.From3.Type = obj.TYPE_REG
		p.From3.Reg = p.From.Index
		p.From.Index = 0
	}

	// TODO(rsc): This special case is for PINSRQ etc, CMPSD etc.
	// Change encoding generated by assemblers and compilers (if any) and remove.
	switch p.As {
	case AIMUL3Q, APEXTRW, APINSRW, APINSRD, APINSRQ, APSHUFHW, APSHUFL, APSHUFW, ASHUFPD, ASHUFPS, AAESKEYGENASSIST, APSHUFD, APCLMULQDQ:
3020 3021 3022
		if p.From3Type() == obj.TYPE_NONE {
			p.From3 = new(obj.Addr)
			*p.From3 = p.From
3023 3024 3025 3026 3027 3028
			p.From = obj.Addr{}
			p.From.Type = obj.TYPE_CONST
			p.From.Offset = p.To.Offset
			p.To.Offset = 0
		}
	case ACMPSD, ACMPSS, ACMPPS, ACMPPD:
3029 3030 3031
		if p.From3Type() == obj.TYPE_NONE {
			p.From3 = new(obj.Addr)
			*p.From3 = p.To
3032 3033 3034 3035 3036 3037 3038
			p.To = obj.Addr{}
			p.To.Type = obj.TYPE_CONST
			p.To.Offset = p.From3.Offset
			p.From3.Offset = 0
		}
	}

3039
	if p.Ft == 0 {
3040
		p.Ft = uint8(oclass(ctxt, p, &p.From))
3041 3042
	}
	if p.Tt == 0 {
3043
		p.Tt = uint8(oclass(ctxt, p, &p.To))
3044 3045
	}

Russ Cox's avatar
Russ Cox committed
3046
	ft := int(p.Ft) * Ymax
3047 3048 3049 3050
	f3t := Ynone * Ymax
	if p.From3 != nil {
		f3t = oclass(ctxt, p, p.From3) * Ymax
	}
Russ Cox's avatar
Russ Cox committed
3051
	tt := int(p.Tt) * Ymax
3052

3053
	xo := obj.Bool2int(o.op[0] == 0x0f)
Russ Cox's avatar
Russ Cox committed
3054 3055 3056 3057 3058 3059 3060 3061
	z := 0
	var a *obj.Addr
	var l int
	var op int
	var q *obj.Prog
	var r *obj.Reloc
	var rel obj.Reloc
	var v int64
3062 3063
	for i := range o.ytab {
		yt := &o.ytab[i]
3064
		if ycover[ft+int(yt.from)] != 0 && ycover[f3t+int(yt.from3)] != 0 && ycover[tt+int(yt.to)] != 0 {
Russ Cox's avatar
Russ Cox committed
3065
			switch o.prefix {
3066 3067 3068 3069 3070
			case Px1: /* first option valid only in 32-bit mode */
				if ctxt.Mode == 64 && z == 0 {
					z += int(yt.zoffset) + xo
					continue
				}
Russ Cox's avatar
Russ Cox committed
3071 3072 3073
			case Pq: /* 16 bit escape and opcode escape */
				ctxt.Andptr[0] = Pe
				ctxt.Andptr = ctxt.Andptr[1:]
3074

Russ Cox's avatar
Russ Cox committed
3075 3076
				ctxt.Andptr[0] = Pm
				ctxt.Andptr = ctxt.Andptr[1:]
3077

Russ Cox's avatar
Russ Cox committed
3078 3079 3080
			case Pq3: /* 16 bit escape, Rex.w, and opcode escape */
				ctxt.Andptr[0] = Pe
				ctxt.Andptr = ctxt.Andptr[1:]
3081

Russ Cox's avatar
Russ Cox committed
3082 3083 3084 3085
				ctxt.Andptr[0] = Pw
				ctxt.Andptr = ctxt.Andptr[1:]
				ctxt.Andptr[0] = Pm
				ctxt.Andptr = ctxt.Andptr[1:]
3086

Russ Cox's avatar
Russ Cox committed
3087 3088 3089 3090
			case Pf2, /* xmm opcode escape */
				Pf3:
				ctxt.Andptr[0] = byte(o.prefix)
				ctxt.Andptr = ctxt.Andptr[1:]
3091

Russ Cox's avatar
Russ Cox committed
3092 3093
				ctxt.Andptr[0] = Pm
				ctxt.Andptr = ctxt.Andptr[1:]
3094

Russ Cox's avatar
Russ Cox committed
3095 3096 3097
			case Pm: /* opcode escape */
				ctxt.Andptr[0] = Pm
				ctxt.Andptr = ctxt.Andptr[1:]
3098

Russ Cox's avatar
Russ Cox committed
3099 3100 3101
			case Pe: /* 16 bit escape */
				ctxt.Andptr[0] = Pe
				ctxt.Andptr = ctxt.Andptr[1:]
3102

Russ Cox's avatar
Russ Cox committed
3103 3104 3105 3106 3107
			case Pw: /* 64-bit escape */
				if p.Mode != 64 {
					ctxt.Diag("asmins: illegal 64: %v", p)
				}
				ctxt.Rexflag |= Pw
3108

3109 3110 3111 3112 3113 3114 3115
			case Pw8: /* 64-bit escape if z >= 8 */
				if z >= 8 {
					if p.Mode != 64 {
						ctxt.Diag("asmins: illegal 64: %v", p)
					}
					ctxt.Rexflag |= Pw
				}
3116

3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132
			case Pb: /* botch */
				if p.Mode != 64 && (isbadbyte(&p.From) || isbadbyte(&p.To)) {
					goto bad
				}
				// NOTE(rsc): This is probably safe to do always,
				// but when enabled it chooses different encodings
				// than the old cmd/internal/obj/i386 code did,
				// which breaks our "same bits out" checks.
				// In particular, CMPB AX, $0 encodes as 80 f8 00
				// in the original obj/i386, and it would encode
				// (using a valid, shorter form) as 3c 00 if we enabled
				// the call to bytereg here.
				if p.Mode == 64 {
					bytereg(&p.From, &p.Ft)
					bytereg(&p.To, &p.Tt)
				}
3133

Russ Cox's avatar
Russ Cox committed
3134 3135 3136 3137
			case P32: /* 32 bit but illegal if 64-bit mode */
				if p.Mode == 64 {
					ctxt.Diag("asmins: illegal in 64-bit mode: %v", p)
				}
3138

Russ Cox's avatar
Russ Cox committed
3139 3140 3141 3142
			case Py: /* 64-bit only, no prefix */
				if p.Mode != 64 {
					ctxt.Diag("asmins: illegal in %d-bit mode: %v", p.Mode, p)
				}
3143 3144 3145 3146 3147 3148 3149 3150 3151 3152

			case Py1: /* 64-bit only if z < 1, no prefix */
				if z < 1 && p.Mode != 64 {
					ctxt.Diag("asmins: illegal in %d-bit mode: %v", p.Mode, p)
				}

			case Py3: /* 64-bit only if z < 3, no prefix */
				if z < 3 && p.Mode != 64 {
					ctxt.Diag("asmins: illegal in %d-bit mode: %v", p.Mode, p)
				}
3153 3154
			}

Russ Cox's avatar
Russ Cox committed
3155 3156 3157
			if z >= len(o.op) {
				log.Fatalf("asmins bad table %v", p)
			}
3158
			op = int(o.op[z])
Russ Cox's avatar
Russ Cox committed
3159 3160 3161 3162 3163
			if op == 0x0f {
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				z++
				op = int(o.op[z])
3164 3165
			}

Russ Cox's avatar
Russ Cox committed
3166 3167 3168 3169
			switch yt.zcase {
			default:
				ctxt.Diag("asmins: unknown z %d %v", yt.zcase, p)
				return
3170

Russ Cox's avatar
Russ Cox committed
3171
			case Zpseudo:
3172 3173
				break

Russ Cox's avatar
Russ Cox committed
3174 3175 3176 3177 3178 3179 3180 3181 3182
			case Zlit:
				for ; ; z++ {
					op = int(o.op[z])
					if op == 0 {
						break
					}
					ctxt.Andptr[0] = byte(op)
					ctxt.Andptr = ctxt.Andptr[1:]
				}
3183

Russ Cox's avatar
Russ Cox committed
3184 3185 3186 3187 3188 3189 3190 3191 3192 3193
			case Zlitm_r:
				for ; ; z++ {
					op = int(o.op[z])
					if op == 0 {
						break
					}
					ctxt.Andptr[0] = byte(op)
					ctxt.Andptr = ctxt.Andptr[1:]
				}
				asmand(ctxt, p, &p.From, &p.To)
3194

Russ Cox's avatar
Russ Cox committed
3195 3196 3197
			case Zmb_r:
				bytereg(&p.From, &p.Ft)
				fallthrough
3198

Russ Cox's avatar
Russ Cox committed
3199 3200 3201 3202
				/* fall through */
			case Zm_r:
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
3203

Russ Cox's avatar
Russ Cox committed
3204
				asmand(ctxt, p, &p.From, &p.To)
3205

Russ Cox's avatar
Russ Cox committed
3206 3207 3208 3209 3210 3211
			case Zm2_r:
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				ctxt.Andptr[0] = byte(o.op[z+1])
				ctxt.Andptr = ctxt.Andptr[1:]
				asmand(ctxt, p, &p.From, &p.To)
3212

Russ Cox's avatar
Russ Cox committed
3213 3214 3215
			case Zm_r_xm:
				mediaop(ctxt, o, op, int(yt.zoffset), z)
				asmand(ctxt, p, &p.From, &p.To)
3216

3217 3218 3219 3220 3221 3222 3223
			case Zm_r_xm_vex:
				ctxt.Vexflag = 1
				vexprefix(ctxt, &p.To, &p.From, o.prefix)
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				asmand(ctxt, p, &p.From, &p.To)

Russ Cox's avatar
Russ Cox committed
3224 3225 3226 3227
			case Zm_r_xm_nr:
				ctxt.Rexflag = 0
				mediaop(ctxt, o, op, int(yt.zoffset), z)
				asmand(ctxt, p, &p.From, &p.To)
3228

Russ Cox's avatar
Russ Cox committed
3229 3230
			case Zm_r_i_xm:
				mediaop(ctxt, o, op, int(yt.zoffset), z)
3231
				asmand(ctxt, p, &p.From, p.From3)
Russ Cox's avatar
Russ Cox committed
3232 3233
				ctxt.Andptr[0] = byte(p.To.Offset)
				ctxt.Andptr = ctxt.Andptr[1:]
3234

Russ Cox's avatar
Russ Cox committed
3235 3236 3237 3238 3239 3240 3241 3242
			case Zm_r_3d:
				ctxt.Andptr[0] = 0x0f
				ctxt.Andptr = ctxt.Andptr[1:]
				ctxt.Andptr[0] = 0x0f
				ctxt.Andptr = ctxt.Andptr[1:]
				asmand(ctxt, p, &p.From, &p.To)
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
3243

Russ Cox's avatar
Russ Cox committed
3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254
			case Zibm_r:
				for {
					tmp1 := z
					z++
					op = int(o.op[tmp1])
					if op == 0 {
						break
					}
					ctxt.Andptr[0] = byte(op)
					ctxt.Andptr = ctxt.Andptr[1:]
				}
3255
				asmand(ctxt, p, p.From3, &p.To)
3256
				ctxt.Andptr[0] = byte(p.From.Offset)
Russ Cox's avatar
Russ Cox committed
3257
				ctxt.Andptr = ctxt.Andptr[1:]
3258

Russ Cox's avatar
Russ Cox committed
3259 3260 3261 3262 3263 3264 3265 3266 3267
			case Zaut_r:
				ctxt.Andptr[0] = 0x8d
				ctxt.Andptr = ctxt.Andptr[1:] /* leal */
				if p.From.Type != obj.TYPE_ADDR {
					ctxt.Diag("asmins: Zaut sb type ADDR")
				}
				p.From.Type = obj.TYPE_MEM
				asmand(ctxt, p, &p.From, &p.To)
				p.From.Type = obj.TYPE_ADDR
3268

Russ Cox's avatar
Russ Cox committed
3269 3270 3271 3272
			case Zm_o:
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				asmando(ctxt, p, &p.From, int(o.op[z+1]))
3273

Russ Cox's avatar
Russ Cox committed
3274 3275 3276 3277
			case Zr_m:
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				asmand(ctxt, p, &p.To, &p.From)
3278

3279 3280 3281 3282 3283 3284 3285
			case Zr_m_xm_vex:
				ctxt.Vexflag = 1
				vexprefix(ctxt, &p.From, &p.To, o.prefix)
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				asmand(ctxt, p, &p.To, &p.From)

Russ Cox's avatar
Russ Cox committed
3286 3287 3288
			case Zr_m_xm:
				mediaop(ctxt, o, op, int(yt.zoffset), z)
				asmand(ctxt, p, &p.To, &p.From)
3289

Russ Cox's avatar
Russ Cox committed
3290 3291 3292 3293
			case Zr_m_xm_nr:
				ctxt.Rexflag = 0
				mediaop(ctxt, o, op, int(yt.zoffset), z)
				asmand(ctxt, p, &p.To, &p.From)
3294

Russ Cox's avatar
Russ Cox committed
3295 3296 3297 3298
			case Zo_m:
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				asmando(ctxt, p, &p.To, int(o.op[z+1]))
3299

Russ Cox's avatar
Russ Cox committed
3300
			case Zcallindreg:
3301
				r = obj.Addrel(ctxt.Cursym)
Russ Cox's avatar
Russ Cox committed
3302 3303 3304 3305
				r.Off = int32(p.Pc)
				r.Type = obj.R_CALLIND
				r.Siz = 0
				fallthrough
3306

Russ Cox's avatar
Russ Cox committed
3307 3308 3309 3310
			case Zo_m64:
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				asmandsz(ctxt, p, &p.To, int(o.op[z+1]), 0, 1)
3311

Russ Cox's avatar
Russ Cox committed
3312 3313 3314 3315 3316 3317
			case Zm_ibo:
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				asmando(ctxt, p, &p.From, int(o.op[z+1]))
				ctxt.Andptr[0] = byte(vaddr(ctxt, p, &p.To, nil))
				ctxt.Andptr = ctxt.Andptr[1:]
3318

Russ Cox's avatar
Russ Cox committed
3319 3320 3321 3322 3323 3324
			case Zibo_m:
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				asmando(ctxt, p, &p.To, int(o.op[z+1]))
				ctxt.Andptr[0] = byte(vaddr(ctxt, p, &p.From, nil))
				ctxt.Andptr = ctxt.Andptr[1:]
3325

Russ Cox's avatar
Russ Cox committed
3326 3327 3328 3329 3330
			case Zibo_m_xm:
				z = mediaop(ctxt, o, op, int(yt.zoffset), z)
				asmando(ctxt, p, &p.To, int(o.op[z+1]))
				ctxt.Andptr[0] = byte(vaddr(ctxt, p, &p.From, nil))
				ctxt.Andptr = ctxt.Andptr[1:]
3331

3332
			case Z_ib, Zib_:
Russ Cox's avatar
Russ Cox committed
3333 3334 3335 3336 3337 3338 3339 3340 3341
				if yt.zcase == Zib_ {
					a = &p.From
				} else {
					a = &p.To
				}
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				ctxt.Andptr[0] = byte(vaddr(ctxt, p, a, nil))
				ctxt.Andptr = ctxt.Andptr[1:]
3342

Russ Cox's avatar
Russ Cox committed
3343 3344 3345 3346 3347 3348
			case Zib_rp:
				ctxt.Rexflag |= regrex[p.To.Reg] & (Rxb | 0x40)
				ctxt.Andptr[0] = byte(op + reg[p.To.Reg])
				ctxt.Andptr = ctxt.Andptr[1:]
				ctxt.Andptr[0] = byte(vaddr(ctxt, p, &p.From, nil))
				ctxt.Andptr = ctxt.Andptr[1:]
3349

Russ Cox's avatar
Russ Cox committed
3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362
			case Zil_rp:
				ctxt.Rexflag |= regrex[p.To.Reg] & Rxb
				ctxt.Andptr[0] = byte(op + reg[p.To.Reg])
				ctxt.Andptr = ctxt.Andptr[1:]
				if o.prefix == Pe {
					v = vaddr(ctxt, p, &p.From, nil)
					ctxt.Andptr[0] = byte(v)
					ctxt.Andptr = ctxt.Andptr[1:]
					ctxt.Andptr[0] = byte(v >> 8)
					ctxt.Andptr = ctxt.Andptr[1:]
				} else {
					relput4(ctxt, p, &p.From)
				}
3363

Russ Cox's avatar
Russ Cox committed
3364 3365 3366 3367 3368 3369 3370 3371 3372 3373
			case Zo_iw:
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				if p.From.Type != obj.TYPE_NONE {
					v = vaddr(ctxt, p, &p.From, nil)
					ctxt.Andptr[0] = byte(v)
					ctxt.Andptr = ctxt.Andptr[1:]
					ctxt.Andptr[0] = byte(v >> 8)
					ctxt.Andptr = ctxt.Andptr[1:]
				}
3374

Russ Cox's avatar
Russ Cox committed
3375 3376 3377 3378 3379
			case Ziq_rp:
				v = vaddr(ctxt, p, &p.From, &rel)
				l = int(v >> 32)
				if l == 0 && rel.Siz != 8 {
					//p->mark |= 0100;
3380
					//print("zero: %llux %v\n", v, p);
Russ Cox's avatar
Russ Cox committed
3381
					ctxt.Rexflag &^= (0x40 | Rxw)
3382

Russ Cox's avatar
Russ Cox committed
3383 3384 3385 3386 3387 3388 3389 3390
					ctxt.Rexflag |= regrex[p.To.Reg] & Rxb
					ctxt.Andptr[0] = byte(0xb8 + reg[p.To.Reg])
					ctxt.Andptr = ctxt.Andptr[1:]
					if rel.Type != 0 {
						r = obj.Addrel(ctxt.Cursym)
						*r = rel
						r.Off = int32(p.Pc + int64(-cap(ctxt.Andptr)+cap(ctxt.And[:])))
					}
3391

Russ Cox's avatar
Russ Cox committed
3392 3393
					put4(ctxt, int32(v))
				} else if l == -1 && uint64(v)&(uint64(1)<<31) != 0 { /* sign extend */
3394

Russ Cox's avatar
Russ Cox committed
3395
					//p->mark |= 0100;
3396
					//print("sign: %llux %v\n", v, p);
Russ Cox's avatar
Russ Cox committed
3397 3398
					ctxt.Andptr[0] = 0xc7
					ctxt.Andptr = ctxt.Andptr[1:]
3399

Russ Cox's avatar
Russ Cox committed
3400 3401 3402
					asmando(ctxt, p, &p.To, 0)
					put4(ctxt, int32(v)) /* need all 8 */
				} else {
3403
					//print("all: %llux %v\n", v, p);
Russ Cox's avatar
Russ Cox committed
3404
					ctxt.Rexflag |= regrex[p.To.Reg] & Rxb
3405

Russ Cox's avatar
Russ Cox committed
3406 3407 3408 3409 3410 3411 3412
					ctxt.Andptr[0] = byte(op + reg[p.To.Reg])
					ctxt.Andptr = ctxt.Andptr[1:]
					if rel.Type != 0 {
						r = obj.Addrel(ctxt.Cursym)
						*r = rel
						r.Off = int32(p.Pc + int64(-cap(ctxt.Andptr)+cap(ctxt.And[:])))
					}
3413

Russ Cox's avatar
Russ Cox committed
3414 3415
					put8(ctxt, v)
				}
3416

Russ Cox's avatar
Russ Cox committed
3417 3418 3419 3420 3421 3422
			case Zib_rr:
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				asmand(ctxt, p, &p.To, &p.To)
				ctxt.Andptr[0] = byte(vaddr(ctxt, p, &p.From, nil))
				ctxt.Andptr = ctxt.Andptr[1:]
3423

3424
			case Z_il, Zil_:
Russ Cox's avatar
Russ Cox committed
3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440
				if yt.zcase == Zil_ {
					a = &p.From
				} else {
					a = &p.To
				}
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				if o.prefix == Pe {
					v = vaddr(ctxt, p, a, nil)
					ctxt.Andptr[0] = byte(v)
					ctxt.Andptr = ctxt.Andptr[1:]
					ctxt.Andptr[0] = byte(v >> 8)
					ctxt.Andptr = ctxt.Andptr[1:]
				} else {
					relput4(ctxt, p, a)
				}
3441

3442
			case Zm_ilo, Zilo_m:
Russ Cox's avatar
Russ Cox committed
3443 3444 3445 3446 3447 3448 3449 3450 3451
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				if yt.zcase == Zilo_m {
					a = &p.From
					asmando(ctxt, p, &p.To, int(o.op[z+1]))
				} else {
					a = &p.To
					asmando(ctxt, p, &p.From, int(o.op[z+1]))
				}
3452

Russ Cox's avatar
Russ Cox committed
3453 3454 3455 3456 3457
				if o.prefix == Pe {
					v = vaddr(ctxt, p, a, nil)
					ctxt.Andptr[0] = byte(v)
					ctxt.Andptr = ctxt.Andptr[1:]
					ctxt.Andptr[0] = byte(v >> 8)
3458
					ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3459 3460
				} else {
					relput4(ctxt, p, a)
3461
				}
Russ Cox's avatar
Russ Cox committed
3462 3463

			case Zil_rr:
3464 3465
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3466 3467 3468 3469
				asmand(ctxt, p, &p.To, &p.To)
				if o.prefix == Pe {
					v = vaddr(ctxt, p, &p.From, nil)
					ctxt.Andptr[0] = byte(v)
3470
					ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3471 3472 3473 3474
					ctxt.Andptr[0] = byte(v >> 8)
					ctxt.Andptr = ctxt.Andptr[1:]
				} else {
					relput4(ctxt, p, &p.From)
3475 3476
				}

Russ Cox's avatar
Russ Cox committed
3477 3478 3479
			case Z_rp:
				ctxt.Rexflag |= regrex[p.To.Reg] & (Rxb | 0x40)
				ctxt.Andptr[0] = byte(op + reg[p.To.Reg])
3480
				ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3481 3482 3483 3484

			case Zrp_:
				ctxt.Rexflag |= regrex[p.From.Reg] & (Rxb | 0x40)
				ctxt.Andptr[0] = byte(op + reg[p.From.Reg])
3485
				ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3486 3487 3488 3489

			case Zclr:
				ctxt.Rexflag &^= Pw
				ctxt.Andptr[0] = byte(op)
3490
				ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3491 3492
				asmand(ctxt, p, &p.To, &p.To)

3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520
			case Zcallcon, Zjmpcon:
				if yt.zcase == Zcallcon {
					ctxt.Andptr[0] = byte(op)
					ctxt.Andptr = ctxt.Andptr[1:]
				} else {
					ctxt.Andptr[0] = byte(o.op[z+1])
					ctxt.Andptr = ctxt.Andptr[1:]
				}
				r = obj.Addrel(ctxt.Cursym)
				r.Off = int32(p.Pc + int64(-cap(ctxt.Andptr)+cap(ctxt.And[:])))
				r.Type = obj.R_PCREL
				r.Siz = 4
				r.Add = p.To.Offset
				put4(ctxt, 0)

			case Zcallind:
				ctxt.Andptr[0] = byte(op)
				ctxt.Andptr = ctxt.Andptr[1:]
				ctxt.Andptr[0] = byte(o.op[z+1])
				ctxt.Andptr = ctxt.Andptr[1:]
				r = obj.Addrel(ctxt.Cursym)
				r.Off = int32(p.Pc + int64(-cap(ctxt.Andptr)+cap(ctxt.And[:])))
				r.Type = obj.R_ADDR
				r.Siz = 4
				r.Add = p.To.Offset
				r.Sym = p.To.Sym
				put4(ctxt, 0)

3521
			case Zcall, Zcallduff:
Russ Cox's avatar
Russ Cox committed
3522 3523 3524 3525 3526
				if p.To.Sym == nil {
					ctxt.Diag("call without target")
					log.Fatalf("bad code")
				}

3527 3528 3529 3530
				if yt.zcase == Zcallduff && ctxt.Flag_dynlink {
					ctxt.Diag("directly calling duff when dynamically linking Go")
				}

3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541
				if obj.Framepointer_enabled != 0 && yt.zcase == Zcallduff && p.Mode == 64 {
					// Maintain BP around call, since duffcopy/duffzero can't do it
					// (the call jumps into the middle of the function).
					// This makes it possible to see call sites for duffcopy/duffzero in
					// BP-based profiling tools like Linux perf (which is the
					// whole point of obj.Framepointer_enabled).
					// MOVQ BP, -16(SP)
					// LEAQ -16(SP), BP
					copy(ctxt.Andptr, bpduff1)
					ctxt.Andptr = ctxt.Andptr[len(bpduff1):]
				}
Russ Cox's avatar
Russ Cox committed
3542
				ctxt.Andptr[0] = byte(op)
3543
				ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3544 3545 3546 3547 3548 3549 3550 3551
				r = obj.Addrel(ctxt.Cursym)
				r.Off = int32(p.Pc + int64(-cap(ctxt.Andptr)+cap(ctxt.And[:])))
				r.Sym = p.To.Sym
				r.Add = p.To.Offset
				r.Type = obj.R_CALL
				r.Siz = 4
				put4(ctxt, 0)

3552 3553 3554 3555 3556 3557 3558 3559
				if obj.Framepointer_enabled != 0 && yt.zcase == Zcallduff && p.Mode == 64 {
					// Pop BP pushed above.
					// MOVQ 0(BP), BP
					copy(ctxt.Andptr, bpduff2)
					ctxt.Andptr = ctxt.Andptr[len(bpduff2):]
				}

			// TODO: jump across functions needs reloc
3560
			case Zbr, Zjmp, Zloop:
Russ Cox's avatar
Russ Cox committed
3561 3562 3563 3564 3565
				if p.To.Sym != nil {
					if yt.zcase != Zjmp {
						ctxt.Diag("branch to ATEXT")
						log.Fatalf("bad code")
					}
3566

Russ Cox's avatar
Russ Cox committed
3567 3568 3569 3570 3571 3572 3573 3574 3575 3576
					ctxt.Andptr[0] = byte(o.op[z+1])
					ctxt.Andptr = ctxt.Andptr[1:]
					r = obj.Addrel(ctxt.Cursym)
					r.Off = int32(p.Pc + int64(-cap(ctxt.Andptr)+cap(ctxt.And[:])))
					r.Sym = p.To.Sym
					r.Type = obj.R_PCREL
					r.Siz = 4
					put4(ctxt, 0)
					break
				}
3577

Russ Cox's avatar
Russ Cox committed
3578 3579
				// Assumes q is in this function.
				// TODO: Check in input, preserve in brchain.
3580

Russ Cox's avatar
Russ Cox committed
3581 3582
				// Fill in backward jump now.
				q = p.Pcond
3583

Russ Cox's avatar
Russ Cox committed
3584 3585 3586 3587
				if q == nil {
					ctxt.Diag("jmp/branch/loop without target")
					log.Fatalf("bad code")
				}
3588

Russ Cox's avatar
Russ Cox committed
3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620
				if p.Back&1 != 0 {
					v = q.Pc - (p.Pc + 2)
					if v >= -128 {
						if p.As == AJCXZL {
							ctxt.Andptr[0] = 0x67
							ctxt.Andptr = ctxt.Andptr[1:]
						}
						ctxt.Andptr[0] = byte(op)
						ctxt.Andptr = ctxt.Andptr[1:]
						ctxt.Andptr[0] = byte(v)
						ctxt.Andptr = ctxt.Andptr[1:]
					} else if yt.zcase == Zloop {
						ctxt.Diag("loop too far: %v", p)
					} else {
						v -= 5 - 2
						if yt.zcase == Zbr {
							ctxt.Andptr[0] = 0x0f
							ctxt.Andptr = ctxt.Andptr[1:]
							v--
						}

						ctxt.Andptr[0] = byte(o.op[z+1])
						ctxt.Andptr = ctxt.Andptr[1:]
						ctxt.Andptr[0] = byte(v)
						ctxt.Andptr = ctxt.Andptr[1:]
						ctxt.Andptr[0] = byte(v >> 8)
						ctxt.Andptr = ctxt.Andptr[1:]
						ctxt.Andptr[0] = byte(v >> 16)
						ctxt.Andptr = ctxt.Andptr[1:]
						ctxt.Andptr[0] = byte(v >> 24)
						ctxt.Andptr = ctxt.Andptr[1:]
					}
3621

Russ Cox's avatar
Russ Cox committed
3622 3623
					break
				}
3624

Russ Cox's avatar
Russ Cox committed
3625
				// Annotate target; will fill in later.
3626
				p.Forwd = q.Rel
Russ Cox's avatar
Russ Cox committed
3627

3628
				q.Rel = p
Russ Cox's avatar
Russ Cox committed
3629 3630 3631 3632 3633 3634 3635 3636
				if p.Back&2 != 0 { // short
					if p.As == AJCXZL {
						ctxt.Andptr[0] = 0x67
						ctxt.Andptr = ctxt.Andptr[1:]
					}
					ctxt.Andptr[0] = byte(op)
					ctxt.Andptr = ctxt.Andptr[1:]
					ctxt.Andptr[0] = 0
3637
					ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649
				} else if yt.zcase == Zloop {
					ctxt.Diag("loop too far: %v", p)
				} else {
					if yt.zcase == Zbr {
						ctxt.Andptr[0] = 0x0f
						ctxt.Andptr = ctxt.Andptr[1:]
					}
					ctxt.Andptr[0] = byte(o.op[z+1])
					ctxt.Andptr = ctxt.Andptr[1:]
					ctxt.Andptr[0] = 0
					ctxt.Andptr = ctxt.Andptr[1:]
					ctxt.Andptr[0] = 0
3650
					ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3651
					ctxt.Andptr[0] = 0
3652
					ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3653
					ctxt.Andptr[0] = 0
3654 3655
					ctxt.Andptr = ctxt.Andptr[1:]
				}
Russ Cox's avatar
Russ Cox committed
3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708

				break

			/*
				v = q->pc - p->pc - 2;
				if((v >= -128 && v <= 127) || p->pc == -1 || q->pc == -1) {
					*ctxt->andptr++ = op;
					*ctxt->andptr++ = v;
				} else {
					v -= 5-2;
					if(yt.zcase == Zbr) {
						*ctxt->andptr++ = 0x0f;
						v--;
					}
					*ctxt->andptr++ = o->op[z+1];
					*ctxt->andptr++ = v;
					*ctxt->andptr++ = v>>8;
					*ctxt->andptr++ = v>>16;
					*ctxt->andptr++ = v>>24;
				}
			*/

			case Zbyte:
				v = vaddr(ctxt, p, &p.From, &rel)
				if rel.Siz != 0 {
					rel.Siz = uint8(op)
					r = obj.Addrel(ctxt.Cursym)
					*r = rel
					r.Off = int32(p.Pc + int64(-cap(ctxt.Andptr)+cap(ctxt.And[:])))
				}

				ctxt.Andptr[0] = byte(v)
				ctxt.Andptr = ctxt.Andptr[1:]
				if op > 1 {
					ctxt.Andptr[0] = byte(v >> 8)
					ctxt.Andptr = ctxt.Andptr[1:]
					if op > 2 {
						ctxt.Andptr[0] = byte(v >> 16)
						ctxt.Andptr = ctxt.Andptr[1:]
						ctxt.Andptr[0] = byte(v >> 24)
						ctxt.Andptr = ctxt.Andptr[1:]
						if op > 4 {
							ctxt.Andptr[0] = byte(v >> 32)
							ctxt.Andptr = ctxt.Andptr[1:]
							ctxt.Andptr[0] = byte(v >> 40)
							ctxt.Andptr = ctxt.Andptr[1:]
							ctxt.Andptr[0] = byte(v >> 48)
							ctxt.Andptr = ctxt.Andptr[1:]
							ctxt.Andptr[0] = byte(v >> 56)
							ctxt.Andptr = ctxt.Andptr[1:]
						}
					}
				}
3709
			}
Russ Cox's avatar
Russ Cox committed
3710 3711

			return
3712
		}
Russ Cox's avatar
Russ Cox committed
3713
		z += int(yt.zoffset) + xo
3714
	}
Russ Cox's avatar
Russ Cox committed
3715
	for mo := ymovtab; mo[0].as != 0; mo = mo[1:] {
3716 3717
		var pp obj.Prog
		var t []byte
3718
		if p.As == mo[0].as {
3719
			if ycover[ft+int(mo[0].ft)] != 0 && ycover[f3t+int(mo[0].f3t)] != 0 && ycover[tt+int(mo[0].tt)] != 0 {
3720 3721 3722 3723 3724 3725 3726 3727 3728 3729
				t = mo[0].op[:]
				switch mo[0].code {
				default:
					ctxt.Diag("asmins: unknown mov %d %v", mo[0].code, p)

				case 0: /* lit */
					for z = 0; t[z] != E; z++ {
						ctxt.Andptr[0] = t[z]
						ctxt.Andptr = ctxt.Andptr[1:]
					}
Russ Cox's avatar
Russ Cox committed
3730

3731 3732 3733
				case 1: /* r,m */
					ctxt.Andptr[0] = t[0]
					ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3734

3735 3736 3737 3738 3739
					asmando(ctxt, p, &p.To, int(t[1]))

				case 2: /* m,r */
					ctxt.Andptr[0] = t[0]
					ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3740

3741
					asmando(ctxt, p, &p.From, int(t[1]))
Russ Cox's avatar
Russ Cox committed
3742

3743 3744 3745
				case 3: /* r,m - 2op */
					ctxt.Andptr[0] = t[0]
					ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3746

3747 3748 3749 3750
					ctxt.Andptr[0] = t[1]
					ctxt.Andptr = ctxt.Andptr[1:]
					asmando(ctxt, p, &p.To, int(t[2]))
					ctxt.Rexflag |= regrex[p.From.Reg] & (Rxr | 0x40)
Russ Cox's avatar
Russ Cox committed
3751

3752 3753 3754
				case 4: /* m,r - 2op */
					ctxt.Andptr[0] = t[0]
					ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3755

3756 3757 3758 3759
					ctxt.Andptr[0] = t[1]
					ctxt.Andptr = ctxt.Andptr[1:]
					asmando(ctxt, p, &p.From, int(t[2]))
					ctxt.Rexflag |= regrex[p.To.Reg] & (Rxr | 0x40)
Russ Cox's avatar
Russ Cox committed
3760

3761 3762
				case 5: /* load full pointer, trash heap */
					if t[0] != 0 {
Russ Cox's avatar
Russ Cox committed
3763 3764
						ctxt.Andptr[0] = t[0]
						ctxt.Andptr = ctxt.Andptr[1:]
3765 3766 3767 3768
					}
					switch p.To.Index {
					default:
						goto bad
Russ Cox's avatar
Russ Cox committed
3769

3770 3771
					case REG_DS:
						ctxt.Andptr[0] = 0xc5
Russ Cox's avatar
Russ Cox committed
3772 3773
						ctxt.Andptr = ctxt.Andptr[1:]

3774 3775 3776 3777 3778
					case REG_SS:
						ctxt.Andptr[0] = 0x0f
						ctxt.Andptr = ctxt.Andptr[1:]
						ctxt.Andptr[0] = 0xb2
						ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3779

3780 3781 3782
					case REG_ES:
						ctxt.Andptr[0] = 0xc4
						ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3783

3784 3785 3786 3787 3788
					case REG_FS:
						ctxt.Andptr[0] = 0x0f
						ctxt.Andptr = ctxt.Andptr[1:]
						ctxt.Andptr[0] = 0xb4
						ctxt.Andptr = ctxt.Andptr[1:]
Russ Cox's avatar
Russ Cox committed
3789

3790 3791 3792 3793 3794 3795
					case REG_GS:
						ctxt.Andptr[0] = 0x0f
						ctxt.Andptr = ctxt.Andptr[1:]
						ctxt.Andptr[0] = 0xb5
						ctxt.Andptr = ctxt.Andptr[1:]
					}
Russ Cox's avatar
Russ Cox committed
3796

3797
					asmand(ctxt, p, &p.From, &p.To)
Russ Cox's avatar
Russ Cox committed
3798

3799 3800 3801 3802
				case 6: /* double shift */
					if t[0] == Pw {
						if p.Mode != 64 {
							ctxt.Diag("asmins: illegal 64: %v", p)
Russ Cox's avatar
Russ Cox committed
3803
						}
3804 3805 3806 3807 3808 3809 3810
						ctxt.Rexflag |= Pw
						t = t[1:]
					} else if t[0] == Pe {
						ctxt.Andptr[0] = Pe
						ctxt.Andptr = ctxt.Andptr[1:]
						t = t[1:]
					}
Russ Cox's avatar
Russ Cox committed
3811

3812 3813 3814
					switch p.From.Type {
					default:
						goto bad
Russ Cox's avatar
Russ Cox committed
3815

3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826
					case obj.TYPE_CONST:
						ctxt.Andptr[0] = 0x0f
						ctxt.Andptr = ctxt.Andptr[1:]
						ctxt.Andptr[0] = t[0]
						ctxt.Andptr = ctxt.Andptr[1:]
						asmandsz(ctxt, p, &p.To, reg[p.From3.Reg], regrex[p.From3.Reg], 0)
						ctxt.Andptr[0] = byte(p.From.Offset)
						ctxt.Andptr = ctxt.Andptr[1:]

					case obj.TYPE_REG:
						switch p.From.Reg {
Russ Cox's avatar
Russ Cox committed
3827 3828 3829
						default:
							goto bad

3830
						case REG_CL, REG_CX:
Russ Cox's avatar
Russ Cox committed
3831 3832
							ctxt.Andptr[0] = 0x0f
							ctxt.Andptr = ctxt.Andptr[1:]
3833
							ctxt.Andptr[0] = t[1]
Russ Cox's avatar
Russ Cox committed
3834
							ctxt.Andptr = ctxt.Andptr[1:]
3835
							asmandsz(ctxt, p, &p.To, reg[p.From3.Reg], regrex[p.From3.Reg], 0)
Russ Cox's avatar
Russ Cox committed
3836
						}
3837
					}
Russ Cox's avatar
Russ Cox committed
3838

3839 3840 3841 3842 3843
				// NOTE: The systems listed here are the ones that use the "TLS initial exec" model,
				// where you load the TLS base register into a register and then index off that
				// register to access the actual TLS variables. Systems that allow direct TLS access
				// are handled in prefixof above and should not be listed here.
				case 7: /* mov tls, r */
3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904
					if p.Mode == 64 && p.As != AMOVQ || p.Mode == 32 && p.As != AMOVL {
						ctxt.Diag("invalid load of TLS: %v", p)
					}

					if p.Mode == 32 {
						// NOTE: The systems listed here are the ones that use the "TLS initial exec" model,
						// where you load the TLS base register into a register and then index off that
						// register to access the actual TLS variables. Systems that allow direct TLS access
						// are handled in prefixof above and should not be listed here.
						switch ctxt.Headtype {
						default:
							log.Fatalf("unknown TLS base location for %s", obj.Headstr(ctxt.Headtype))

						case obj.Hlinux,
							obj.Hnacl:
							// ELF TLS base is 0(GS).
							pp.From = p.From

							pp.From.Type = obj.TYPE_MEM
							pp.From.Reg = REG_GS
							pp.From.Offset = 0
							pp.From.Index = REG_NONE
							pp.From.Scale = 0
							ctxt.Andptr[0] = 0x65
							ctxt.Andptr = ctxt.Andptr[1:] // GS
							ctxt.Andptr[0] = 0x8B
							ctxt.Andptr = ctxt.Andptr[1:]
							asmand(ctxt, p, &pp.From, &p.To)

						case obj.Hplan9:
							if ctxt.Plan9privates == nil {
								ctxt.Plan9privates = obj.Linklookup(ctxt, "_privates", 0)
							}
							pp.From = obj.Addr{}
							pp.From.Type = obj.TYPE_MEM
							pp.From.Name = obj.NAME_EXTERN
							pp.From.Sym = ctxt.Plan9privates
							pp.From.Offset = 0
							pp.From.Index = REG_NONE
							ctxt.Andptr[0] = 0x8B
							ctxt.Andptr = ctxt.Andptr[1:]
							asmand(ctxt, p, &pp.From, &p.To)

						case obj.Hwindows:
							// Windows TLS base is always 0x14(FS).
							pp.From = p.From

							pp.From.Type = obj.TYPE_MEM
							pp.From.Reg = REG_FS
							pp.From.Offset = 0x14
							pp.From.Index = REG_NONE
							pp.From.Scale = 0
							ctxt.Andptr[0] = 0x64
							ctxt.Andptr = ctxt.Andptr[1:] // FS
							ctxt.Andptr[0] = 0x8B
							ctxt.Andptr = ctxt.Andptr[1:]
							asmand(ctxt, p, &pp.From, &p.To)
						}
						break
					}

3905 3906 3907 3908
					switch ctxt.Headtype {
					default:
						log.Fatalf("unknown TLS base location for %s", obj.Headstr(ctxt.Headtype))

3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934
					case obj.Hlinux:
						if ctxt.Flag_shared == 0 {
							log.Fatalf("unknown TLS base location for linux without -shared")
						}
						// Note that this is not generating the same insn as the other cases.
						//     MOV TLS, R_to
						// becomes
						//     movq g@gottpoff(%rip), R_to
						// which is encoded as
						//     movq 0(%rip), R_to
						// and a R_TLS_IE reloc. This all assumes the only tls variable we access
						// is g, which we can't check here, but will when we assemble the second
						// instruction.
						ctxt.Rexflag = Pw | (regrex[p.To.Reg] & Rxr)

						ctxt.Andptr[0] = 0x8B
						ctxt.Andptr = ctxt.Andptr[1:]
						ctxt.Andptr[0] = byte(0x05 | (reg[p.To.Reg] << 3))
						ctxt.Andptr = ctxt.Andptr[1:]
						r = obj.Addrel(ctxt.Cursym)
						r.Off = int32(p.Pc + int64(-cap(ctxt.Andptr)+cap(ctxt.And[:])))
						r.Type = obj.R_TLS_IE
						r.Siz = 4
						r.Add = -4
						put4(ctxt, 0)

3935 3936 3937
					case obj.Hplan9:
						if ctxt.Plan9privates == nil {
							ctxt.Plan9privates = obj.Linklookup(ctxt, "_privates", 0)
Russ Cox's avatar
Russ Cox committed
3938
						}
3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950
						pp.From = obj.Addr{}
						pp.From.Type = obj.TYPE_MEM
						pp.From.Name = obj.NAME_EXTERN
						pp.From.Sym = ctxt.Plan9privates
						pp.From.Offset = 0
						pp.From.Index = REG_NONE
						ctxt.Rexflag |= Pw
						ctxt.Andptr[0] = 0x8B
						ctxt.Andptr = ctxt.Andptr[1:]
						asmand(ctxt, p, &pp.From, &p.To)

					case obj.Hsolaris: // TODO(rsc): Delete Hsolaris from list. Should not use this code. See progedit in obj6.c.
3951
						// TLS base is 0(FS).
3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967
						pp.From = p.From

						pp.From.Type = obj.TYPE_MEM
						pp.From.Name = obj.NAME_NONE
						pp.From.Reg = REG_NONE
						pp.From.Offset = 0
						pp.From.Index = REG_NONE
						pp.From.Scale = 0
						ctxt.Rexflag |= Pw
						ctxt.Andptr[0] = 0x64
						ctxt.Andptr = ctxt.Andptr[1:] // FS
						ctxt.Andptr[0] = 0x8B
						ctxt.Andptr = ctxt.Andptr[1:]
						asmand(ctxt, p, &pp.From, &p.To)

					case obj.Hwindows:
3968
						// Windows TLS base is always 0x28(GS).
3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982
						pp.From = p.From

						pp.From.Type = obj.TYPE_MEM
						pp.From.Name = obj.NAME_NONE
						pp.From.Reg = REG_GS
						pp.From.Offset = 0x28
						pp.From.Index = REG_NONE
						pp.From.Scale = 0
						ctxt.Rexflag |= Pw
						ctxt.Andptr[0] = 0x65
						ctxt.Andptr = ctxt.Andptr[1:] // GS
						ctxt.Andptr[0] = 0x8B
						ctxt.Andptr = ctxt.Andptr[1:]
						asmand(ctxt, p, &pp.From, &p.To)
Russ Cox's avatar
Russ Cox committed
3983
					}
3984
				}
3985
				return
3986 3987 3988
			}
		}
	}
Russ Cox's avatar
Russ Cox committed
3989
	goto bad
3990 3991 3992 3993 3994 3995 3996 3997 3998 3999

bad:
	if p.Mode != 64 {
		/*
		 * here, the assembly has failed.
		 * if its a byte instruction that has
		 * unaddressable registers, try to
		 * exchange registers and reissue the
		 * instruction with the operands renamed.
		 */
Russ Cox's avatar
Russ Cox committed
4000
		pp := *p
4001

4002 4003 4004
		unbytereg(&pp.From, &pp.Ft)
		unbytereg(&pp.To, &pp.Tt)

Russ Cox's avatar
Russ Cox committed
4005
		z := int(p.From.Reg)
4006
		if p.From.Type == obj.TYPE_REG && z >= REG_BP && z <= REG_DI {
4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030
			// TODO(rsc): Use this code for x86-64 too. It has bug fixes not present in the amd64 code base.
			// For now, different to keep bit-for-bit compatibility.
			if p.Mode == 32 {
				breg := byteswapreg(ctxt, &p.To)
				if breg != REG_AX {
					ctxt.Andptr[0] = 0x87
					ctxt.Andptr = ctxt.Andptr[1:] /* xchg lhs,bx */
					asmando(ctxt, p, &p.From, reg[breg])
					subreg(&pp, z, breg)
					doasm(ctxt, &pp)
					ctxt.Andptr[0] = 0x87
					ctxt.Andptr = ctxt.Andptr[1:] /* xchg lhs,bx */
					asmando(ctxt, p, &p.From, reg[breg])
				} else {
					ctxt.Andptr[0] = byte(0x90 + reg[z])
					ctxt.Andptr = ctxt.Andptr[1:] /* xchg lsh,ax */
					subreg(&pp, z, REG_AX)
					doasm(ctxt, &pp)
					ctxt.Andptr[0] = byte(0x90 + reg[z])
					ctxt.Andptr = ctxt.Andptr[1:] /* xchg lsh,ax */
				}
				return
			}

4031
			if isax(&p.To) || p.To.Type == obj.TYPE_NONE {
4032 4033 4034 4035
				// We certainly don't want to exchange
				// with AX if the op is MUL or DIV.
				ctxt.Andptr[0] = 0x87
				ctxt.Andptr = ctxt.Andptr[1:] /* xchg lhs,bx */
4036 4037
				asmando(ctxt, p, &p.From, reg[REG_BX])
				subreg(&pp, z, REG_BX)
4038 4039 4040
				doasm(ctxt, &pp)
				ctxt.Andptr[0] = 0x87
				ctxt.Andptr = ctxt.Andptr[1:] /* xchg lhs,bx */
4041
				asmando(ctxt, p, &p.From, reg[REG_BX])
4042 4043 4044
			} else {
				ctxt.Andptr[0] = byte(0x90 + reg[z])
				ctxt.Andptr = ctxt.Andptr[1:] /* xchg lsh,ax */
4045
				subreg(&pp, z, REG_AX)
4046 4047 4048 4049 4050 4051 4052
				doasm(ctxt, &pp)
				ctxt.Andptr[0] = byte(0x90 + reg[z])
				ctxt.Andptr = ctxt.Andptr[1:] /* xchg lsh,ax */
			}
			return
		}

4053 4054
		z = int(p.To.Reg)
		if p.To.Type == obj.TYPE_REG && z >= REG_BP && z <= REG_DI {
4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078
			// TODO(rsc): Use this code for x86-64 too. It has bug fixes not present in the amd64 code base.
			// For now, different to keep bit-for-bit compatibility.
			if p.Mode == 32 {
				breg := byteswapreg(ctxt, &p.From)
				if breg != REG_AX {
					ctxt.Andptr[0] = 0x87
					ctxt.Andptr = ctxt.Andptr[1:] /* xchg rhs,bx */
					asmando(ctxt, p, &p.To, reg[breg])
					subreg(&pp, z, breg)
					doasm(ctxt, &pp)
					ctxt.Andptr[0] = 0x87
					ctxt.Andptr = ctxt.Andptr[1:] /* xchg rhs,bx */
					asmando(ctxt, p, &p.To, reg[breg])
				} else {
					ctxt.Andptr[0] = byte(0x90 + reg[z])
					ctxt.Andptr = ctxt.Andptr[1:] /* xchg rsh,ax */
					subreg(&pp, z, REG_AX)
					doasm(ctxt, &pp)
					ctxt.Andptr[0] = byte(0x90 + reg[z])
					ctxt.Andptr = ctxt.Andptr[1:] /* xchg rsh,ax */
				}
				return
			}

4079
			if isax(&p.From) {
4080 4081
				ctxt.Andptr[0] = 0x87
				ctxt.Andptr = ctxt.Andptr[1:] /* xchg rhs,bx */
4082 4083
				asmando(ctxt, p, &p.To, reg[REG_BX])
				subreg(&pp, z, REG_BX)
4084 4085 4086
				doasm(ctxt, &pp)
				ctxt.Andptr[0] = 0x87
				ctxt.Andptr = ctxt.Andptr[1:] /* xchg rhs,bx */
4087
				asmando(ctxt, p, &p.To, reg[REG_BX])
4088 4089 4090
			} else {
				ctxt.Andptr[0] = byte(0x90 + reg[z])
				ctxt.Andptr = ctxt.Andptr[1:] /* xchg rsh,ax */
4091
				subreg(&pp, z, REG_AX)
4092 4093 4094 4095 4096 4097 4098 4099
				doasm(ctxt, &pp)
				ctxt.Andptr[0] = byte(0x90 + reg[z])
				ctxt.Andptr = ctxt.Andptr[1:] /* xchg rsh,ax */
			}
			return
		}
	}

4100
	ctxt.Diag("doasm: notfound ft=%d tt=%d %v %d %d", p.Ft, p.Tt, p, oclass(ctxt, p, &p.From), oclass(ctxt, p, &p.To))
4101 4102 4103
	return
}

4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124
// byteswapreg returns a byte-addressable register (AX, BX, CX, DX)
// which is not referenced in a.
// If a is empty, it returns BX to account for MULB-like instructions
// that might use DX and AX.
func byteswapreg(ctxt *obj.Link, a *obj.Addr) int {
	cand := 1
	canc := cand
	canb := canc
	cana := canb

	if a.Type == obj.TYPE_NONE {
		cand = 0
		cana = cand
	}

	if a.Type == obj.TYPE_REG || ((a.Type == obj.TYPE_MEM || a.Type == obj.TYPE_ADDR) && a.Name == obj.NAME_NONE) {
		switch a.Reg {
		case REG_NONE:
			cand = 0
			cana = cand

4125
		case REG_AX, REG_AL, REG_AH:
4126 4127
			cana = 0

4128
		case REG_BX, REG_BL, REG_BH:
4129 4130
			canb = 0

4131
		case REG_CX, REG_CL, REG_CH:
4132 4133
			canc = 0

4134
		case REG_DX, REG_DL, REG_DH:
4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176
			cand = 0
		}
	}

	if a.Type == obj.TYPE_MEM || a.Type == obj.TYPE_ADDR {
		switch a.Index {
		case REG_AX:
			cana = 0

		case REG_BX:
			canb = 0

		case REG_CX:
			canc = 0

		case REG_DX:
			cand = 0
		}
	}

	if cana != 0 {
		return REG_AX
	}
	if canb != 0 {
		return REG_BX
	}
	if canc != 0 {
		return REG_CX
	}
	if cand != 0 {
		return REG_DX
	}

	ctxt.Diag("impossible byte register")
	log.Fatalf("bad code")
	return 0
}

func isbadbyte(a *obj.Addr) bool {
	return a.Type == obj.TYPE_REG && (REG_BP <= a.Reg && a.Reg <= REG_DI || REG_BPB <= a.Reg && a.Reg <= REG_DIB)
}

4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189
var naclret = []uint8{
	0x5e, // POPL SI
	// 0x8b, 0x7d, 0x00, // MOVL (BP), DI - catch return to invalid address, for debugging
	0x83,
	0xe6,
	0xe0, // ANDL $~31, SI
	0x4c,
	0x01,
	0xfe, // ADDQ R15, SI
	0xff,
	0xe6, // JMP SI
}

4190 4191 4192 4193 4194 4195 4196 4197 4198 4199
var naclret8 = []uint8{
	0x5d, // POPL BP
	// 0x8b, 0x7d, 0x00, // MOVL (BP), DI - catch return to invalid address, for debugging
	0x83,
	0xe5,
	0xe0, // ANDL $~31, BP
	0xff,
	0xe5, // JMP BP
}

4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228
var naclspfix = []uint8{0x4c, 0x01, 0xfc} // ADDQ R15, SP

var naclbpfix = []uint8{0x4c, 0x01, 0xfd} // ADDQ R15, BP

var naclmovs = []uint8{
	0x89,
	0xf6, // MOVL SI, SI
	0x49,
	0x8d,
	0x34,
	0x37, // LEAQ (R15)(SI*1), SI
	0x89,
	0xff, // MOVL DI, DI
	0x49,
	0x8d,
	0x3c,
	0x3f, // LEAQ (R15)(DI*1), DI
}

var naclstos = []uint8{
	0x89,
	0xff, // MOVL DI, DI
	0x49,
	0x8d,
	0x3c,
	0x3f, // LEAQ (R15)(DI*1), DI
}

func nacltrunc(ctxt *obj.Link, reg int) {
4229
	if reg >= REG_R8 {
4230 4231 4232
		ctxt.Andptr[0] = 0x45
		ctxt.Andptr = ctxt.Andptr[1:]
	}
4233
	reg = (reg - REG_AX) & 7
4234 4235 4236 4237 4238 4239 4240 4241 4242 4243
	ctxt.Andptr[0] = 0x89
	ctxt.Andptr = ctxt.Andptr[1:]
	ctxt.Andptr[0] = byte(3<<6 | reg<<3 | reg)
	ctxt.Andptr = ctxt.Andptr[1:]
}

func asmins(ctxt *obj.Link, p *obj.Prog) {
	ctxt.Andptr = ctxt.And[:]
	ctxt.Asmode = int(p.Mode)

4244
	if p.As == obj.AUSEFIELD {
Russ Cox's avatar
Russ Cox committed
4245
		r := obj.Addrel(ctxt.Cursym)
4246 4247 4248
		r.Off = 0
		r.Siz = 0
		r.Sym = p.From.Sym
4249
		r.Type = obj.R_USEFIELD
4250 4251 4252
		return
	}

4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278
	if ctxt.Headtype == obj.Hnacl && p.Mode == 32 {
		switch p.As {
		case obj.ARET:
			copy(ctxt.Andptr, naclret8)
			ctxt.Andptr = ctxt.Andptr[len(naclret8):]
			return

		case obj.ACALL,
			obj.AJMP:
			if p.To.Type == obj.TYPE_REG && REG_AX <= p.To.Reg && p.To.Reg <= REG_DI {
				ctxt.Andptr[0] = 0x83
				ctxt.Andptr = ctxt.Andptr[1:]
				ctxt.Andptr[0] = byte(0xe0 | (p.To.Reg - REG_AX))
				ctxt.Andptr = ctxt.Andptr[1:]
				ctxt.Andptr[0] = 0xe0
				ctxt.Andptr = ctxt.Andptr[1:]
			}

		case AINT:
			ctxt.Andptr[0] = 0xf4
			ctxt.Andptr = ctxt.Andptr[1:]
			return
		}
	}

	if ctxt.Headtype == obj.Hnacl && p.Mode == 64 {
4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294
		if p.As == AREP {
			ctxt.Rep++
			return
		}

		if p.As == AREPN {
			ctxt.Repn++
			return
		}

		if p.As == ALOCK {
			ctxt.Lock++
			return
		}

		if p.As != ALEAQ && p.As != ALEAL {
4295
			if p.From.Index != obj.TYPE_NONE && p.From.Scale > 0 {
4296 4297
				nacltrunc(ctxt, int(p.From.Index))
			}
4298
			if p.To.Index != obj.TYPE_NONE && p.To.Scale > 0 {
4299 4300 4301 4302 4303
				nacltrunc(ctxt, int(p.To.Index))
			}
		}

		switch p.As {
4304
		case obj.ARET:
4305 4306 4307 4308
			copy(ctxt.Andptr, naclret)
			ctxt.Andptr = ctxt.Andptr[len(naclret):]
			return

4309 4310 4311
		case obj.ACALL,
			obj.AJMP:
			if p.To.Type == obj.TYPE_REG && REG_AX <= p.To.Reg && p.To.Reg <= REG_DI {
4312 4313 4314 4315
				// ANDL $~31, reg
				ctxt.Andptr[0] = 0x83
				ctxt.Andptr = ctxt.Andptr[1:]

4316
				ctxt.Andptr[0] = byte(0xe0 | (p.To.Reg - REG_AX))
4317 4318 4319 4320 4321 4322 4323 4324 4325 4326
				ctxt.Andptr = ctxt.Andptr[1:]
				ctxt.Andptr[0] = 0xe0
				ctxt.Andptr = ctxt.Andptr[1:]

				// ADDQ R15, reg
				ctxt.Andptr[0] = 0x4c
				ctxt.Andptr = ctxt.Andptr[1:]

				ctxt.Andptr[0] = 0x01
				ctxt.Andptr = ctxt.Andptr[1:]
4327
				ctxt.Andptr[0] = byte(0xf8 | (p.To.Reg - REG_AX))
4328 4329 4330
				ctxt.Andptr = ctxt.Andptr[1:]
			}

4331
			if p.To.Type == obj.TYPE_REG && REG_R8 <= p.To.Reg && p.To.Reg <= REG_R15 {
4332 4333 4334 4335 4336 4337
				// ANDL $~31, reg
				ctxt.Andptr[0] = 0x41
				ctxt.Andptr = ctxt.Andptr[1:]

				ctxt.Andptr[0] = 0x83
				ctxt.Andptr = ctxt.Andptr[1:]
4338
				ctxt.Andptr[0] = byte(0xe0 | (p.To.Reg - REG_R8))
4339 4340 4341 4342 4343 4344 4345 4346 4347 4348
				ctxt.Andptr = ctxt.Andptr[1:]
				ctxt.Andptr[0] = 0xe0
				ctxt.Andptr = ctxt.Andptr[1:]

				// ADDQ R15, reg
				ctxt.Andptr[0] = 0x4d
				ctxt.Andptr = ctxt.Andptr[1:]

				ctxt.Andptr[0] = 0x01
				ctxt.Andptr = ctxt.Andptr[1:]
4349
				ctxt.Andptr[0] = byte(0xf8 | (p.To.Reg - REG_R8))
4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368
				ctxt.Andptr = ctxt.Andptr[1:]
			}

		case AINT:
			ctxt.Andptr[0] = 0xf4
			ctxt.Andptr = ctxt.Andptr[1:]
			return

		case ASCASB,
			ASCASW,
			ASCASL,
			ASCASQ,
			ASTOSB,
			ASTOSW,
			ASTOSL,
			ASTOSQ:
			copy(ctxt.Andptr, naclstos)
			ctxt.Andptr = ctxt.Andptr[len(naclstos):]

4369
		case AMOVSB, AMOVSW, AMOVSL, AMOVSQ:
4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393
			copy(ctxt.Andptr, naclmovs)
			ctxt.Andptr = ctxt.Andptr[len(naclmovs):]
		}

		if ctxt.Rep != 0 {
			ctxt.Andptr[0] = 0xf3
			ctxt.Andptr = ctxt.Andptr[1:]
			ctxt.Rep = 0
		}

		if ctxt.Repn != 0 {
			ctxt.Andptr[0] = 0xf2
			ctxt.Andptr = ctxt.Andptr[1:]
			ctxt.Repn = 0
		}

		if ctxt.Lock != 0 {
			ctxt.Andptr[0] = 0xf0
			ctxt.Andptr = ctxt.Andptr[1:]
			ctxt.Lock = 0
		}
	}

	ctxt.Rexflag = 0
4394
	ctxt.Vexflag = 0
Russ Cox's avatar
Russ Cox committed
4395
	and0 := ctxt.Andptr
4396 4397
	ctxt.Asmode = int(p.Mode)
	doasm(ctxt, p)
4398
	if ctxt.Rexflag != 0 && ctxt.Vexflag == 0 {
4399 4400 4401 4402 4403 4404 4405 4406
		/*
		 * as befits the whole approach of the architecture,
		 * the rex prefix must appear before the first opcode byte
		 * (and thus after any 66/67/f2/f3/26/2e/3e prefix bytes, but
		 * before the 0f opcode escape!), or it might be ignored.
		 * note that the handbook often misleadingly shows 66/f2/f3 in `opcode'.
		 */
		if p.Mode != 64 {
4407
			ctxt.Diag("asmins: illegal in mode %d: %v (%d %d)", p.Mode, p, p.Ft, p.Tt)
4408
		}
Russ Cox's avatar
Russ Cox committed
4409 4410 4411
		n := -cap(ctxt.Andptr) + cap(and0)
		var c int
		var np int
4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423
		for np = 0; np < n; np++ {
			c = int(and0[np])
			if c != 0xf2 && c != 0xf3 && (c < 0x64 || c > 0x67) && c != 0x2e && c != 0x3e && c != 0x26 {
				break
			}
		}

		copy(and0[np+1:], and0[np:n])
		and0[np] = byte(0x40 | ctxt.Rexflag)
		ctxt.Andptr = ctxt.Andptr[1:]
	}

Russ Cox's avatar
Russ Cox committed
4424 4425 4426
	n := -cap(ctxt.Andptr) + cap(ctxt.And[:])
	var r *obj.Reloc
	for i := len(ctxt.Cursym.R) - 1; i >= 0; i-- {
4427 4428 4429 4430 4431 4432 4433
		r = &ctxt.Cursym.R[i:][0]
		if int64(r.Off) < p.Pc {
			break
		}
		if ctxt.Rexflag != 0 {
			r.Off++
		}
4434 4435 4436 4437 4438 4439 4440 4441
		if r.Type == obj.R_PCREL {
			// PC-relative addressing is relative to the end of the instruction,
			// but the relocations applied by the linker are relative to the end
			// of the relocation. Because immediate instruction
			// arguments can follow the PC-relative memory reference in the
			// instruction encoding, the two may not coincide. In this case,
			// adjust addend so that linker can keep relocating relative to the
			// end of the relocation.
4442 4443 4444 4445
			r.Add -= p.Pc + int64(n) - (int64(r.Off) + int64(r.Siz))
		}
	}

4446
	if p.Mode == 64 && ctxt.Headtype == obj.Hnacl && p.As != ACMPL && p.As != ACMPQ && p.To.Type == obj.TYPE_REG {
4447 4448
		switch p.To.Reg {
		case REG_SP:
4449 4450 4451
			copy(ctxt.Andptr, naclspfix)
			ctxt.Andptr = ctxt.Andptr[len(naclspfix):]

4452
		case REG_BP:
4453 4454 4455 4456 4457
			copy(ctxt.Andptr, naclbpfix)
			ctxt.Andptr = ctxt.Andptr[len(naclbpfix):]
		}
	}
}