1 package main
2 3 import (
4 "moxielang.dev/pkg/mxutil"
5 "runtime"
6 "unsafe"
7 )
8 9 // compilerState is the compiler's mutable process state that is not already a
10 // field of a receiver: a recursion guard, the deferred interface-assignment
11 // queue, the coverage site table, and the repository locks this process holds.
12 //
13 // It is one self-mutating type behind one package-level pointer for a reason
14 // that is now enforced rather than exempted: a store to a package-level
15 // variable outside an init-named function is a language error, because two
16 // domains forked from one address space must not see different values of a
17 // global. The compiler is single-domain, so the rule buys it nothing - but the
18 // rule is the rule, and the compliant shape for mutable state is a receiver
19 // whose methods own the writes. Every one of these was a plain global before
20 // (fromTailDepth, pendingIfaceAssigns, coverNames/coverCount, lastHolder,
21 // heldRepoLocks) and each was a "package globals are immutable outside init"
22 // diagnostic once `_mxc_stage4` stopped being exempt from its own rules.
23 //
24 // Reads stay direct (`cstate.coverCount`) - reading a global is legal; only
25 // stores had to move behind a method.
26 type compilerState struct {
27 tailDepth int32
28 pendingIface []ifaceAssign
29 coverNames []string
30 coverCount int32
31 lastHolder string
32 heldRepoLocks []string
33 }
34 35 // cstate is assigned once, by initCompilerState, which is init-named and may
36 // therefore store a global.
37 var cstate *compilerState
38 39 func initCompilerState() {
40 cstate = &compilerState{}
41 }
42 43 // enterTail guards typeFromTail against a recursive type whose Tail names
44 // itself. It reports whether the walk is too deep, having already undone its
45 // own increment so the caller does not have to.
46 func (s *compilerState) enterTail() (tooDeep bool) {
47 s.tailDepth = s.tailDepth + 1
48 if s.tailDepth > 40 {
49 s.tailDepth = s.tailDepth - 1
50 return true
51 }
52 return false
53 }
54 55 func (s *compilerState) leaveTail() {
56 s.tailDepth = s.tailDepth - 1
57 }
58 59 // resetCover clears the coverage site table between builds.
60 func (s *compilerState) resetCover() {
61 s.coverCount = 0
62 }
63 64 // addCover records one coverage site name in the root arena and returns its
65 // id. The table must live in the root arena: each name is built in a package's
66 // transient arena and read again after every package has been emitted, so a
67 // frame-arena string dangles by then.
68 func (s *compilerState) addCover(name string) (id int32) {
69 a := runtime.RootArena()
70 if a == nil || s.coverCount >= maxCoverSites {
71 return -1
72 }
73 if s.coverNames == nil {
74 s.coverNames = unsafe.Slice((*string)(unsafe.Pointer(runtime.ArenaAlloc(a, uintptr(maxCoverSites)*unsafe.Sizeof("")))), maxCoverSites)
75 }
76 id = s.coverCount
77 s.coverNames[id] = rootCopyString(a, name)
78 s.coverCount++
79 return id
80 }
81 82 // noteLockHolder records the pid named in a lock file this process lost to, so
83 // the wait loop can decide whether the holder is still alive.
84 func (s *compilerState) noteLockHolder(pid string) {
85 s.lastHolder = pid
86 }
87 88 // holdRepoLock records one acquired repository lock.
89 func (s *compilerState) holdRepoLock(mpath, repo string) {
90 s.heldRepoLocks = mxutil.Ensure(s.heldRepoLocks, 1)
91 s.heldRepoLocks = push(s.heldRepoLocks, mpath | "\x00" | repo)
92 }
93 94 // releaseHeldLocks forgets the locks this process holds; the files themselves
95 // are unlinked by the caller.
96 func (s *compilerState) releaseHeldLocks() {
97 s.heldRepoLocks = nil
98 }
99