1// UNREVIEWED
2
3// Copyright 2021 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7package noder
8
9import (
10	"fmt"
11	"strings"
12)
13
14// enableSync controls whether sync markers are written into unified
15// IR's export data format and also whether they're expected when
16// reading them back in. They're inessential to the correct
17// functioning of unified IR, but are helpful during development to
18// detect mistakes.
19//
20// When sync is enabled, writer stack frames will also be included in
21// the export data. Currently, a fixed number of frames are included,
22// controlled by -d=syncframes (default 0).
23const enableSync = true
24
25// fmtFrames formats a backtrace for reporting reader/writer desyncs.
26func fmtFrames(pcs ...uintptr) []string {
27	res := make([]string, 0, len(pcs))
28	walkFrames(pcs, func(file string, line int, name string, offset uintptr) {
29		// Trim package from function name. It's just redundant noise.
30		name = strings.TrimPrefix(name, "cmd/compile/internal/noder.")
31
32		res = append(res, fmt.Sprintf("%s:%v: %s +0x%v", file, line, name, offset))
33	})
34	return res
35}
36
37type frameVisitor func(file string, line int, name string, offset uintptr)
38
39// syncMarker is an enum type that represents markers that may be
40// written to export data to ensure the reader and writer stay
41// synchronized.
42type syncMarker int
43
44//go:generate stringer -type=syncMarker -trimprefix=sync
45
46// TODO(mdempsky): Cleanup unneeded sync markers.
47
48// TODO(mdempsky): Split these markers into public/stable markers, and
49// private ones. Also, trim unused ones.
50const (
51	_ syncMarker = iota
52	syncNode
53	syncBool
54	syncInt64
55	syncUint64
56	syncString
57	syncPos
58	syncPkg
59	syncSym
60	syncSelector
61	syncKind
62	syncType
63	syncTypePkg
64	syncSignature
65	syncParam
66	syncOp
67	syncObject
68	syncExpr
69	syncStmt
70	syncDecl
71	syncConstDecl
72	syncFuncDecl
73	syncTypeDecl
74	syncVarDecl
75	syncPragma
76	syncValue
77	syncEOF
78	syncMethod
79	syncFuncBody
80	syncUse
81	syncUseObj
82	syncObjectIdx
83	syncTypeIdx
84	syncBOF
85	syncEntry
86	syncOpenScope
87	syncCloseScope
88	syncGlobal
89	syncLocal
90	syncDefine
91	syncDefLocal
92	syncUseLocal
93	syncDefGlobal
94	syncUseGlobal
95	syncTypeParams
96	syncUseLabel
97	syncDefLabel
98	syncFuncLit
99	syncCommonFunc
100	syncBodyRef
101	syncLinksymExt
102	syncHack
103	syncSetlineno
104	syncName
105	syncImportDecl
106	syncDeclNames
107	syncDeclName
108	syncExprList
109	syncExprs
110	syncWrapname
111	syncTypeExpr
112	syncTypeExprOrNil
113	syncChanDir
114	syncParams
115	syncCloseAnotherScope
116	syncSum
117	syncUnOp
118	syncBinOp
119	syncStructType
120	syncInterfaceType
121	syncPackname
122	syncEmbedded
123	syncStmts
124	syncStmtsFall
125	syncStmtFall
126	syncBlockStmt
127	syncIfStmt
128	syncForStmt
129	syncSwitchStmt
130	syncRangeStmt
131	syncCaseClause
132	syncCommClause
133	syncSelectStmt
134	syncDecls
135	syncLabeledStmt
136	syncCompLit
137
138	sync1
139	sync2
140	sync3
141	sync4
142
143	syncN
144	syncDefImplicit
145	syncUseName
146	syncUseObjLocal
147	syncAddLocal
148	syncBothSignature
149	syncSetUnderlying
150	syncLinkname
151	syncStmt1
152	syncStmtsEnd
153	syncDeclare
154	syncTopDecls
155	syncTopConstDecl
156	syncTopFuncDecl
157	syncTopTypeDecl
158	syncTopVarDecl
159	syncObject1
160	syncAddBody
161	syncLabel
162	syncFuncExt
163	syncMethExt
164	syncOptLabel
165	syncScalar
166	syncStmtDecls
167	syncDeclLocal
168	syncObjLocal
169	syncObjLocal1
170	syncDeclareLocal
171	syncPublic
172	syncPrivate
173	syncRelocs
174	syncReloc
175	syncUseReloc
176	syncVarExt
177	syncPkgDef
178	syncTypeExt
179	syncVal
180	syncCodeObj
181	syncPosBase
182	syncLocalIdent
183	syncTypeParamNames
184	syncTypeParamBounds
185	syncImplicitTypes
186	syncObjectName
187)
188