-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnative_source.go
More file actions
411 lines (393 loc) · 12.8 KB
/
Copy pathnative_source.go
File metadata and controls
411 lines (393 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package plan9asm
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"
)
// NativeData describes a Go global whose storage is supplied by native assembly.
type NativeData struct {
Name string
Size uint32
}
// ForeignNativeFunctions identifies files containing only file-local TEXT symbols.
// This is a routing hint, NOT an inference of a C function signature. The native
// backend validates frames, flags, instructions, and references independently.
// Files with package-visible TEXT must use the typed backend and explicit Go
// declarations. Unsupported native forms must not fall back to signature guessing.
func ForeignNativeFunctions(src []byte, goarch string) map[string]bool {
f, err := Parse(Arch(goarch), string(src))
if err != nil || len(f.Funcs) == 0 {
return nil
}
result := make(map[string]bool)
for _, fn := range f.Funcs {
if !strings.HasSuffix(fn.Sym, "<>") {
return nil
}
result[strings.TrimSuffix(fn.Sym, "<>")] = true
}
return result
}
// NativeOptions selects a bounded physical-register backend. It does not describe
// Go ABI entries: callers must already obey the target native calling convention.
type NativeOptions struct {
GOOS, GOARCH string
PackagePath string
Imports map[string]string
}
// SupportsNativeTarget reports implemented instruction/object-format pairs.
func SupportsNativeTarget(goos, goarch string) bool {
return (goos == "darwin" || goos == "linux") && (goarch == "arm64" || goarch == "amd64")
}
// TranslateNativeSource translates checked Plan 9 source directly to native
// assembly, without Go object files, implicit frames, or signature inference.
func TranslateNativeSource(src []byte, opts NativeOptions) (string, []NativeData, error) {
f, ep, data, err := prepareNativeSource(src, opts)
if err != nil {
return "", nil, err
}
e := *ep
var out strings.Builder
for i, fn := range f.Funcs {
if err := e.functionLabels(fn, i); err != nil {
return "", nil, err
}
fmt.Fprintf(&out, ".text\n.p2align 2\n%s:\n", e.funcs[fn.Sym])
if opts.GOOS == "linux" {
fmt.Fprintf(&out, ".type %s, @function\n", e.funcs[fn.Sym])
}
if err := e.functionBody(&out, fn); err != nil {
return "", nil, err
}
if opts.GOOS == "linux" {
fmt.Fprintf(&out, ".size %s, .-%s\n", e.funcs[fn.Sym], e.funcs[fn.Sym])
}
}
for _, g := range f.Globl {
flags, _ := nativeFlags(g.Flags, "RODATA", "NOPTR")
if flags["RODATA"] {
if opts.GOOS == "darwin" {
out.WriteString(".section __DATA_CONST,__const\n")
} else {
out.WriteString(".section .data.rel.ro,\"aw\",@progbits\n")
}
} else {
out.WriteString(".data\n")
}
label := strconv.Quote(e.globals[g.Sym])
fmt.Fprintf(&out, ".p2align 3\n.globl %s\n%s:\n", label, label)
values, _ := e.dataValues(f, g)
pos := int64(0)
for _, d := range values {
if d.Off > pos {
fmt.Fprintf(&out, ".zero %d\n", d.Off-pos)
}
if d.Addr != "" {
target, err := e.symbol(d.Addr, true)
if err != nil {
return "", nil, err
}
fmt.Fprintf(&out, ".quad %s\n", target)
} else {
directive := map[int64]string{1: ".byte", 2: ".short", 4: ".long", 8: ".quad"}[d.Width]
mask := ^uint64(0)
if d.Width < 8 {
mask = (uint64(1) << (8 * d.Width)) - 1
}
fmt.Fprintf(&out, "%s %#x\n", directive, d.Value&mask)
}
pos = d.Off + d.Width
}
if pos < g.Size {
fmt.Fprintf(&out, ".zero %d\n", g.Size-pos)
}
}
if opts.GOOS == "linux" {
out.WriteString(".section .note.GNU-stack,\"\",@progbits\n")
}
return out.String(), data, nil
}
func prepareNativeSource(src []byte, opts NativeOptions) (*File, *nativeEmitter, []NativeData, error) {
if !SupportsNativeTarget(opts.GOOS, opts.GOARCH) {
return nil, nil, nil, fmt.Errorf("unsupported native target %s/%s", opts.GOOS, opts.GOARCH)
}
imports, pkgPath := opts.Imports, opts.PackagePath
clean, err := nativeSource(src, opts.GOARCH)
if err != nil {
return nil, nil, nil, err
}
f, err := Parse(Arch(opts.GOARCH), clean)
if err != nil {
return nil, nil, nil, err
}
if len(f.Funcs) == 0 {
return nil, nil, nil, fmt.Errorf("native assembly requires TEXT")
}
e := nativeEmitter{target: opts, funcs: map[string]string{}, globals: map[string]string{}, imports: imports}
for name, alias := range imports {
if !nativeName.MatchString(name) || !nativeName.MatchString(alias) {
return nil, nil, nil, fmt.Errorf("unsupported native import %q -> %q", name, alias)
}
}
for i, fn := range f.Funcs {
name := strings.TrimSuffix(fn.Sym, "<>")
if name == fn.Sym || !nativeName.MatchString(name) {
return nil, nil, nil, fmt.Errorf("native TEXT must be file-local: %s", fn.Sym)
}
if _, ok := e.funcs[fn.Sym]; ok {
return nil, nil, nil, fmt.Errorf("duplicate native TEXT %s", fn.Sym)
}
e.funcs[fn.Sym] = fmt.Sprintf("Lnative_func_%d", i)
parts := strings.Split(fn.Instrs[0].Raw, ",")
if len(parts) != 3 || (strings.TrimSpace(parts[2]) != "$0" && strings.TrimSpace(parts[2]) != "$0-0") {
return nil, nil, nil, fmt.Errorf("native TEXT requires zero Go frame and arguments: %s", fn.Sym)
}
flags, err := nativeFlags(parts[1], "NOSPLIT", "NOFRAME")
if err != nil {
return nil, nil, nil, err
}
if !flags["NOSPLIT"] {
return nil, nil, nil, fmt.Errorf("native TEXT requires NOSPLIT: %s", fn.Sym)
}
for _, ins := range fn.Instrs {
if (ins.Op == "BL" || ins.Op == "CALL") && !flags["NOFRAME"] {
return nil, nil, nil, fmt.Errorf("native non-leaf TEXT requires NOFRAME: %s", fn.Sym)
}
}
}
var data []NativeData
for _, g := range f.Globl {
name := strings.TrimPrefix(g.Sym, "·")
if name == g.Sym || !nativeName.MatchString(name) {
return nil, nil, nil, fmt.Errorf("native GLOBL must name a package global: %s", g.Sym)
}
if _, ok := e.globals[g.Sym]; ok {
return nil, nil, nil, fmt.Errorf("duplicate native GLOBL %s", g.Sym)
}
if g.Size <= 0 || g.Size > 64<<20 {
return nil, nil, nil, fmt.Errorf("unsupported native GLOBL size %d", g.Size)
}
if _, err := nativeFlags(g.Flags, "RODATA", "NOPTR"); err != nil {
return nil, nil, nil, err
}
e.globals[g.Sym] = e.prefix() + pkgPath + "." + name
data = append(data, NativeData{pkgPath + "." + name, uint32(g.Size)})
}
for _, d := range f.Data {
if _, ok := e.globals[d.Sym]; !ok {
return nil, nil, nil, fmt.Errorf("native DATA has no GLOBL: %s", d.Sym)
}
}
for _, g := range f.Globl {
if _, err := e.dataValues(f, g); err != nil {
return nil, nil, nil, err
}
}
return f, &e, data, nil
}
var nativeName = regexp.MustCompile(`^[A-Za-z_][A-Za-z_0-9]*$`)
var nativePseudoRegister = regexp.MustCompile(`\b(?:SP|FP|g|G|R18_PLATFORM|R18|R31|W[0-9]+)\b`)
// The general parser deliberately tolerates ignored includes and symbolic
// placeholders. The native backend must reject these before parsing, rather
// than silently emitting guessed offsets or dropping conditional code.
func nativeSource(src []byte, goarch string) (string, error) {
var b strings.Builder
for _, line := range strings.Split(string(src), "\n") {
line, _, _ = strings.Cut(line, "//")
line = strings.TrimSpace(line)
if line == "" {
continue
}
if strings.HasPrefix(line, "#") {
if line != `#include "textflag.h"` {
return "", fmt.Errorf("unsupported native preprocessor directive: %s", line)
}
continue
}
if strings.Contains(line, "/*") || strings.ContainsAny(line, "\"\\") {
return "", fmt.Errorf("unsupported native source syntax: %s", line)
}
if (goarch == "arm64" && nativePseudoRegister.MatchString(line)) || (goarch == "amd64" && nativeAMD64PseudoRegister.MatchString(line)) {
return "", fmt.Errorf("unsupported native register or Go stack operand: %s", line)
}
for _, stmt := range splitSemicolons(line) {
if strings.Contains(stmt, ":") && (!strings.HasSuffix(stmt, ":") || !nativeName.MatchString(strings.TrimSuffix(stmt, ":"))) {
return "", fmt.Errorf("unsupported native label or segment syntax: %s", stmt)
}
op, rest := splitOpcode(stmt)
if op != "TEXT" && op != "GLOBL" && op != "DATA" {
for _, raw := range splitTopLevelCSV(rest) {
raw = strings.TrimSpace(raw)
if !strings.HasPrefix(raw, "$") && strings.HasSuffix(raw, ")") && !strings.HasSuffix(raw, "(SB)") {
if i := strings.LastIndex(raw, "("); i >= 0 {
offset := strings.TrimSpace(raw[:i])
if offset != "" {
if _, ok := parseImmExpr(offset); !ok {
return "", fmt.Errorf("native memory requires a constant offset: %s", raw)
}
}
}
}
}
}
// Enforce integer expressions before the permissive parser can invent a
// symbolic GLOBL size or interpret an integer operand as float bits.
if op == "GLOBL" {
parts := strings.Split(rest, ",")
if len(parts) != 3 {
return "", fmt.Errorf("invalid native GLOBL")
}
if _, ok := nativeInteger(strings.TrimSpace(parts[2])); !ok {
return "", fmt.Errorf("native GLOBL requires a constant integer size")
}
}
if op == "DATA" {
_, rhs, ok := strings.Cut(rest, ",")
rhs = strings.TrimSpace(rhs)
if !ok {
return "", fmt.Errorf("invalid native DATA")
}
if _, ok := nativeInteger(rhs); !ok && !(strings.HasPrefix(rhs, "$") && strings.HasSuffix(rhs, "(SB)")) {
return "", fmt.Errorf("unsupported native DATA initializer: %s", rhs)
}
}
}
b.WriteString(line)
b.WriteByte('\n')
}
return b.String(), nil
}
func nativeInteger(s string) (uint64, bool) {
if !strings.HasPrefix(s, "$") {
return 0, false
}
return parseImmExpr(strings.TrimPrefix(s, "$"))
}
func nativeFlags(s string, allowed ...string) (map[string]bool, error) {
flags := map[string]bool{}
for _, flag := range strings.Split(strings.TrimSpace(s), "|") {
flag = strings.TrimSpace(flag)
if flag == "0" {
continue
}
ok := false
for _, a := range allowed {
ok = ok || flag == a
}
if !ok || flags[flag] {
return nil, fmt.Errorf("unsupported native flag %q", flag)
}
flags[flag] = true
}
return flags, nil
}
var nativeAMD64PseudoRegister = regexp.MustCompile(`\b(?:FP|g|G)\b`)
type nativeEmitter struct {
symbolOperand func(string, bool) (string, error)
target NativeOptions
funcs, globals, imports, labels map[string]string
}
func (e *nativeEmitter) prefix() string {
if e.target.GOOS == "darwin" {
return "_"
}
return ""
}
func (e *nativeEmitter) symbol(s string, data bool) (string, error) {
if e.symbolOperand != nil {
return e.symbolOperand(s, data)
}
if !strings.HasSuffix(s, "(SB)") {
return "", fmt.Errorf("unsupported native symbol %s", s)
}
name := strings.TrimSuffix(s, "(SB)")
if label, ok := e.funcs[name]; ok {
return label, nil
}
if data {
if label, ok := e.globals[name]; ok {
return strconv.Quote(label), nil
}
}
if alias, ok := e.imports[name]; ok {
return strconv.Quote(e.prefix() + alias), nil
}
return "", fmt.Errorf("undeclared foreign symbol or unsupported native reference %s", s)
}
func (e *nativeEmitter) branch(o Operand, external bool) (string, error) {
if external && o.Kind == OpSym {
return e.symbol(o.Sym, false)
}
name := o.Ident
if o.Kind == OpLabel {
name = o.Sym
}
if label, ok := e.labels[name]; ok {
return label, nil
}
return "", fmt.Errorf("undefined native branch label %s", o.String())
}
func (e *nativeEmitter) functionLabels(fn Func, i int) error {
e.labels = map[string]string{}
for _, ins := range fn.Instrs {
if ins.Op == OpLABEL {
name := ins.Args[0].Sym
if !nativeName.MatchString(name) {
return fmt.Errorf("unsupported native label %s", name)
}
if _, ok := e.labels[name]; ok {
return fmt.Errorf("duplicate native label %s", name)
}
e.labels[name] = fmt.Sprintf("Lnative_%d_label_%d", i, len(e.labels))
}
}
return nil
}
func (e *nativeEmitter) functionBody(out *strings.Builder, fn Func) error {
for _, ins := range fn.Instrs {
var err error
if e.target.GOARCH == "arm64" {
err = (&nativeARM64{e}).instruction(out, ins)
} else {
err = (&nativeAMD64{e}).instruction(out, ins)
}
if err != nil {
return fmt.Errorf("native %s: %s: %w", fn.Sym, ins.Raw, err)
}
}
return nil
}
func (e *nativeEmitter) dataValues(f *File, g GloblStmt) ([]DataStmt, error) {
var values []DataStmt
for _, d := range f.Data {
if d.Sym == g.Sym {
values = append(values, d)
}
}
sort.Slice(values, func(i, j int) bool { return values[i].Off < values[j].Off })
pos := int64(0)
for _, d := range values {
if d.Off < pos || d.Width <= 0 || d.Off > g.Size || d.Width > g.Size-d.Off {
return nil, fmt.Errorf("overlapping or out-of-bounds native DATA for %s", g.Sym)
}
if d.Addr != "" {
if d.Width != 8 {
return nil, fmt.Errorf("native address DATA requires width 8")
}
if _, err := e.symbol(d.Addr, true); err != nil {
return nil, err
}
} else {
if d.Payload != nil {
return nil, fmt.Errorf("native string DATA is unsupported")
}
if d.Width != 1 && d.Width != 2 && d.Width != 4 && d.Width != 8 {
return nil, fmt.Errorf("unsupported native DATA width %d", d.Width)
}
}
pos = d.Off + d.Width
}
return values, nil
}