| 1 |
// Copyright 2015 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// The gen command generates Go code (in the parent directory) for all |
| 6 |
// the architecture-specific opcodes, blocks, and rewrites. |
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"flag" |
| 12 |
"fmt" |
| 13 |
"go/format" |
| 14 |
"log" |
| 15 |
"math/bits" |
| 16 |
"os" |
| 17 |
"path" |
| 18 |
"regexp" |
| 19 |
"runtime" |
| 20 |
"runtime/pprof" |
| 21 |
"runtime/trace" |
| 22 |
"slices" |
| 23 |
"sort" |
| 24 |
"strings" |
| 25 |
"sync" |
| 26 |
) |
| 27 |
|
| 28 |
// TODO: capitalize these types, so that we can more easily tell variable names |
| 29 |
// apart from type names, and avoid awkward func parameters like "arch arch". |
| 30 |
|
| 31 |
type arch struct { |
| 32 |
name string |
| 33 |
pkg string // obj package to import for this arch. |
| 34 |
genfile string // source file containing opcode code generation. |
| 35 |
ops []opData |
| 36 |
blocks []blockData |
| 37 |
regnames []string |
| 38 |
ParamIntRegNames string |
| 39 |
ParamFloatRegNames string |
| 40 |
gpregmask regMask |
| 41 |
fpregmask regMask |
| 42 |
fp32regmask regMask |
| 43 |
fp64regmask regMask |
| 44 |
specialregmask regMask |
| 45 |
framepointerreg int8 |
| 46 |
linkreg int8 |
| 47 |
generic bool |
| 48 |
imports []string |
| 49 |
} |
| 50 |
|
| 51 |
type opData struct { |
| 52 |
name string |
| 53 |
reg regInfo |
| 54 |
asm string |
| 55 |
typ string // default result type |
| 56 |
aux string |
| 57 |
rematerializeable bool |
| 58 |
argLength int32 // number of arguments, if -1, then this operation has a variable number of arguments |
| 59 |
commutative bool // this operation is commutative on its first 2 arguments (e.g. addition) |
| 60 |
resultInArg0 bool // (first, if a tuple) output of v and v.Args[0] must be allocated to the same register |
| 61 |
resultNotInArgs bool // outputs must not be allocated to the same registers as inputs |
| 62 |
clobberFlags bool // this op clobbers flags register |
| 63 |
needIntTemp bool // need a temporary free integer register |
| 64 |
call bool // is a function call |
| 65 |
tailCall bool // is a tail call |
| 66 |
nilCheck bool // this op is a nil check on arg0 |
| 67 |
faultOnNilArg0 bool // this op will fault if arg0 is nil (and aux encodes a small offset) |
| 68 |
faultOnNilArg1 bool // this op will fault if arg1 is nil (and aux encodes a small offset) |
| 69 |
hasSideEffects bool // for "reasons", not to be eliminated. E.g., atomic store, #19182. |
| 70 |
zeroWidth bool // op never translates into any machine code. example: copy, which may sometimes translate to machine code, is not zero-width. |
| 71 |
unsafePoint bool // this op is an unsafe point, i.e. not safe for async preemption |
| 72 |
fixedReg bool // this op will be assigned a fixed register |
| 73 |
addrSinkArg0 bool // the address in arg0 does not propagate to the result |
| 74 |
addrSinkArg1 bool // the address in arg1 does not propagate to the result |
| 75 |
symEffect string // effect this op has on symbol in aux |
| 76 |
scale uint8 // amd64/386 indexed load scale |
| 77 |
} |
| 78 |
|
| 79 |
type blockData struct { |
| 80 |
name string // the suffix for this block ("EQ", "LT", etc.) |
| 81 |
controls int // the number of control values this type of block requires |
| 82 |
aux string // the type of the Aux/AuxInt value, if any |
| 83 |
} |
| 84 |
|
| 85 |
type regInfo struct { |
| 86 |
// inputs[i] encodes the set of registers allowed for the i'th input. |
| 87 |
// Inputs that don't use registers (flags, memory, etc.) should be 0. |
| 88 |
inputs []regMask |
| 89 |
// clobbers encodes the set of registers that are overwritten by |
| 90 |
// the instruction (other than the output registers). |
| 91 |
clobbers regMask |
| 92 |
// outputs[i] encodes the set of registers allowed for the i'th output. |
| 93 |
outputs []regMask |
| 94 |
} |
| 95 |
|
| 96 |
type regMask uint64 |
| 97 |
|
| 98 |
func (a arch) regMaskComment(r regMask) string { |
| 99 |
var buf strings.Builder |
| 100 |
for i := uint64(0); r != 0; i++ { |
| 101 |
if r&1 != 0 { |
| 102 |
if buf.Len() == 0 { |
| 103 |
buf.WriteString(" //") |
| 104 |
} |
| 105 |
buf.WriteString(" ") |
| 106 |
buf.WriteString(a.regnames[i]) |
| 107 |
} |
| 108 |
r >>= 1 |
| 109 |
} |
| 110 |
return buf.String() |
| 111 |
} |
| 112 |
|
| 113 |
var archs []arch |
| 114 |
|
| 115 |
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`") |
| 116 |
var memprofile = flag.String("memprofile", "", "write memory profile to `file`") |
| 117 |
var tracefile = flag.String("trace", "", "write trace to `file`") |
| 118 |
var outDir = flag.String("outdir", "..", "directory in which to write generated files") |
| 119 |
|
| 120 |
func main() { |
| 121 |
flag.Parse() |
| 122 |
if *cpuprofile != "" { |
| 123 |
f, err := os.Create(*cpuprofile) |
| 124 |
if err != nil { |
| 125 |
log.Fatal("could not create CPU profile: ", err) |
| 126 |
} |
| 127 |
defer f.Close() |
| 128 |
if err := pprof.StartCPUProfile(f); err != nil { |
| 129 |
log.Fatal("could not start CPU profile: ", err) |
| 130 |
} |
| 131 |
defer pprof.StopCPUProfile() |
| 132 |
} |
| 133 |
if *tracefile != "" { |
| 134 |
f, err := os.Create(*tracefile) |
| 135 |
if err != nil { |
| 136 |
log.Fatalf("failed to create trace output file: %v", err) |
| 137 |
} |
| 138 |
defer func() { |
| 139 |
if err := f.Close(); err != nil { |
| 140 |
log.Fatalf("failed to close trace file: %v", err) |
| 141 |
} |
| 142 |
}() |
| 143 |
|
| 144 |
if err := trace.Start(f); err != nil { |
| 145 |
log.Fatalf("failed to start trace: %v", err) |
| 146 |
} |
| 147 |
defer trace.Stop() |
| 148 |
} |
| 149 |
|
| 150 |
if *outDir != ".." { |
| 151 |
err := os.MkdirAll(*outDir, 0755) |
| 152 |
if err != nil { |
| 153 |
log.Fatalf("failed to create output directory: %v", err) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
slices.SortFunc(archs, func(a, b arch) int { |
| 158 |
return strings.Compare(a.name, b.name) |
| 159 |
}) |
| 160 |
|
| 161 |
// The generate tasks are run concurrently, since they are CPU-intensive |
| 162 |
// that can easily make use of many cores on a machine. |
| 163 |
// |
| 164 |
// Note that there is no limit on the concurrency at the moment. On a |
| 165 |
// four-core laptop at the time of writing, peak RSS usually reaches |
| 166 |
// ~200MiB, which seems doable by practically any machine nowadays. If |
| 167 |
// that stops being the case, we can cap this func to a fixed number of |
| 168 |
// architectures being generated at once. |
| 169 |
|
| 170 |
tasks := []func(){ |
| 171 |
genOp, |
| 172 |
genAllocators, |
| 173 |
} |
| 174 |
for _, a := range archs { |
| 175 |
a := a // the funcs are ran concurrently at a later time |
| 176 |
tasks = append(tasks, func() { |
| 177 |
genRules(a) |
| 178 |
genSplitLoadRules(a) |
| 179 |
genLateLowerRules(a) |
| 180 |
}) |
| 181 |
} |
| 182 |
var wg sync.WaitGroup |
| 183 |
for _, task := range tasks { |
| 184 |
task := task |
| 185 |
wg.Add(1) |
| 186 |
go func() { |
| 187 |
task() |
| 188 |
wg.Done() |
| 189 |
}() |
| 190 |
} |
| 191 |
wg.Wait() |
| 192 |
|
| 193 |
if *memprofile != "" { |
| 194 |
f, err := os.Create(*memprofile) |
| 195 |
if err != nil { |
| 196 |
log.Fatal("could not create memory profile: ", err) |
| 197 |
} |
| 198 |
defer f.Close() |
| 199 |
runtime.GC() // get up-to-date statistics |
| 200 |
if err := pprof.WriteHeapProfile(f); err != nil { |
| 201 |
log.Fatal("could not write memory profile: ", err) |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func outFile(file string) string { |
| 207 |
return *outDir + "/" + file |
| 208 |
} |
| 209 |
|
| 210 |
func genOp() { |
| 211 |
w := new(bytes.Buffer) |
| 212 |
fmt.Fprintf(w, "// Code generated from _gen/*Ops.go using 'go generate'; DO NOT EDIT.\n") |
| 213 |
fmt.Fprintln(w) |
| 214 |
fmt.Fprintln(w, "package ssa") |
| 215 |
|
| 216 |
fmt.Fprintln(w, "import (") |
| 217 |
fmt.Fprintln(w, "\"cmd/internal/obj\"") |
| 218 |
for _, a := range archs { |
| 219 |
if a.pkg != "" { |
| 220 |
fmt.Fprintf(w, "%q\n", a.pkg) |
| 221 |
} |
| 222 |
} |
| 223 |
fmt.Fprintln(w, ")") |
| 224 |
|
| 225 |
// generate Block* declarations |
| 226 |
fmt.Fprintln(w, "const (") |
| 227 |
fmt.Fprintln(w, "BlockInvalid BlockKind = iota") |
| 228 |
for _, a := range archs { |
| 229 |
fmt.Fprintln(w) |
| 230 |
for _, d := range a.blocks { |
| 231 |
fmt.Fprintf(w, "Block%s%s\n", a.Name(), d.name) |
| 232 |
} |
| 233 |
} |
| 234 |
fmt.Fprintln(w, ")") |
| 235 |
|
| 236 |
// generate block kind string method |
| 237 |
fmt.Fprintln(w, "var blockString = [...]string{") |
| 238 |
fmt.Fprintln(w, "BlockInvalid:\"BlockInvalid\",") |
| 239 |
for _, a := range archs { |
| 240 |
fmt.Fprintln(w) |
| 241 |
for _, b := range a.blocks { |
| 242 |
fmt.Fprintf(w, "Block%s%s:\"%s\",\n", a.Name(), b.name, b.name) |
| 243 |
} |
| 244 |
} |
| 245 |
fmt.Fprintln(w, "}") |
| 246 |
fmt.Fprintln(w, "func (k BlockKind) String() string {return blockString[k]}") |
| 247 |
|
| 248 |
// generate block kind auxint method |
| 249 |
fmt.Fprintln(w, "func (k BlockKind) AuxIntType() string {") |
| 250 |
fmt.Fprintln(w, "switch k {") |
| 251 |
for _, a := range archs { |
| 252 |
for _, b := range a.blocks { |
| 253 |
if b.auxIntType() == "invalid" { |
| 254 |
continue |
| 255 |
} |
| 256 |
fmt.Fprintf(w, "case Block%s%s: return \"%s\"\n", a.Name(), b.name, b.auxIntType()) |
| 257 |
} |
| 258 |
} |
| 259 |
fmt.Fprintln(w, "}") |
| 260 |
fmt.Fprintln(w, "return \"\"") |
| 261 |
fmt.Fprintln(w, "}") |
| 262 |
|
| 263 |
// generate Op* declarations |
| 264 |
fmt.Fprintln(w, "const (") |
| 265 |
fmt.Fprintln(w, "OpInvalid Op = iota") // make sure OpInvalid is 0. |
| 266 |
for _, a := range archs { |
| 267 |
fmt.Fprintln(w) |
| 268 |
for _, v := range a.ops { |
| 269 |
if v.name == "Invalid" { |
| 270 |
continue |
| 271 |
} |
| 272 |
fmt.Fprintf(w, "Op%s%s\n", a.Name(), v.name) |
| 273 |
} |
| 274 |
} |
| 275 |
fmt.Fprintln(w, ")") |
| 276 |
|
| 277 |
// generate OpInfo table |
| 278 |
fmt.Fprintln(w, "var opcodeTable = [...]opInfo{") |
| 279 |
fmt.Fprintln(w, " { name: \"OpInvalid\" },") |
| 280 |
for _, a := range archs { |
| 281 |
fmt.Fprintln(w) |
| 282 |
|
| 283 |
pkg := path.Base(a.pkg) |
| 284 |
for _, v := range a.ops { |
| 285 |
if v.name == "Invalid" { |
| 286 |
continue |
| 287 |
} |
| 288 |
fmt.Fprintln(w, "{") |
| 289 |
fmt.Fprintf(w, "name:\"%s\",\n", v.name) |
| 290 |
|
| 291 |
// flags |
| 292 |
if v.aux != "" { |
| 293 |
fmt.Fprintf(w, "auxType: aux%s,\n", v.aux) |
| 294 |
} |
| 295 |
fmt.Fprintf(w, "argLen: %d,\n", v.argLength) |
| 296 |
|
| 297 |
if v.rematerializeable { |
| 298 |
if v.reg.clobbers != 0 { |
| 299 |
log.Fatalf("%s is rematerializeable and clobbers registers", v.name) |
| 300 |
} |
| 301 |
if v.clobberFlags { |
| 302 |
log.Fatalf("%s is rematerializeable and clobbers flags", v.name) |
| 303 |
} |
| 304 |
fmt.Fprintln(w, "rematerializeable: true,") |
| 305 |
} |
| 306 |
if v.commutative { |
| 307 |
fmt.Fprintln(w, "commutative: true,") |
| 308 |
} |
| 309 |
if v.resultInArg0 { |
| 310 |
fmt.Fprintln(w, "resultInArg0: true,") |
| 311 |
// OpConvert's register mask is selected dynamically, |
| 312 |
// so don't try to check it in the static table. |
| 313 |
if v.name != "Convert" && v.reg.inputs[0] != v.reg.outputs[0] { |
| 314 |
log.Fatalf("%s: input[0] and output[0] must use the same registers for %s", a.name, v.name) |
| 315 |
} |
| 316 |
if v.name != "Convert" && v.commutative && v.reg.inputs[1] != v.reg.outputs[0] { |
| 317 |
log.Fatalf("%s: input[1] and output[0] must use the same registers for %s", a.name, v.name) |
| 318 |
} |
| 319 |
} |
| 320 |
if v.resultNotInArgs { |
| 321 |
fmt.Fprintln(w, "resultNotInArgs: true,") |
| 322 |
} |
| 323 |
if v.clobberFlags { |
| 324 |
fmt.Fprintln(w, "clobberFlags: true,") |
| 325 |
} |
| 326 |
if v.needIntTemp { |
| 327 |
fmt.Fprintln(w, "needIntTemp: true,") |
| 328 |
} |
| 329 |
if v.call { |
| 330 |
fmt.Fprintln(w, "call: true,") |
| 331 |
} |
| 332 |
if v.tailCall { |
| 333 |
fmt.Fprintln(w, "tailCall: true,") |
| 334 |
} |
| 335 |
if v.nilCheck { |
| 336 |
fmt.Fprintln(w, "nilCheck: true,") |
| 337 |
} |
| 338 |
if v.faultOnNilArg0 { |
| 339 |
fmt.Fprintln(w, "faultOnNilArg0: true,") |
| 340 |
if v.aux != "Sym" && v.aux != "SymOff" && v.aux != "SymValAndOff" && v.aux != "Int64" && v.aux != "Int32" && v.aux != "" { |
| 341 |
log.Fatalf("faultOnNilArg0 with aux %s not allowed", v.aux) |
| 342 |
} |
| 343 |
} |
| 344 |
if v.faultOnNilArg1 { |
| 345 |
fmt.Fprintln(w, "faultOnNilArg1: true,") |
| 346 |
if v.aux != "Sym" && v.aux != "SymOff" && v.aux != "SymValAndOff" && v.aux != "Int64" && v.aux != "Int32" && v.aux != "" { |
| 347 |
log.Fatalf("faultOnNilArg1 with aux %s not allowed", v.aux) |
| 348 |
} |
| 349 |
} |
| 350 |
if v.hasSideEffects { |
| 351 |
fmt.Fprintln(w, "hasSideEffects: true,") |
| 352 |
} |
| 353 |
if v.zeroWidth { |
| 354 |
fmt.Fprintln(w, "zeroWidth: true,") |
| 355 |
} |
| 356 |
if v.fixedReg { |
| 357 |
fmt.Fprintln(w, "fixedReg: true,") |
| 358 |
} |
| 359 |
if v.addrSinkArg0 { |
| 360 |
fmt.Fprintln(w, "addrSinkArg0: true,") |
| 361 |
} |
| 362 |
if v.addrSinkArg1 { |
| 363 |
fmt.Fprintln(w, "addrSinkArg1: true,") |
| 364 |
} |
| 365 |
if v.unsafePoint { |
| 366 |
fmt.Fprintln(w, "unsafePoint: true,") |
| 367 |
} |
| 368 |
needEffect := strings.HasPrefix(v.aux, "Sym") |
| 369 |
if v.symEffect != "" { |
| 370 |
if !needEffect { |
| 371 |
log.Fatalf("symEffect with aux %s not allowed", v.aux) |
| 372 |
} |
| 373 |
fmt.Fprintf(w, "symEffect: Sym%s,\n", strings.ReplaceAll(v.symEffect, ",", "|Sym")) |
| 374 |
} else if needEffect { |
| 375 |
log.Fatalf("symEffect needed for aux %s", v.aux) |
| 376 |
} |
| 377 |
if a.name == "generic" { |
| 378 |
fmt.Fprintln(w, "generic:true,") |
| 379 |
fmt.Fprintln(w, "},") // close op |
| 380 |
// generic ops have no reg info or asm |
| 381 |
continue |
| 382 |
} |
| 383 |
if v.asm != "" { |
| 384 |
fmt.Fprintf(w, "asm: %s.A%s,\n", pkg, v.asm) |
| 385 |
} |
| 386 |
if v.scale != 0 { |
| 387 |
fmt.Fprintf(w, "scale: %d,\n", v.scale) |
| 388 |
} |
| 389 |
fmt.Fprintln(w, "reg:regInfo{") |
| 390 |
|
| 391 |
// Compute input allocation order. We allocate from the |
| 392 |
// most to the least constrained input. This order guarantees |
| 393 |
// that we will always be able to find a register. |
| 394 |
var s []intPair |
| 395 |
for i, r := range v.reg.inputs { |
| 396 |
if r != 0 { |
| 397 |
s = append(s, intPair{countRegs(r), i}) |
| 398 |
} |
| 399 |
} |
| 400 |
if len(s) > 0 { |
| 401 |
sort.Sort(byKey(s)) |
| 402 |
fmt.Fprintln(w, "inputs: []inputInfo{") |
| 403 |
for _, p := range s { |
| 404 |
r := v.reg.inputs[p.val] |
| 405 |
fmt.Fprintf(w, "{%d,%d},%s\n", p.val, r, a.regMaskComment(r)) |
| 406 |
} |
| 407 |
fmt.Fprintln(w, "},") |
| 408 |
} |
| 409 |
|
| 410 |
if v.reg.clobbers > 0 { |
| 411 |
fmt.Fprintf(w, "clobbers: %d,%s\n", v.reg.clobbers, a.regMaskComment(v.reg.clobbers)) |
| 412 |
} |
| 413 |
|
| 414 |
// reg outputs |
| 415 |
s = s[:0] |
| 416 |
for i, r := range v.reg.outputs { |
| 417 |
s = append(s, intPair{countRegs(r), i}) |
| 418 |
} |
| 419 |
if len(s) > 0 { |
| 420 |
sort.Sort(byKey(s)) |
| 421 |
fmt.Fprintln(w, "outputs: []outputInfo{") |
| 422 |
for _, p := range s { |
| 423 |
r := v.reg.outputs[p.val] |
| 424 |
fmt.Fprintf(w, "{%d,%d},%s\n", p.val, r, a.regMaskComment(r)) |
| 425 |
} |
| 426 |
fmt.Fprintln(w, "},") |
| 427 |
} |
| 428 |
fmt.Fprintln(w, "},") // close reg info |
| 429 |
fmt.Fprintln(w, "},") // close op |
| 430 |
} |
| 431 |
} |
| 432 |
fmt.Fprintln(w, "}") |
| 433 |
|
| 434 |
fmt.Fprintln(w, "func (o Op) Asm() obj.As {return opcodeTable[o].asm}") |
| 435 |
fmt.Fprintln(w, "func (o Op) Scale() int16 {return int16(opcodeTable[o].scale)}") |
| 436 |
|
| 437 |
// generate op string method |
| 438 |
fmt.Fprintln(w, "func (o Op) String() string {return opcodeTable[o].name }") |
| 439 |
|
| 440 |
fmt.Fprintln(w, "func (o Op) SymEffect() SymEffect { return opcodeTable[o].symEffect }") |
| 441 |
fmt.Fprintln(w, "func (o Op) IsCall() bool { return opcodeTable[o].call }") |
| 442 |
fmt.Fprintln(w, "func (o Op) IsTailCall() bool { return opcodeTable[o].tailCall }") |
| 443 |
fmt.Fprintln(w, "func (o Op) HasSideEffects() bool { return opcodeTable[o].hasSideEffects }") |
| 444 |
fmt.Fprintln(w, "func (o Op) UnsafePoint() bool { return opcodeTable[o].unsafePoint }") |
| 445 |
fmt.Fprintln(w, "func (o Op) ResultInArg0() bool { return opcodeTable[o].resultInArg0 }") |
| 446 |
|
| 447 |
// generate registers |
| 448 |
for _, a := range archs { |
| 449 |
if a.generic { |
| 450 |
continue |
| 451 |
} |
| 452 |
fmt.Fprintf(w, "var registers%s = [...]Register {\n", a.name) |
| 453 |
num := map[string]int8{} |
| 454 |
for i, r := range a.regnames { |
| 455 |
num[r] = int8(i) |
| 456 |
pkg := a.pkg[len("cmd/internal/obj/"):] |
| 457 |
var objname string // name in cmd/internal/obj/$ARCH |
| 458 |
switch r { |
| 459 |
case "SB": |
| 460 |
// SB isn't a real register. cmd/internal/obj expects 0 in this case. |
| 461 |
objname = "0" |
| 462 |
case "SP": |
| 463 |
objname = pkg + ".REGSP" |
| 464 |
case "g": |
| 465 |
objname = pkg + ".REGG" |
| 466 |
case "ZERO": |
| 467 |
objname = pkg + ".REGZERO" |
| 468 |
default: |
| 469 |
objname = pkg + ".REG_" + r |
| 470 |
} |
| 471 |
fmt.Fprintf(w, " {%d, %s, \"%s\"},\n", i, objname, r) |
| 472 |
} |
| 473 |
parameterRegisterList := func(paramNamesString string) []int8 { |
| 474 |
paramNamesString = strings.TrimSpace(paramNamesString) |
| 475 |
if paramNamesString == "" { |
| 476 |
return nil |
| 477 |
} |
| 478 |
paramNames := strings.Split(paramNamesString, " ") |
| 479 |
var paramRegs []int8 |
| 480 |
for _, regName := range paramNames { |
| 481 |
if regName == "" { |
| 482 |
// forgive extra spaces |
| 483 |
continue |
| 484 |
} |
| 485 |
if regNum, ok := num[regName]; ok { |
| 486 |
paramRegs = append(paramRegs, regNum) |
| 487 |
delete(num, regName) |
| 488 |
} else { |
| 489 |
log.Fatalf("parameter register %s for architecture %s not a register name (or repeated in parameter list)", regName, a.name) |
| 490 |
} |
| 491 |
} |
| 492 |
return paramRegs |
| 493 |
} |
| 494 |
|
| 495 |
paramIntRegs := parameterRegisterList(a.ParamIntRegNames) |
| 496 |
paramFloatRegs := parameterRegisterList(a.ParamFloatRegNames) |
| 497 |
|
| 498 |
fmt.Fprintln(w, "}") |
| 499 |
fmt.Fprintf(w, "var paramIntReg%s = %#v\n", a.name, paramIntRegs) |
| 500 |
fmt.Fprintf(w, "var paramFloatReg%s = %#v\n", a.name, paramFloatRegs) |
| 501 |
fmt.Fprintf(w, "var gpRegMask%s = regMask(%d)\n", a.name, a.gpregmask) |
| 502 |
fmt.Fprintf(w, "var fpRegMask%s = regMask(%d)\n", a.name, a.fpregmask) |
| 503 |
if a.fp32regmask != 0 { |
| 504 |
fmt.Fprintf(w, "var fp32RegMask%s = regMask(%d)\n", a.name, a.fp32regmask) |
| 505 |
} |
| 506 |
if a.fp64regmask != 0 { |
| 507 |
fmt.Fprintf(w, "var fp64RegMask%s = regMask(%d)\n", a.name, a.fp64regmask) |
| 508 |
} |
| 509 |
fmt.Fprintf(w, "var specialRegMask%s = regMask(%d)\n", a.name, a.specialregmask) |
| 510 |
fmt.Fprintf(w, "var framepointerReg%s = int8(%d)\n", a.name, a.framepointerreg) |
| 511 |
fmt.Fprintf(w, "var linkReg%s = int8(%d)\n", a.name, a.linkreg) |
| 512 |
} |
| 513 |
|
| 514 |
// gofmt result |
| 515 |
b := w.Bytes() |
| 516 |
var err error |
| 517 |
b, err = format.Source(b) |
| 518 |
if err != nil { |
| 519 |
fmt.Printf("%s\n", w.Bytes()) |
| 520 |
panic(err) |
| 521 |
} |
| 522 |
|
| 523 |
if err := os.WriteFile(outFile("opGen.go"), b, 0666); err != nil { |
| 524 |
log.Fatalf("can't write output: %v\n", err) |
| 525 |
} |
| 526 |
|
| 527 |
// Check that the arch genfile handles all the arch-specific opcodes. |
| 528 |
// This is very much a hack, but it is better than nothing. |
| 529 |
// |
| 530 |
// Do a single regexp pass to record all ops being handled in a map, and |
| 531 |
// then compare that with the ops list. This is much faster than one |
| 532 |
// regexp pass per opcode. |
| 533 |
for _, a := range archs { |
| 534 |
if a.genfile == "" { |
| 535 |
continue |
| 536 |
} |
| 537 |
|
| 538 |
pattern := fmt.Sprintf(`\Wssa\.Op%s([a-zA-Z0-9_]+)\W`, a.name) |
| 539 |
rxOp, err := regexp.Compile(pattern) |
| 540 |
if err != nil { |
| 541 |
log.Fatalf("bad opcode regexp %s: %v", pattern, err) |
| 542 |
} |
| 543 |
|
| 544 |
src, err := os.ReadFile(a.genfile) |
| 545 |
if err != nil { |
| 546 |
log.Fatalf("can't read %s: %v", a.genfile, err) |
| 547 |
} |
| 548 |
seen := make(map[string]bool, len(a.ops)) |
| 549 |
for _, m := range rxOp.FindAllSubmatch(src, -1) { |
| 550 |
seen[string(m[1])] = true |
| 551 |
} |
| 552 |
for _, op := range a.ops { |
| 553 |
if !seen[op.name] { |
| 554 |
log.Fatalf("Op%s%s has no code generation in %s", a.name, op.name, a.genfile) |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
// Name returns the name of the architecture for use in Op* and Block* enumerations. |
| 561 |
func (a arch) Name() string { |
| 562 |
s := a.name |
| 563 |
if s == "generic" { |
| 564 |
s = "" |
| 565 |
} |
| 566 |
return s |
| 567 |
} |
| 568 |
|
| 569 |
// countRegs returns the number of set bits in the register mask. |
| 570 |
func countRegs(r regMask) int { |
| 571 |
return bits.OnesCount64(uint64(r)) |
| 572 |
} |
| 573 |
|
| 574 |
// for sorting a pair of integers by key |
| 575 |
type intPair struct { |
| 576 |
key, val int |
| 577 |
} |
| 578 |
type byKey []intPair |
| 579 |
|
| 580 |
func (a byKey) Len() int { return len(a) } |
| 581 |
func (a byKey) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 582 |
func (a byKey) Less(i, j int) bool { return a[i].key < a[j].key } |