compiler: add location information to the IR checker

This commit is contained in:
Ayke van Laethem
2019-12-07 22:10:21 +01:00
committed by Ron Evans
parent dffb9fbfa7
commit 5510dec846
5 changed files with 85 additions and 42 deletions
+10 -12
View File
@@ -33,10 +33,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(stri
// Compile Go code to IR.
errs := c.Compile(pkgName)
if len(errs) != 0 {
if len(errs) == 1 {
return errs[0]
}
return &MultiError{errs}
return newMultiError(errs)
}
if config.Options.PrintIR {
fmt.Println("; Generated LLVM IR:")
@@ -72,22 +69,23 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(stri
// Optimization levels here are roughly the same as Clang, but probably not
// exactly.
errs = nil
switch config.Options.Opt {
case "none:", "0":
err = c.Optimize(0, 0, 0) // -O0
errs = c.Optimize(0, 0, 0) // -O0
case "1":
err = c.Optimize(1, 0, 0) // -O1
errs = c.Optimize(1, 0, 0) // -O1
case "2":
err = c.Optimize(2, 0, 225) // -O2
errs = c.Optimize(2, 0, 225) // -O2
case "s":
err = c.Optimize(2, 1, 225) // -Os
errs = c.Optimize(2, 1, 225) // -Os
case "z":
err = c.Optimize(2, 2, 5) // -Oz, default
errs = c.Optimize(2, 2, 5) // -Oz, default
default:
err = errors.New("unknown optimization level: -opt=" + config.Options.Opt)
errs = []error{errors.New("unknown optimization level: -opt=" + config.Options.Opt)}
}
if err != nil {
return err
if len(errs) > 0 {
return newMultiError(errs)
}
if err := c.Verify(); err != nil {
return errors.New("verification failure after LLVM optimization passes")
+14
View File
@@ -12,6 +12,20 @@ func (e *MultiError) Error() string {
return e.Errs[0].Error()
}
// newMultiError returns a *MultiError if there is more than one error, or
// returns that error directly when there is only one. Passing an empty slice
// will lead to a panic.
func newMultiError(errs []error) error {
switch len(errs) {
case 0:
panic("attempted to create empty MultiError")
case 1:
return errs[0]
default:
return &MultiError{errs}
}
}
// commandError is an error type to wrap os/exec.Command errors. This provides
// some more information regarding what went wrong while running a command.
type commandError struct {