Skip to content

Commit 9736009

Browse files
committed
cmd/compile: replace Ctype switches with type switches
Instead of switching on Ctype (which internally uses a type switch) and then scattering lots of type assertions throughout the CTFOO case clauses, just use type switches directly on the underlying constant value. Passes toolstash/buildall. Change-Id: I9bc172cc67e5f391cddc15539907883b4010689e Reviewed-on: https://go-review.googlesource.com/22384 Run-TryBot: Matthew Dempsky <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 2d342fb commit 9736009

File tree

9 files changed

+146
-168
lines changed

9 files changed

+146
-168
lines changed

src/cmd/compile/internal/gc/const.go

Lines changed: 50 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -378,39 +378,39 @@ bad:
378378
}
379379

380380
func copyval(v Val) Val {
381-
switch v.Ctype() {
382-
case CTINT, CTRUNE:
381+
switch u := v.U.(type) {
382+
case *Mpint:
383383
i := new(Mpint)
384-
i.Set(v.U.(*Mpint))
385-
i.Rune = v.U.(*Mpint).Rune
384+
i.Set(u)
385+
i.Rune = u.Rune
386386
v.U = i
387387

388-
case CTFLT:
388+
case *Mpflt:
389389
f := newMpflt()
390-
f.Set(v.U.(*Mpflt))
390+
f.Set(u)
391391
v.U = f
392392

393-
case CTCPLX:
393+
case *Mpcplx:
394394
c := new(Mpcplx)
395-
c.Real.Set(&v.U.(*Mpcplx).Real)
396-
c.Imag.Set(&v.U.(*Mpcplx).Imag)
395+
c.Real.Set(&u.Real)
396+
c.Imag.Set(&u.Imag)
397397
v.U = c
398398
}
399399

400400
return v
401401
}
402402

403403
func tocplx(v Val) Val {
404-
switch v.Ctype() {
405-
case CTINT, CTRUNE:
404+
switch u := v.U.(type) {
405+
case *Mpint:
406406
c := new(Mpcplx)
407-
c.Real.SetInt(v.U.(*Mpint))
407+
c.Real.SetInt(u)
408408
c.Imag.SetFloat64(0.0)
409409
v.U = c
410410

411-
case CTFLT:
411+
case *Mpflt:
412412
c := new(Mpcplx)
413-
c.Real.Set(v.U.(*Mpflt))
413+
c.Real.Set(u)
414414
c.Imag.SetFloat64(0.0)
415415
v.U = c
416416
}
@@ -419,17 +419,17 @@ func tocplx(v Val) Val {
419419
}
420420

421421
func toflt(v Val) Val {
422-
switch v.Ctype() {
423-
case CTINT, CTRUNE:
422+
switch u := v.U.(type) {
423+
case *Mpint:
424424
f := newMpflt()
425-
f.SetInt(v.U.(*Mpint))
425+
f.SetInt(u)
426426
v.U = f
427427

428-
case CTCPLX:
428+
case *Mpcplx:
429429
f := newMpflt()
430-
f.Set(&v.U.(*Mpcplx).Real)
431-
if v.U.(*Mpcplx).Imag.CmpFloat64(0) != 0 {
432-
Yyerror("constant %v%vi truncated to real", Fconv(&v.U.(*Mpcplx).Real, FmtSharp), Fconv(&v.U.(*Mpcplx).Imag, FmtSharp|FmtSign))
430+
f.Set(&u.Real)
431+
if u.Imag.CmpFloat64(0) != 0 {
432+
Yyerror("constant %v%vi truncated to real", Fconv(&u.Real, FmtSharp), Fconv(&u.Imag, FmtSharp|FmtSign))
433433
}
434434
v.U = f
435435
}
@@ -438,31 +438,33 @@ func toflt(v Val) Val {
438438
}
439439

440440
func toint(v Val) Val {
441-
switch v.Ctype() {
442-
case CTRUNE:
443-
i := new(Mpint)
444-
i.Set(v.U.(*Mpint))
445-
v.U = i
441+
switch u := v.U.(type) {
442+
case *Mpint:
443+
if u.Rune {
444+
i := new(Mpint)
445+
i.Set(u)
446+
v.U = i
447+
}
446448

447-
case CTFLT:
449+
case *Mpflt:
448450
i := new(Mpint)
449-
if f := v.U.(*Mpflt); i.SetFloat(f) < 0 {
451+
if i.SetFloat(u) < 0 {
450452
msg := "constant %v truncated to integer"
451453
// provide better error message if SetFloat failed because f was too large
452-
if f.Val.IsInt() {
454+
if u.Val.IsInt() {
453455
msg = "constant %v overflows integer"
454456
}
455-
Yyerror(msg, Fconv(f, FmtSharp))
457+
Yyerror(msg, Fconv(u, FmtSharp))
456458
}
457459
v.U = i
458460

459-
case CTCPLX:
461+
case *Mpcplx:
460462
i := new(Mpint)
461-
if i.SetFloat(&v.U.(*Mpcplx).Real) < 0 {
462-
Yyerror("constant %v%vi truncated to integer", Fconv(&v.U.(*Mpcplx).Real, FmtSharp), Fconv(&v.U.(*Mpcplx).Imag, FmtSharp|FmtSign))
463+
if i.SetFloat(&u.Real) < 0 {
464+
Yyerror("constant %v%vi truncated to integer", Fconv(&u.Real, FmtSharp), Fconv(&u.Imag, FmtSharp|FmtSign))
463465
}
464-
if v.U.(*Mpcplx).Imag.CmpFloat64(0) != 0 {
465-
Yyerror("constant %v%vi truncated to real", Fconv(&v.U.(*Mpcplx).Real, FmtSharp), Fconv(&v.U.(*Mpcplx).Imag, FmtSharp|FmtSign))
466+
if u.Imag.CmpFloat64(0) != 0 {
467+
Yyerror("constant %v%vi truncated to real", Fconv(&u.Real, FmtSharp), Fconv(&u.Imag, FmtSharp|FmtSign))
466468
}
467469
v.U = i
468470
}
@@ -471,30 +473,25 @@ func toint(v Val) Val {
471473
}
472474

473475
func doesoverflow(v Val, t *Type) bool {
474-
switch v.Ctype() {
475-
case CTINT, CTRUNE:
476+
switch u := v.U.(type) {
477+
case *Mpint:
476478
if !t.IsInteger() {
477479
Fatalf("overflow: %v integer constant", t)
478480
}
479-
if v.U.(*Mpint).Cmp(Minintval[t.Etype]) < 0 || v.U.(*Mpint).Cmp(Maxintval[t.Etype]) > 0 {
480-
return true
481-
}
481+
return u.Cmp(Minintval[t.Etype]) < 0 || u.Cmp(Maxintval[t.Etype]) > 0
482482

483-
case CTFLT:
483+
case *Mpflt:
484484
if !t.IsFloat() {
485485
Fatalf("overflow: %v floating-point constant", t)
486486
}
487-
if v.U.(*Mpflt).Cmp(minfltval[t.Etype]) <= 0 || v.U.(*Mpflt).Cmp(maxfltval[t.Etype]) >= 0 {
488-
return true
489-
}
487+
return u.Cmp(minfltval[t.Etype]) <= 0 || u.Cmp(maxfltval[t.Etype]) >= 0
490488

491-
case CTCPLX:
489+
case *Mpcplx:
492490
if !t.IsComplex() {
493491
Fatalf("overflow: %v complex constant", t)
494492
}
495-
if v.U.(*Mpcplx).Real.Cmp(minfltval[t.Etype]) <= 0 || v.U.(*Mpcplx).Real.Cmp(maxfltval[t.Etype]) >= 0 || v.U.(*Mpcplx).Imag.Cmp(minfltval[t.Etype]) <= 0 || v.U.(*Mpcplx).Imag.Cmp(maxfltval[t.Etype]) >= 0 {
496-
return true
497-
}
493+
return u.Real.Cmp(minfltval[t.Etype]) <= 0 || u.Real.Cmp(maxfltval[t.Etype]) >= 0 ||
494+
u.Imag.Cmp(minfltval[t.Etype]) <= 0 || u.Imag.Cmp(maxfltval[t.Etype]) >= 0
498495
}
499496

500497
return false
@@ -518,21 +515,16 @@ func overflow(v Val, t *Type) {
518515
}
519516

520517
func tostr(v Val) Val {
521-
switch v.Ctype() {
522-
case CTINT, CTRUNE:
518+
switch u := v.U.(type) {
519+
case *Mpint:
523520
var i int64 = 0xFFFD
524-
if u := v.U.(*Mpint); u.Cmp(Minintval[TUINT32]) >= 0 && u.Cmp(Maxintval[TUINT32]) <= 0 {
521+
if u.Cmp(Minintval[TUINT32]) >= 0 && u.Cmp(Maxintval[TUINT32]) <= 0 {
525522
i = u.Int64()
526523
}
527-
v = Val{}
528524
v.U = string(i)
529525

530-
case CTFLT:
531-
Yyerror("no float -> string")
532-
fallthrough
533-
534-
case CTNIL:
535-
v = Val{}
526+
case *NilVal:
527+
// Can happen because of string([]byte(nil)).
536528
v.U = ""
537529
}
538530

src/cmd/compile/internal/gc/cplx.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ func subnode(nr *Node, ni *Node, nc *Node) {
8989
t := Types[tc]
9090

9191
if nc.Op == OLITERAL {
92-
nodfconst(nr, t, &nc.Val().U.(*Mpcplx).Real)
93-
nodfconst(ni, t, &nc.Val().U.(*Mpcplx).Imag)
92+
u := nc.Val().U.(*Mpcplx)
93+
nodfconst(nr, t, &u.Real)
94+
nodfconst(ni, t, &u.Imag)
9495
return
9596
}
9697

src/cmd/compile/internal/gc/dcl.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -755,17 +755,13 @@ func structfield(n *Node) *Field {
755755
f.Broke = true
756756
}
757757

758-
switch n.Val().Ctype() {
759-
case CTSTR:
760-
f.Note = new(string)
761-
*f.Note = n.Val().U.(string)
762-
758+
switch u := n.Val().U.(type) {
759+
case string:
760+
f.Note = &u
763761
default:
764762
Yyerror("field annotation must be string")
765-
fallthrough
766-
767-
case CTxxx:
768-
f.Note = nil
763+
case nil:
764+
// noop
769765
}
770766

771767
if n.Left != nil && n.Left.Op == ONAME {

src/cmd/compile/internal/gc/fmt.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,16 @@ func Jconv(n *Node, flag FmtFlag) string {
334334

335335
// Fmt "%V": Values
336336
func Vconv(v Val, flag FmtFlag) string {
337-
switch v.Ctype() {
338-
case CTINT:
339-
if (flag&FmtSharp != 0) || fmtmode == FExp {
340-
return Bconv(v.U.(*Mpint), FmtSharp)
337+
switch u := v.U.(type) {
338+
case *Mpint:
339+
if !u.Rune {
340+
if (flag&FmtSharp != 0) || fmtmode == FExp {
341+
return Bconv(u, FmtSharp)
342+
}
343+
return Bconv(u, 0)
341344
}
342-
return Bconv(v.U.(*Mpint), 0)
343345

344-
case CTRUNE:
345-
x := v.U.(*Mpint).Int64()
346+
x := u.Int64()
346347
if ' ' <= x && x < utf8.RuneSelf && x != '\\' && x != '\'' {
347348
return fmt.Sprintf("'%c'", int(x))
348349
}
@@ -352,39 +353,39 @@ func Vconv(v Val, flag FmtFlag) string {
352353
if 0 <= x && x <= utf8.MaxRune {
353354
return fmt.Sprintf("'\\U%08x'", uint64(x))
354355
}
355-
return fmt.Sprintf("('\\x00' + %v)", v.U.(*Mpint))
356+
return fmt.Sprintf("('\\x00' + %v)", u)
356357

357-
case CTFLT:
358+
case *Mpflt:
358359
if (flag&FmtSharp != 0) || fmtmode == FExp {
359-
return Fconv(v.U.(*Mpflt), 0)
360+
return Fconv(u, 0)
360361
}
361-
return Fconv(v.U.(*Mpflt), FmtSharp)
362+
return Fconv(u, FmtSharp)
362363

363-
case CTCPLX:
364+
case *Mpcplx:
364365
if (flag&FmtSharp != 0) || fmtmode == FExp {
365-
return fmt.Sprintf("(%v+%vi)", &v.U.(*Mpcplx).Real, &v.U.(*Mpcplx).Imag)
366+
return fmt.Sprintf("(%v+%vi)", &u.Real, &u.Imag)
366367
}
367368
if v.U.(*Mpcplx).Real.CmpFloat64(0) == 0 {
368-
return fmt.Sprintf("%vi", Fconv(&v.U.(*Mpcplx).Imag, FmtSharp))
369+
return fmt.Sprintf("%vi", Fconv(&u.Imag, FmtSharp))
369370
}
370371
if v.U.(*Mpcplx).Imag.CmpFloat64(0) == 0 {
371-
return Fconv(&v.U.(*Mpcplx).Real, FmtSharp)
372+
return Fconv(&u.Real, FmtSharp)
372373
}
373374
if v.U.(*Mpcplx).Imag.CmpFloat64(0) < 0 {
374-
return fmt.Sprintf("(%v%vi)", Fconv(&v.U.(*Mpcplx).Real, FmtSharp), Fconv(&v.U.(*Mpcplx).Imag, FmtSharp))
375+
return fmt.Sprintf("(%v%vi)", Fconv(&u.Real, FmtSharp), Fconv(&u.Imag, FmtSharp))
375376
}
376-
return fmt.Sprintf("(%v+%vi)", Fconv(&v.U.(*Mpcplx).Real, FmtSharp), Fconv(&v.U.(*Mpcplx).Imag, FmtSharp))
377+
return fmt.Sprintf("(%v+%vi)", Fconv(&u.Real, FmtSharp), Fconv(&u.Imag, FmtSharp))
377378

378-
case CTSTR:
379-
return strconv.Quote(v.U.(string))
379+
case string:
380+
return strconv.Quote(u)
380381

381-
case CTBOOL:
382-
if v.U.(bool) {
382+
case bool:
383+
if u {
383384
return "true"
384385
}
385386
return "false"
386387

387-
case CTNIL:
388+
case *NilVal:
388389
return "nil"
389390
}
390391

src/cmd/compile/internal/gc/gsubr.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -430,28 +430,28 @@ func Naddr(a *obj.Addr, n *Node) {
430430
if Thearch.LinkArch.Family == sys.I386 {
431431
a.Width = 0
432432
}
433-
switch n.Val().Ctype() {
433+
switch u := n.Val().U.(type) {
434434
default:
435435
Fatalf("naddr: const %v", Tconv(n.Type, FmtLong))
436436

437-
case CTFLT:
437+
case *Mpflt:
438438
a.Type = obj.TYPE_FCONST
439-
a.Val = n.Val().U.(*Mpflt).Float64()
439+
a.Val = u.Float64()
440440

441-
case CTINT, CTRUNE:
441+
case *Mpint:
442442
a.Sym = nil
443443
a.Type = obj.TYPE_CONST
444-
a.Offset = n.Int64()
444+
a.Offset = u.Int64()
445445

446-
case CTSTR:
447-
datagostring(n.Val().U.(string), a)
446+
case string:
447+
datagostring(u, a)
448448

449-
case CTBOOL:
449+
case bool:
450450
a.Sym = nil
451451
a.Type = obj.TYPE_CONST
452-
a.Offset = int64(obj.Bool2int(n.Val().U.(bool)))
452+
a.Offset = int64(obj.Bool2int(u))
453453

454-
case CTNIL:
454+
case *NilVal:
455455
a.Sym = nil
456456
a.Type = obj.TYPE_CONST
457457
a.Offset = 0

src/cmd/compile/internal/gc/obj.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,20 +342,23 @@ func gdata(nam *Node, nr *Node, wid int) {
342342

343343
switch nr.Op {
344344
case OLITERAL:
345-
switch nr.Val().Ctype() {
346-
case CTCPLX:
347-
gdatacomplex(nam, nr.Val().U.(*Mpcplx))
345+
switch u := nr.Val().U.(type) {
346+
case *Mpcplx:
347+
gdatacomplex(nam, u)
348348

349-
case CTSTR:
350-
gdatastring(nam, nr.Val().U.(string))
349+
case string:
350+
gdatastring(nam, u)
351351

352-
case CTINT, CTRUNE, CTBOOL:
353-
i, _ := nr.IntLiteral()
352+
case bool:
353+
i := int64(obj.Bool2int(u))
354354
Linksym(nam.Sym).WriteInt(Ctxt, nam.Xoffset, wid, i)
355355

356-
case CTFLT:
356+
case *Mpint:
357+
Linksym(nam.Sym).WriteInt(Ctxt, nam.Xoffset, wid, u.Int64())
358+
359+
case *Mpflt:
357360
s := Linksym(nam.Sym)
358-
f := nr.Val().U.(*Mpflt).Float64()
361+
f := u.Float64()
359362
switch nam.Type.Etype {
360363
case TFLOAT32:
361364
s.WriteFloat32(Ctxt, nam.Xoffset, float32(f))

0 commit comments

Comments
 (0)