Beispiel #1
0
func initCompiler(opts *driverOptions) (*irgen.Compiler, error) {
	importPaths := make([]string, len(opts.importPaths)+len(opts.libPaths))
	copy(importPaths, opts.importPaths)
	copy(importPaths[len(opts.importPaths):], opts.libPaths)
	if opts.prefix != "" {
		importPaths = append(importPaths, filepath.Join(opts.prefix, "lib"+LibDirSuffix, "go", "llgo-"+llvmVersion()))
	}
	copts := irgen.CompilerOptions{
		TargetTriple:       opts.triple,
		GenerateDebug:      opts.generateDebug,
		DebugPrefixMaps:    opts.debugPrefixMaps,
		DumpSSA:            opts.dumpSSA,
		GccgoPath:          opts.gccgoPath,
		GccgoABI:           opts.gccgoPath != "",
		ImportPaths:        importPaths,
		SanitizerAttribute: opts.sanitizer.getAttribute(),
	}
	if opts.dumpTrace {
		copts.Logger = log.New(os.Stderr, "", 0)
	}
	return irgen.NewCompiler(copts)
}
Beispiel #2
0
func (in *interp) loadSourcePackage(fset *token.FileSet, files []*ast.File, pkgpath string, copts irgen.CompilerOptions) (pkg *types.Package, err error) {
	compiler, err := irgen.NewCompiler(copts)
	if err != nil {
		return
	}

	module, err := compiler.Compile(fset, files, pkgpath)
	if err != nil {
		return
	}
	pkg = module.Package

	if in.engine.C != nil {
		in.engine.AddModule(module.Module)
	} else {
		options := llvm.NewMCJITCompilerOptions()
		in.engine, err = llvm.NewMCJITCompiler(module.Module, options)
		if err != nil {
			return
		}
	}

	importname := irgen.ManglePackagePath(pkgpath) + "..import$descriptor"
	importglobal := module.Module.NamedGlobal(importname)

	var importfunc func()
	*(*unsafe.Pointer)(unsafe.Pointer(&importfunc)) = in.engine.PointerToGlobal(importglobal)

	defer func() {
		p := recover()
		if p != nil {
			err = fmt.Errorf("panic: %v\n%v", p, string(debug.Stack()))
		}
	}()
	importfunc()
	in.pkgmap[pkgpath] = pkg
	return
}