-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspector.go
87 lines (72 loc) · 2.6 KB
/
inspector.go
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
// Copyright (c) 2020-2021 C4 Project
//
// This file is part of c4t.
// Licenced under the MIT licence; see `LICENSE`.
package compiler
import (
"errors"
"fmt"
"github.com/c4-project/c4t/internal/helper/stringhelp"
"github.com/c4-project/c4t/internal/model/service/compiler/optlevel"
)
var (
// ErrConfigNil occurs if we try to select optimisation levels for a nil compiler.
ErrConfigNil = errors.New("can't select levels for nil compiler")
// ErrNoSuchLevel occurs if a Selection enables an optimisation level that isn't available.
ErrNoSuchLevel = errors.New("no such optimisation level")
)
// Inspector is the interface of types that support optimisation level lookup.
type Inspector interface {
// DefaultOptLevels retrieves a set of optimisation levels that are enabled by default for compiler c.
DefaultOptLevels(c *Compiler) (stringhelp.Set, error)
// OptLevels retrieves a set of potential optimisation levels for compiler c.
// This map shouldn't be modified, as it may be global.
OptLevels(c *Compiler) (map[string]optlevel.Level, error)
// DefaultMOpts retrieves a set of machine optimisation directives that are enabled by default for compiler c.
DefaultMOpts(c *Compiler) (stringhelp.Set, error)
// We don't request a list of all possible MOpts from compilers, as the list expands so rapidly that any such
// list would be hideously out of date.
}
//go:generate mockery --name=Inspector
// SelectLevels selects from in the optimisation levels permitted by the configuration c.
func SelectLevels(in Inspector, c *Compiler) (map[string]optlevel.Level, error) {
if c == nil {
return nil, ErrConfigNil
}
all, err := in.OptLevels(c)
if err != nil {
return nil, err
}
dls, err := in.DefaultOptLevels(c)
if err != nil {
return nil, err
}
return filterLevels(chosenLevels(dls, c.Opt), all)
}
// SelectMOpts selects from in the machine optimisation profiles (-march, etc.) permitted by the configuration c.
func SelectMOpts(in Inspector, c *Compiler) (stringhelp.Set, error) {
if c == nil {
return nil, ErrConfigNil
}
dls, err := in.DefaultMOpts(c)
if err != nil {
return nil, err
}
return chosenLevels(dls, c.MOpt), nil
}
func chosenLevels(defaults stringhelp.Set, s *optlevel.Selection) stringhelp.Set {
if s == nil {
return defaults
}
return s.Override(defaults)
}
func filterLevels(choices stringhelp.Set, all map[string]optlevel.Level) (map[string]optlevel.Level, error) {
chosen := make(map[string]optlevel.Level, len(choices))
for c := range choices {
var ok bool
if chosen[c], ok = all[c]; !ok {
return nil, fmt.Errorf("%w: %q", ErrNoSuchLevel, c)
}
}
return chosen, nil
}