// Package mxsrc holds the byte-level source rewrites shared by the compiler // and the distillation tools. Moving them here is the first step of the package // layout refactor: there is one implementation of the make-literal lowering, not // one in the compiler and another in a rewriter. package mxsrc import ( "moxielang.dev/pkg/mxutil" ) // JoinChunks concatenates byte slices in one allocation. func JoinChunks(chunks [][]byte) (r []byte) { n := int32(0) for _, c := range chunks { n += len(c) } out := []byte{:n} off := int32(0) for _, c := range chunks { for j := int32(0); j < len(c); j++ { out[off+j] = c[j] } off += len(c) } return out } // LIndexByte returns the first index of c in b, or -1. func LIndexByte(b []byte, c byte) (n int32) { for i := int32(0); i < int32(len(b)); i++ { if b[i] == c { return i } } return -1 } // LIndexFrom returns the first absolute index at or after `from` of sep in s, // or -1. A sub-slice is a COPY in Moxie, so the rewrite loops that called // `LIndex(src[i:], ...)` copied the whole remaining source on every iteration. // The scans themselves are linear, so this is an efficiency fix, not the pass's // quadratic term (measured: 1.847 GB -> 1.832 GB on the `runtime` package). func LIndexFrom(s []byte, from int32, sep []byte) (n int32) { n = len(sep) if n == 0 { return from } if from < 0 { from = 0 } if from > int32(len(s)) || n > int32(len(s))-from { return -1 } c0 := sep[0] for i := from; i <= int32(len(s))-n; i++ { if s[i] != c0 { continue } hit := true for j := int32(1); j < n; j++ { if s[i+j] != sep[j] { hit = false break } } if hit { return i } } return -1 } // LIndex returns the first index of sep in s, or -1. func LIndex(s, sep []byte) (n int32) { n = len(sep) if n == 0 { return 0 } if n > len(s) { return -1 } c0 := sep[0] for i := int32(0); i <= int32(len(s))-int32(n); i++ { if s[i] == c0 { match := true for j := int32(1); j < int32(n); j++ { if s[i+j] != sep[j] { match = false break } } if match { return i } } } return -1 } // LTrimSpace trims leading and trailing ASCII whitespace. func LTrimSpace(s []byte) (buf []byte) { start := int32(0) for start < int32(len(s)) && (s[start] == ' ' || s[start] == '\t' || s[start] == '\n' || s[start] == '\r') { start++ } stop := int32(len(s)) for stop > start && (s[stop-1] == ' ' || s[stop-1] == '\t' || s[stop-1] == '\n' || s[stop-1] == '\r') { stop-- } return s[start:stop] } // LIndexByteFrom returns the first index of c at or after `from`, or -1. The // caller otherwise has to sub-slice, and a sub-slice is a COPY in Moxie. func LIndexByteFrom(s []byte, from int32, c byte) (n int32) { if from < 0 { from = 0 } for i := from; i < int32(len(s)); i++ { if s[i] == c { return i } } return -1 } // trimRange trims ASCII whitespace from src[from:to] and returns the bounds, so // trimming costs two indexes instead of a copied sub-slice. func trimRange(src []byte, from, to int32) (a, b int32) { a = from for a < to && (src[a] == ' ' || src[a] == '\t' || src[a] == '\n' || src[a] == '\r') { a++ } b = to for b > a && (src[b-1] == ' ' || src[b-1] == '\t' || src[b-1] == '\n' || src[b-1] == '\r') { b-- } return a, b } // sliceLiteralScan resolves the `{:` at `start` to its match. ok is false when // the construct is not a slice literal, in which case the caller emits the two // bytes and continues. func sliceLiteralScan(src []byte, start int32) (lbrack, closeIdx, colonIdx int32, ok bool) { lbrack = findSliceTypeStart(src, start) if lbrack < 0 { return -1, -1, -1, false } closeIdx = findMatchingBrace(src, start) if closeIdx < 0 { return -1, -1, -1, false } colonIdx = LIndexByteFrom(src, start+2, ':') if colonIdx >= closeIdx { colonIdx = -1 } return lbrack, closeIdx, colonIdx, true } // putStr copies s into out at o and returns the next cursor position. func putStr(out []byte, o int32, s string) (n int32) { for i := int32(0); i < int32(len(s)); i++ { out[o] = s[i] o++ } return o } // putRange copies src[from:to] into out at o and returns the next cursor. func putRange(out []byte, o int32, src []byte, from, to int32) (n int32) { for i := from; i < to; i++ { out[o] = src[i] o++ } return o } // emitSliceLiteral writes one rewritten literal into out at o. func emitSliceLiteral(out []byte, o int32, src []byte, lbrack, start, closeIdx, colonIdx int32) (n int32) { a, b := trimRange(src, start+2, closeIdx) a2 := int32(0) b2 := int32(0) if colonIdx >= 0 { a, b = trimRange(src, start+2, colonIdx) a2, b2 = trimRange(src, colonIdx+1, closeIdx) } o = putStr(out, o, "__slicealloc(") o = putRange(out, o, src, lbrack, start) o = putStr(out, o, ", ") o = putRange(out, o, src, a, b) if colonIdx >= 0 { o = putStr(out, o, ", ") o = putRange(out, o, src, a2, b2) } return putStr(out, o, ")") } // trimmedLen is the length of src[from:to] with ASCII whitespace trimmed. func trimmedLen(src []byte, from, to int32) (n int32) { a, b := trimRange(src, from, to) return b - a } // sliceLiteralOutLen is the number of bytes emitSliceLiteral writes for the // same match, without writing them. func sliceLiteralOutLen(src []byte, lbrack, start, closeIdx, colonIdx int32) (n int32) { n = (start - lbrack) + 16 if colonIdx < 0 { return n + trimmedLen(src, start+2, closeIdx) } n += trimmedLen(src, start+2, colonIdx) + 2 return n + trimmedLen(src, colonIdx+1, closeIdx) } // RewriteSliceMakeLiterals lowers `[]T{:n}` and `[]T{:a:b}` to __slicealloc. // // It scans twice and writes once. The obvious shape - collect the segments in a // [][]byte and JoinChunks them - is quadratic here, and not because of the // scans: every `chunks = mxutil.Ensure(chunks, 1)` RETURNS a pointer-containing // slice across a function boundary, so the return protocol walks and copies // every segment accumulated so far, once per push. Measured on `runtime` // (519 KB of source): 32 MB -> 1.92 GB of arena inside this function, and // `io` (32 KB): 1.5 MB -> 35 MB. // // The output length is exactly computable from a scan that allocates nothing, // so pass one totals it, pass two writes through a cursor into one allocation. func RewriteSliceMakeLiterals(src []byte) (buf []byte) { bc := []byte("{:") n := int32(len(src)) outLen := n matches := int32(0) i := int32(0) for i < n { start := LIndexFrom(src, i, bc) if start < 0 { break } lbrack, closeIdx, colonIdx, ok := sliceLiteralScan(src, start) if !ok { i = start + 2 continue } outLen += sliceLiteralOutLen(src, lbrack, start, closeIdx, colonIdx) - (closeIdx + 1 - start) matches++ i = closeIdx + 1 } if matches == 0 { return src } out := []byte{:outLen} o := int32(0) i = 0 for i < n { start := LIndexFrom(src, i, bc) if start < 0 { o = putRange(out, o, src, i, n) break } lbrack, closeIdx, colonIdx, ok := sliceLiteralScan(src, start) if !ok { o = putRange(out, o, src, i, start+2) i = start + 2 continue } o = putRange(out, o, src, i, lbrack) o = emitSliceLiteral(out, o, src, lbrack, start, closeIdx, colonIdx) i = closeIdx + 1 } // out[:o] rather than out: the estimate can only err LARGE (an underestimate // faults at once in putRange), and an overestimate leaves unwritten NULs // that the parser reports as "invalid NUL character" at end of file - a // silent corruption that a miscount of __slicealloc( by one byte produced. return out[:o] } func isIdentByteAt(src []byte, k int32) (r bool) { if k < 0 { return false } ch := src[k] return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '.' } // followsConstruct reports whether the identifier at start is the result type // of a construct whose text continues leftward, in which case a space before it // is inside the type rather than a boundary: `func() int32` and `map[K]V` end in // `)` / `]`, while `chan int32` and friends end in a bare type keyword. func followsConstruct(src []byte, start int32) (r bool) { k := start - 1 for k >= 0 && (src[k] == ' ' || src[k] == '\t') { k-- } if k < 0 { return false } if src[k] == ')' || src[k] == ']' { return true } for _, kw := range []string{"chan", "map", "func", "struct", "interface"} { if wordEndsAt(src, k+1, kw) { return true } } return false } // wordEndsAt reports whether word occupies src[end-len(word):end] as a whole // word (not as the tail of a longer identifier). func wordEndsAt(src []byte, end int32, word string) (r bool) { n := int32(len(word)) if end < n { return false } for i := int32(0); i < n; i++ { if src[end-n+i] != word[i] { return false } } return !isIdentByteAt(src, end-n-1) } func findSliceTypeStart(src []byte, braceIdx int32) (n int32) { j := braceIdx - 1 for j >= 0 && (src[j] == ' ' || src[j] == '\t' || src[j] == '\n') { j-- } if j < 0 { return -1 } depth := 0 parenDepth := 0 // braceDepth tracks the TYPE's own braces: `[]struct{ n int32 }{:2}` must // scan back over `struct{ n int32 }` as one unit. Without it the `}` of the // struct was treated as a boundary and the type text came out as // `int32 }`, which the parser then rejected with "unexpected :". inTag // skips a backquoted struct tag, whose contents may hold braces or colons. braceDepth := 0 inTag := false candidate := -1 identStart := -1 for j >= 0 { ch := src[j] if ch == '`' { inTag = !inTag j-- continue } if inTag { j-- continue } if ch == '}' { braceDepth++ j-- continue } if ch == '{' { if braceDepth > 0 { braceDepth-- } j-- continue } if braceDepth > 0 { j-- continue } if ch == ')' { parenDepth++ } else if ch == '(' { if parenDepth > 0 { parenDepth-- } else { if candidate >= 0 { return candidate } if identStart >= 0 { return identStart } return -1 } } else if parenDepth > 0 { j-- continue } if ch == ']' { depth++ } else if ch == '[' { depth-- if depth == 0 && !isIdentByteAt(src, j-1) { // A balanced pair starts the type only when nothing names it: // the `[` of `map[K]V` or of a generic `List[T]` is preceded by // an identifier and must not be mistaken for the slice's `[`. candidate = j } } else if depth == 0 && parenDepth == 0 { if candidate >= 0 { return candidate } if ch == ' ' || ch == '\t' || ch == '\n' { // A space after a type name ends the type: `return nat{:1}` // must not scan back into the `return` keyword. Spaces // before any type char (e.g. `[]byte {`) are skipped. if identStart >= 0 && !followsConstruct(src, identStart) { return identStart } j-- continue } if ch == '*' || ch == '(' || ch == ')' { j-- continue } if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '.' { // A bare type name (buffer{:0:64}) has no brackets to mark // its start; record the leftmost identifier byte and return // it when a real boundary follows. identStart = j j-- continue } if identStart >= 0 { return identStart } return -1 } j-- } if candidate >= 0 { return candidate } if identStart >= 0 { return identStart } return -1 } func findMatchingBrace(src []byte, openIdx int32) (n int32) { depth := 1 for i := openIdx + 1; i < len(src); i++ { if src[i] == '{' { depth++ } else if src[i] == '}' { depth-- if depth == 0 { return i } } } return -1 } func RewriteChanMakeLiterals(src []byte) (buf []byte) { chanKw := []byte("chan ") var chunks [][]byte started := false i := 0 for i < len(src) { idx := LIndexFrom(src, i, chanKw) if idx < 0 { if started { chunks = mxutil.Ensure(chunks, 1) push(chunks, src[i:]) } break } j := idx + 5 for j < len(src) && (src[j] == ' ' || src[j] == '\t') { j++ } if j >= len(src) { if started { chunks = mxutil.Ensure(chunks, 1) push(chunks, src[i:]) } break } if src[j] == '<' && j+1 < len(src) && src[j+1] == '-' { if started { chunks = mxutil.Ensure(chunks, 1) push(chunks, src[i:j+2]) } i = j + 2 continue } for j < len(src) && (src[j] != '{' && src[j] != '\n' && src[j] != ';' && src[j] != '(' && src[j] != ')') { if src[j] == ' ' || src[j] == '\t' { break } j++ } if j >= len(src) || src[j] != '{' { if !started { i = idx + 4 } else { chunks = mxutil.Ensure(chunks, 1) push(chunks, src[i:idx+4]) i = idx + 4 } continue } braceOpen := j endsStruct := braceOpen >= 6 && string(src[braceOpen-6:braceOpen]) == "struct" endsIface := braceOpen >= 9 && string(src[braceOpen-9:braceOpen]) == "interface" if endsStruct || endsIface { typeClose := findMatchingBrace(src, braceOpen) if typeClose < 0 { if !started { i = idx + 4 } else { chunks = mxutil.Ensure(chunks, 1) push(chunks, src[i:idx+4]) i = idx + 4 } continue } k := typeClose + 1 if k >= len(src) || src[k] != '{' { if !started { i = typeClose + 1 } else { chunks = mxutil.Ensure(chunks, 1) push(chunks, src[i:typeClose+1]) i = typeClose + 1 } continue } braceOpen = k } closeIdx := findMatchingBrace(src, braceOpen) if closeIdx < 0 { if !started { i = idx + 4 } else { chunks = mxutil.Ensure(chunks, 1) push(chunks, src[i:idx+4]) i = idx + 4 } continue } inner := LTrimSpace(src[braceOpen+1 : closeIdx]) // chan T{:n} is the capacity-only form (a channel has no length // separate from its capacity): the allocator takes the number. if len(inner) > 0 && inner[0] == ':' { inner = LTrimSpace(inner[1:]) } chanType := src[idx : braceOpen] for len(chanType) > 0 && (chanType[len(chanType)-1] == ' ' || chanType[len(chanType)-1] == '\t') { chanType = resize(chanType, len(chanType)-1) } if !started { chunks = mxutil.Ensure(chunks, 1) push(chunks, src[:idx]) started = true } else { chunks = mxutil.Ensure(chunks, 1) push(chunks, src[i:idx]) } // chan T{depth, budget}: the second field is the PER-MESSAGE byte budget, // which the ring is sized from. It is an ordinary expression, // so relay policy read from config can set it at run time. A channel // with no stated depth still gets one slot. depth, budget := splitChanSpec(inner) if len(depth) == 0 { depth = []byte("1") } chunks = mxutil.Ensure(chunks, 1) push(chunks, []byte("__slicealloc(")) chunks = mxutil.Ensure(chunks, 1) push(chunks, chanType) chunks = mxutil.Ensure(chunks, 1) push(chunks, []byte(", ")) chunks = mxutil.Ensure(chunks, 1) push(chunks, depth) if len(budget) > 0 { chunks = mxutil.Ensure(chunks, 1) push(chunks, []byte(", ")) chunks = mxutil.Ensure(chunks, 1) push(chunks, budget) } chunks = mxutil.Ensure(chunks, 1) push(chunks, []byte(")")) i = closeIdx + 1 } if !started { return src } return JoinChunks(chunks) } // splitChanSpec splits a channel literal's inner text into its element count // and its optional per-element byte budget at the first TOP-LEVEL colon, the // notation `chan T{:depth:bytes}`. A colon inside brackets is part of a slice // expression, not a field separator. An empty depth means the default (one // slot), which is how `chan T{::65536}` states a budget with no element count. func splitChanSpec(inner []byte) (depth, budget []byte) { parens := int32(0) brackets := int32(0) braces := int32(0) for i := int32(0); i < len(inner); i++ { switch inner[i] { case '(': parens++ case ')': parens-- case '[': brackets++ case ']': brackets-- case '{': braces++ case '}': braces-- case ':': if parens == 0 && brackets == 0 && braces == 0 { return LTrimSpace(inner[:i]), LTrimSpace(inner[i+1:]) } } } return LTrimSpace(inner), nil } // RewriteEdgeDecls lowers the declared ownership pair // // parent, child := edge[In, Out]{depth} // // into the form the compiler special-cases: // // var parent Edge[In, Out]; var child Edge[In, Out]; __edgealloc(&parent, &child, Edge[In, Out]{}, depth) // // The declaration is the language surface (docs/memory-model.md D19); the // lowered form is the same machinery []T{:n} uses, where the first argument of // the internal builtin is a TYPE expression and the compiler lowers the call. // Newlines in the replaced range are re-emitted so diagnostics keep their // line numbers. func RewriteEdgeDecls(src []byte) (buf []byte) { var chunks [][]byte i := int32(0) last := int32(0) for i < len(src) { k := LIndexFrom(src, i, []byte("edge[")) if k < 0 { break } e0 := k // Identifier boundary: not part of a longer identifier. if e0 > 0 && isIdentByteAt(src, e0-1) { i = e0 + 5 continue } open := e0 + 4 rb := edgeMatchDelim(src, open, '[', ']') if rb < 0 { i = e0 + 5 continue } j := rb + 1 for j < len(src) && edgeSpace(src[j]) { j = j + 1 } if j >= len(src) || src[j] != '{' { i = e0 + 5 continue } rc := findMatchingBrace(src, j) if rc < 0 { i = e0 + 5 continue } // Backward: ident ',' ident ':=' before the keyword. b := e0 - 1 for b >= 0 && edgeSpace(src[b]) { b = b - 1 } // Backward from the keyword the define token is `=:` read right-to-left. if b < 1 || src[b] != '=' || src[b-1] != ':' { i = e0 + 5 continue } b = b - 2 for b >= 0 && edgeSpace(src[b]) { b = b - 1 } cEnd := b + 1 for b >= 0 && edgeIdentBody(src[b]) { b = b - 1 } cStart := b + 1 if cStart >= cEnd { i = e0 + 5 continue } for b >= 0 && edgeSpace(src[b]) { b = b - 1 } if b < 0 || src[b] != ',' { i = e0 + 5 continue } b = b - 1 for b >= 0 && edgeSpace(src[b]) { b = b - 1 } pEnd := b + 1 for b >= 0 && edgeIdentBody(src[b]) { b = b - 1 } pStart := b + 1 if pStart >= pEnd { i = e0 + 5 continue } // Statement position: start of input, or after ; { } or a line break. if pStart > 0 { pc := src[pStart-1] if !(pc == ';' || pc == '{' || pc == '}' || pc == '\n' || pc == '\t' || pc == ' ') { i = e0 + 5 continue } } paren := src[pStart:pEnd] cname := src[cStart:cEnd] types := LTrimSpace(src[open+1 : rb]) depth := LTrimSpace(src[j+1 : rc]) if len(types) == 0 { i = e0 + 5 continue } if len(depth) == 0 { depth = []byte("1") } // The child is the COMPLEMENT: Edge[Out, In]. One type on both sides // would type the child's Receive end for In while the parent sends Out. inText, outText, okArgs := splitEdgeArgs(types) if !okArgs { i = e0 + 5 continue } childType := []byte("Edge[") childType = push(childType, outText...) childType = push(childType, []byte(", ")...) childType = push(childType, inText...) childType = push(childType, []byte("]")...) push(chunks, src[last:pStart]) push(chunks, []byte("var ")) push(chunks, paren) push(chunks, []byte(" Edge[")) push(chunks, types) push(chunks, []byte("]; var ")) push(chunks, cname) push(chunks, []byte(" ")) // childType already carries Edge[...] push(chunks, childType) push(chunks, []byte("; __edgealloc(&")) push(chunks, paren) push(chunks, []byte(", &")) push(chunks, cname) push(chunks, []byte(", Edge[")) push(chunks, types) push(chunks, []byte("]{}")) push(chunks, []byte(", ")) push(chunks, depth) push(chunks, []byte(")")) for nl := pStart; nl <= rc; nl++ { if src[nl] == '\n' { push(chunks, []byte("\n")) } } last = rc + 1 i = rc + 1 } if len(chunks) == 0 { return src } push(chunks, src[last:]) return JoinChunks(chunks) } func edgeSpace(c byte) (ok bool) { return c == ' ' || c == '\t' || c == '\n' || c == '\r' } func edgeIdentBody(c byte) (ok bool) { return c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') } // edgeMatchDelim finds the delimiter that closes the one at open. Type // arguments nest brackets ([]byte) but cannot contain a string literal, so // counting is enough. func edgeMatchDelim(src []byte, open int32, oc, cc byte) (n int32) { d := int32(0) for i := open; i < len(src); i++ { if src[i] == oc { d = d + 1 } else if src[i] == cc { d = d - 1 if d == 0 { return i } } } return -1 } // splitEdgeArgs splits an edge declaration's type arguments at the top-level // comma: edge[In, Out] names the parent's receive type first and its send type // second. func splitEdgeArgs(b []byte) (ins, outs []byte, ok bool) { d := int32(0) for i := int32(0); i < len(b); i++ { switch b[i] { case '[', '(', '{': d = d + 1 case ']', ')', '}': d = d - 1 case ',': if d == 0 { return LTrimSpace(b[:i]), LTrimSpace(b[i+1:]), true } } } return nil, nil, false }