// Emit the start of a function. func emitFuncStart(fn *ssa.Function, blks []*ssa.BasicBlock, trackPhi bool, canOptMap map[string]bool, mustSplitCode bool, reconstruct []tgossa.BlockFormat) { l := TargetLang posStr := CodePosition(fn.Pos()) pName, mName := GetFnNameParts(fn) isPublic := unicode.IsUpper(rune(mName[0])) // TODO check rules for non-ASCII 1st characters and fix fmt.Fprintln(&LanguageList[l].buffer, LanguageList[l].FuncStart(pName, mName, fn, blks, posStr, isPublic, trackPhi, grMap[fn] || mustSplitCode, canOptMap, reconstruct)) }
// FunctionName returns a unique function path and name. // TODO refactor this code and everywhere it is called to remove duplication. func FuncPathName(fn *ssa.Function) (path, name string) { rx := fn.Signature.Recv() pf := MakeID(rootProgram.Fset.Position(fn.Pos()).String()) //fmt.Sprintf("fn%d", fn.Pos()) if rx != nil { // it is not the name of a normal function, but that of a method, so append the method description pf = rx.Type().String() // NOTE no underlying() } else { if fn.Pkg != nil { pf = fn.Pkg.Object.Path() // was .Name(), but not unique } } return pf, fn.Name() }
// callSSA interprets a call to function fn with arguments args, // and lexical environment env, returning its result. // callpos is the position of the callsite. // func callSSA(i *interpreter, caller *frame, callpos token.Pos, fn *ssa.Function, args []value, env []value) value { if i.mode&EnableTracing != 0 { fset := fn.Prog.Fset // TODO(adonovan): fix: loc() lies for external functions. fmt.Fprintf(os.Stderr, "Entering %s%s.\n", fn, loc(fset, fn.Pos())) suffix := "" if caller != nil { suffix = ", resuming " + caller.fn.String() + loc(fset, callpos) } defer fmt.Fprintf(os.Stderr, "Leaving %s%s.\n", fn, suffix) } fr := &frame{ i: i, caller: caller, // for panic/recover fn: fn, } if fn.Parent() == nil { name := fn.String() if ext := externals[name]; ext != nil { if i.mode&EnableTracing != 0 { fmt.Fprintln(os.Stderr, "\t(external)") } return ext(fr, args) } if fn.Blocks == nil { panic("no code for function: " + name) } } fr.env = make(map[ssa.Value]value) fr.block = fn.Blocks[0] fr.locals = make([]value, len(fn.Locals)) for i, l := range fn.Locals { fr.locals[i] = zero(deref(l.Type())) fr.env[l] = &fr.locals[i] } for i, p := range fn.Params { fr.env[p] = args[i] } for i, fv := range fn.FreeVars { fr.env[fv] = env[i] } for fr.block != nil { runFrame(fr) } // Destroy the locals to avoid accidental use after return. for i := range fn.Locals { fr.locals[i] = bad{} } return fr.result }
func checkFn(fn *ssa.Function, prog *ssa.Program, ptrResult *pointer.Result, roots []*ssa.Function) { nam := fn.String() if strings.HasPrefix(strings.TrimPrefix(nam, "("), *prefix) { hasPath := false usedExternally := false for _, r := range roots { if r != nil { nod, ok := ptrResult.CallGraph.Nodes[r] if ok { //fmt.Println("NODE root", r.Name()) pth := callgraph.PathSearch(nod, func(n *callgraph.Node) bool { if n == nil { return false } if n.Func == fn { for _, ine := range n.In { if ine.Caller.Func.Pkg != fn.Pkg { //fmt.Println("DEBUG diff? ", // ine.Caller.Func.Pkg, fn.Pkg) usedExternally = true break } } return true } return false }) if pth != nil { //fmt.Printf("DEBUG path from %v to %v = %v\n", // r, fn, pth) hasPath = true break } } } } isUpper := unicode.IsUpper(rune(fn.Name()[0])) pos := fn.Pos() //if strings.HasPrefix(nam, "(") && (!hasPath || (!usedExternally && isUpper)) { // fmt.Println("bad Pos", pos, prog.Fset.Position(pos).String()) //} loc := strings.TrimPrefix( prog.Fset.Position(pos).String(), gopath+"/src/"+*prefix+"/") showFuncResult(loc, nam, hasPath, usedExternally, isUpper) } wg.Done() }
// FuncPathName returns a unique function path and name. func (comp *Compilation) FuncPathName(fn *ssa.Function) (path, name string) { rx := fn.Signature.Recv() pf := tgoutil.MakeID(comp.rootProgram.Fset.Position(fn.Pos()).String()) //fmt.Sprintf("fn%d", fn.Pos()) if rx != nil { // it is not the name of a normal function, but that of a method, so append the method description pf = rx.Type().String() // NOTE no underlying() } else { if fn.Pkg != nil { pf = fn.Pkg.Object.Path() // was .Name(), but not unique } else { goroot := tgoutil.MakeID(LanguageList[comp.TargetLang].GOROOT + string(os.PathSeparator)) pf1 := strings.Split(pf, goroot) // make auto-generated names shorter if len(pf1) == 2 { pf = pf1[1] } // TODO use GOPATH for names not in std pkgs } } return pf, fn.Name() }
// Emit a particular function. func emitFunc(fn *ssa.Function) { /* TODO research if the ssautil.Switches() function can be incorporated to provide any run-time improvement to the code // it would give a big adavantage, but only to a very small number of functions - so definately TODO sw := ssautil.Switches(fn) fmt.Printf("DEBUG Switches for : %s \n", fn) for num,swi := range sw { fmt.Printf("DEBUG Switches[%d]= %+v\n", num, swi) } */ var subFnList []subFnInstrs // where the sub-functions are canOptMap := make(map[string]bool) // TODO review use of this mechanism //println("DEBUG processing function: ", fn.Name()) MakePosHash(fn.Pos()) // mark that we have entered a function trackPhi := true switch len(fn.Blocks) { case 0: // NoOp - only output a function if it has a body... so ignore pure definitions (target language may generate an error, if truely undef) //fmt.Printf("DEBUG function has no body, ignored: %v %v \n", fn.Name(), fn.String()) case 1: // Only one block, so no Phi tracking required trackPhi = false fallthrough default: if trackPhi { // check that there actually are Phi instructions to track trackPhi = false phiSearch: for b := range fn.Blocks { for i := range fn.Blocks[b].Instrs { _, trackPhi = fn.Blocks[b].Instrs[i].(*ssa.Phi) if trackPhi { break phiSearch } } } } instrCount := 0 for b := range fn.Blocks { instrCount += len(fn.Blocks[b].Instrs) } mustSplitCode := false if instrCount > LanguageList[TargetLang].InstructionLimit { //println("DEBUG mustSplitCode => large function length:", instrCount, " in ", fn.Name()) mustSplitCode = true } blks := fn.DomPreorder() // was fn.Blocks for b := range blks { // go though the blocks looking for sub-functions instrsEmitted := 0 inSubFn := false for i := range blks[b].Instrs { canPutInSubFn := true in := blks[b].Instrs[i] switch in.(type) { case *ssa.Phi: // phi uses self-referential temp vars that must be pre-initialised canPutInSubFn = false case *ssa.Return: canPutInSubFn = false case *ssa.Call: switch in.(*ssa.Call).Call.Value.(type) { case *ssa.Builtin: //NoOp default: canPutInSubFn = false } case *ssa.Select, *ssa.Send, *ssa.Defer, *ssa.RunDefers, *ssa.Panic: canPutInSubFn = false case *ssa.UnOp: if in.(*ssa.UnOp).Op == token.ARROW { canPutInSubFn = false } } if canPutInSubFn { if inSubFn { if instrsEmitted > LanguageList[TargetLang].SubFnInstructionLimit { subFnList[len(subFnList)-1].end = i subFnList = append(subFnList, subFnInstrs{b, i, 0}) instrsEmitted = 0 } } else { subFnList = append(subFnList, subFnInstrs{b, i, 0}) inSubFn = true } } else { if inSubFn { subFnList[len(subFnList)-1].end = i inSubFn = false } } instrsEmitted++ } if inSubFn { subFnList[len(subFnList)-1].end = len(blks[b].Instrs) } } for sf := range subFnList { // go though the sub-functions looking for optimisable temp vars var instrMap = make(map[ssa.Instruction]bool) for ii := subFnList[sf].start; ii < subFnList[sf].end; ii++ { instrMap[blks[subFnList[sf].block].Instrs[ii]] = true } for i := subFnList[sf].start; i < subFnList[sf].end; i++ { instrVal, hasVal := blks[subFnList[sf].block].Instrs[i].(ssa.Value) if hasVal { refs := *blks[subFnList[sf].block].Instrs[i].(ssa.Value).Referrers() switch len(refs) { case 0: // no other instruction uses the result of this one default: //multiple usage of the register canOpt := true for r := range refs { user := refs[r] if user.Block() != blks[subFnList[sf].block] { canOpt = false break } _, inRange := instrMap[user] if !inRange { canOpt = false break } } if canOpt && !LanguageList[TargetLang].CanInline(blks[subFnList[sf].block].Instrs[i]) { canOptMap[instrVal.Name()] = true } } } } } reconstruct := tgossa.Reconstruct(blks, grMap[fn] || mustSplitCode) if reconstruct != nil { //fmt.Printf("DEBUG reconstruct %s %#v\n",fn.String(),reconstruct) } emitFuncStart(fn, blks, trackPhi, canOptMap, mustSplitCode, reconstruct) thisSubFn := 0 for b := range blks { emitPhi := trackPhi emitBlockStart(blks, b, emitPhi) inSubFn := false for i := 0; i < len(blks[b].Instrs); i++ { if thisSubFn >= 0 && thisSubFn < len(subFnList) { // not at the end of the list if b == subFnList[thisSubFn].block { if i >= subFnList[thisSubFn].end && inSubFn { inSubFn = false thisSubFn++ if thisSubFn >= len(subFnList) { thisSubFn = -1 // we have come to the end of the list } } } } if thisSubFn >= 0 && thisSubFn < len(subFnList) { // not at the end of the list if b == subFnList[thisSubFn].block { if i == subFnList[thisSubFn].start { inSubFn = true l := TargetLang if mustSplitCode { fmt.Fprintln(&LanguageList[l].buffer, LanguageList[l].SubFnCall(thisSubFn)) } else { emitSubFn(fn, blks, subFnList, thisSubFn, mustSplitCode, canOptMap) } } } } if !inSubFn { // optimize phi case statements phiList := 0 phiLoop: switch blks[b].Instrs[i+phiList].(type) { case *ssa.Phi: if len(*blks[b].Instrs[i+phiList].(*ssa.Phi).Referrers()) > 0 { phiList++ if (i + phiList) < len(blks[b].Instrs) { goto phiLoop } } } if phiList > 0 { peephole(blks[b].Instrs[i : i+phiList]) i += phiList - 1 } else { emitPhi = emitInstruction(blks[b].Instrs[i], blks[b].Instrs[i].Operands(make([]*ssa.Value, 0))) } } } if thisSubFn >= 0 && thisSubFn < len(subFnList) { // not at the end of the list if b == subFnList[thisSubFn].block { if inSubFn { thisSubFn++ if thisSubFn >= len(subFnList) { thisSubFn = -1 // we have come to the end of the list } } } } emitBlockEnd(blks, b, emitPhi && trackPhi) } emitRunEnd(fn) if mustSplitCode { for sf := range subFnList { emitSubFn(fn, blks, subFnList, sf, mustSplitCode, canOptMap) } } emitFuncEnd(fn) } }
func (u *unit) defineFunction(f *ssa.Function) { // Only define functions from this package, or synthetic // wrappers (which do not have a package). if f.Pkg != nil && f.Pkg != u.pkg { return } llfn := u.resolveFunctionGlobal(f) linkage := u.getFunctionLinkage(f) isMethod := f.Signature.Recv() != nil // Methods cannot be referred to via a descriptor. if !isMethod { llfd := u.resolveFunctionDescriptorGlobal(f) llfd.SetInitializer(llvm.ConstBitCast(llfn, llvm.PointerType(llvm.Int8Type(), 0))) llfd.SetLinkage(linkage) } // We only need to emit a descriptor for functions without bodies. if len(f.Blocks) == 0 { return } ssaopt.LowerAllocsToStack(f) if u.DumpSSA { f.WriteTo(os.Stderr) } fr := newFrame(u, llfn) defer fr.dispose() fr.addCommonFunctionAttrs(fr.function) fr.function.SetLinkage(linkage) fr.logf("Define function: %s", f.String()) fti := u.llvmtypes.getSignatureInfo(f.Signature) delete(u.undefinedFuncs, f) fr.retInf = fti.retInf // Push the compile unit and function onto the debug context. if u.GenerateDebug { u.debug.PushFunction(fr.function, f.Signature, f.Pos()) defer u.debug.PopFunction() u.debug.SetLocation(fr.builder, f.Pos()) } // If a function calls recover, we create a separate function to // hold the real function, and this function calls __go_can_recover // and bridges to it. if callsRecover(f) { fr = fr.bridgeRecoverFunc(fr.function, fti) } fr.blocks = make([]llvm.BasicBlock, len(f.Blocks)) fr.lastBlocks = make([]llvm.BasicBlock, len(f.Blocks)) for i, block := range f.Blocks { fr.blocks[i] = llvm.AddBasicBlock(fr.function, fmt.Sprintf(".%d.%s", i, block.Comment)) } fr.builder.SetInsertPointAtEnd(fr.blocks[0]) prologueBlock := llvm.InsertBasicBlock(fr.blocks[0], "prologue") fr.builder.SetInsertPointAtEnd(prologueBlock) // Map parameter positions to indices. We use this // when processing locals to map back to parameters // when generating debug metadata. paramPos := make(map[token.Pos]int) for i, param := range f.Params { paramPos[param.Pos()] = i llparam := fti.argInfos[i].decode(llvm.GlobalContext(), fr.builder, fr.builder) if isMethod && i == 0 { if _, ok := param.Type().Underlying().(*types.Pointer); !ok { llparam = fr.builder.CreateBitCast(llparam, llvm.PointerType(fr.types.ToLLVM(param.Type()), 0), "") llparam = fr.builder.CreateLoad(llparam, "") } } fr.env[param] = newValue(llparam, param.Type()) } // Load closure, extract free vars. if len(f.FreeVars) > 0 { for _, fv := range f.FreeVars { fr.env[fv] = newValue(llvm.ConstNull(u.llvmtypes.ToLLVM(fv.Type())), fv.Type()) } elemTypes := make([]llvm.Type, len(f.FreeVars)+1) elemTypes[0] = llvm.PointerType(llvm.Int8Type(), 0) // function pointer for i, fv := range f.FreeVars { elemTypes[i+1] = u.llvmtypes.ToLLVM(fv.Type()) } structType := llvm.StructType(elemTypes, false) closure := fr.runtime.getClosure.call(fr)[0] closure = fr.builder.CreateBitCast(closure, llvm.PointerType(structType, 0), "") for i, fv := range f.FreeVars { ptr := fr.builder.CreateStructGEP(closure, i+1, "") ptr = fr.builder.CreateLoad(ptr, "") fr.env[fv] = newValue(ptr, fv.Type()) } } // Allocate stack space for locals in the prologue block. for _, local := range f.Locals { typ := fr.llvmtypes.ToLLVM(deref(local.Type())) alloca := fr.builder.CreateAlloca(typ, local.Comment) fr.memsetZero(alloca, llvm.SizeOf(typ)) bcalloca := fr.builder.CreateBitCast(alloca, llvm.PointerType(llvm.Int8Type(), 0), "") value := newValue(bcalloca, local.Type()) fr.env[local] = value if fr.GenerateDebug { paramIndex, ok := paramPos[local.Pos()] if !ok { paramIndex = -1 } fr.debug.Declare(fr.builder, local, alloca, paramIndex) } } // If this is the "init" function, enable init-specific optimizations. if !isMethod && f.Name() == "init" { fr.isInit = true } // If the function contains any defers, we must first create // an unwind block. We can short-circuit the check for defers with // f.Recover != nil. if f.Recover != nil || hasDefer(f) { fr.unwindBlock = llvm.AddBasicBlock(fr.function, "") fr.frameptr = fr.builder.CreateAlloca(llvm.Int8Type(), "") } term := fr.builder.CreateBr(fr.blocks[0]) fr.allocaBuilder.SetInsertPointBefore(term) for _, block := range f.DomPreorder() { fr.translateBlock(block, fr.blocks[block.Index]) } fr.fixupPhis() if !fr.unwindBlock.IsNil() { fr.setupUnwindBlock(f.Recover, f.Signature.Results()) } // The init function needs to register the GC roots first. We do this // after generating code for it because allocations may have caused // additional GC roots to be created. if fr.isInit { fr.builder.SetInsertPointBefore(prologueBlock.FirstInstruction()) fr.registerGcRoots() } }