mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-26 06:38:42 +00:00
ir: add descriptive error messages to SimpleDCE pass
This commit modifies the SimpleDCE pass to emit errors similar to those emitted by gc when the main function is missing.
This commit is contained in:
@@ -237,7 +237,10 @@ func Compile(pkgName string, machine llvm.TargetMachine, config *compileopts.Con
|
||||
c.ir = ir.NewProgram(lprogram, pkgName)
|
||||
|
||||
// Run a simple dead code elimination pass.
|
||||
c.ir.SimpleDCE()
|
||||
err = c.ir.SimpleDCE()
|
||||
if err != nil {
|
||||
return c.mod, nil, []error{err}
|
||||
}
|
||||
|
||||
// Initialize debug information.
|
||||
if c.Debug() {
|
||||
|
||||
+12
-2
@@ -1,6 +1,7 @@
|
||||
package ir
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"go/types"
|
||||
|
||||
"golang.org/x/tools/go/ssa"
|
||||
@@ -58,7 +59,7 @@ func signature(sig *types.Signature) string {
|
||||
|
||||
// Simple pass that removes dead code. This pass makes later analysis passes
|
||||
// more useful.
|
||||
func (p *Program) SimpleDCE() {
|
||||
func (p *Program) SimpleDCE() error {
|
||||
// Unmark all functions.
|
||||
for _, f := range p.Functions {
|
||||
f.flag = false
|
||||
@@ -66,7 +67,14 @@ func (p *Program) SimpleDCE() {
|
||||
|
||||
// Initial set of live functions. Include main.main, *.init and runtime.*
|
||||
// functions.
|
||||
main := p.mainPkg.Members["main"].(*ssa.Function)
|
||||
main, ok := p.mainPkg.Members["main"].(*ssa.Function)
|
||||
if !ok {
|
||||
if p.mainPkg.Members["main"] == nil {
|
||||
return errors.New("function main is undeclared in the main package")
|
||||
} else {
|
||||
return errors.New("cannot declare main - must be func")
|
||||
}
|
||||
}
|
||||
runtimePkg := p.Program.ImportedPackage("runtime")
|
||||
mathPkg := p.Program.ImportedPackage("math")
|
||||
taskPkg := p.Program.ImportedPackage("internal/task")
|
||||
@@ -136,4 +144,6 @@ func (p *Program) SimpleDCE() {
|
||||
}
|
||||
}
|
||||
p.Functions = livefunctions
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user