lookup_sig.mx raw

   1  package iskra
   2  
   3  import "bytes"
   4  
   5  // FindBySignature uses the SigIdx for O(1) bucket lookup by (stage, sighash24),
   6  // then verifies with full IsIsomorphic on content.
   7  func FindBySignature(t *Tree, sig *BindingSignature, stage uint8) []uint32 {
   8  	targetHash := SignatureHash24(sig)
   9  	bucket := (uint32(stage) << 24) | targetHash
  10  	candidates := t.SigIdx[bucket]
  11  	if len(candidates) == 0 {
  12  		return nil
  13  	}
  14  	var matches []uint32
  15  	for _, recIdx := range candidates {
  16  		content := t.GetContent(recIdx)
  17  		if len(content) == 0 {
  18  			continue
  19  		}
  20  		cst := ExtractSymbols(string(content))
  21  		candidateSig := SignatureWithTypes(cst)
  22  		if sig.IsIsomorphic(candidateSig) {
  23  			matches = append(matches, recIdx)
  24  		}
  25  	}
  26  	return matches
  27  }
  28  
  29  // FindBySignatureFromAST extracts symbols from an AST dump and finds
  30  // matching entries in the lattice. Convenience wrapper.
  31  func FindBySignatureFromAST(t *Tree, astDump string) []uint32 {
  32  	st := ExtractSymbols(astDump)
  33  	sig := SignatureWithTypes(st)
  34  	return FindBySignature(t, sig, StageAST)
  35  }
  36  
  37  // FindBySignatureInPkg uses SigIdx for O(1) bucket lookup, then filters by
  38  // package prefix in the IR representation (cross-stage key-implicit adjacency).
  39  func FindBySignatureInPkg(t *Tree, sig *BindingSignature, stage uint8, pkgPrefix string) []uint32 {
  40  	targetHash := SignatureHash24(sig)
  41  	bucket := (uint32(stage) << 24) | targetHash
  42  	candidates := t.SigIdx[bucket]
  43  	if len(candidates) == 0 {
  44  		return nil
  45  	}
  46  	prefix := []byte(pkgPrefix)
  47  	var matches []uint32
  48  
  49  	for _, recIdx := range candidates {
  50  		meta := &t.RecMeta[recIdx]
  51  		if len(pkgPrefix) > 0 {
  52  			key, ok := t.db.RecKey[recIdx]
  53  			if !ok {
  54  				continue
  55  			}
  56  			branch := KindToBranch(meta.Kind)
  57  			irKey := MakeCodeKey(StageIR, KeyHash(key))
  58  			irRecIdx := t.db.LookupRecIdx(branch, irKey)
  59  			if irRecIdx == NullLatticeRec {
  60  				continue
  61  			}
  62  			ir := t.GetContent(irRecIdx)
  63  			if !bytes.Contains(ir, prefix) {
  64  				continue
  65  			}
  66  		}
  67  		content := t.GetContent(recIdx)
  68  		if len(content) == 0 {
  69  			continue
  70  		}
  71  		cst := ExtractSymbols(string(content))
  72  		candidateSig := SignatureWithTypes(cst)
  73  		if sig.IsIsomorphic(candidateSig) {
  74  			matches = append(matches, recIdx)
  75  		}
  76  	}
  77  	return matches
  78  }
  79  
  80  func FindBySignatureFromASTInPkg(t *Tree, astDump string, pkgPrefix string) []uint32 {
  81  	st := ExtractSymbols(astDump)
  82  	sig := SignatureWithTypes(st)
  83  	return FindBySignatureInPkg(t, sig, StageAST, pkgPrefix)
  84  }
  85  
  86  // RankMatches reorders matches: self first, then name match, then lowest cost.
  87  func RankMatches(t *Tree, matches []uint32, selfIdx uint32, queryName string) {
  88  	if len(matches) < 2 {
  89  		return
  90  	}
  91  	qMethod := unqualifiedName(queryName)
  92  
  93  	for j, mi := range matches {
  94  		if mi == selfIdx && j != 0 {
  95  			matches[0], matches[j] = matches[j], matches[0]
  96  			return
  97  		}
  98  	}
  99  	if matches[0] == selfIdx {
 100  		return
 101  	}
 102  
 103  	for j := 0; j < len(matches); j++ {
 104  		rec := t.db.GetRecord(matches[j])
 105  		if rec == nil {
 106  			continue
 107  		}
 108  		mName := FormFromRecord(rec, t.StringPool)
 109  		if unqualifiedName(mName) == qMethod {
 110  			if j != 0 {
 111  				matches[0], matches[j] = matches[j], matches[0]
 112  			}
 113  			return
 114  		}
 115  	}
 116  
 117  	RankByCost(t, matches)
 118  }
 119  
 120  // RankByCost sorts matches by ascending CostMap cost.
 121  func RankByCost(t *Tree, matches []uint32) {
 122  	if len(t.CostMap) == 0 || len(matches) < 2 {
 123  		return
 124  	}
 125  	for i := 1; i < len(matches); i++ {
 126  		key := matches[i]
 127  		kCost, kHas := t.CostMap[key]
 128  		j := i - 1
 129  		for j >= 0 {
 130  			jCost, jHas := t.CostMap[matches[j]]
 131  			if !costLess(kCost, kHas, jCost, jHas) {
 132  				break
 133  			}
 134  			matches[j+1] = matches[j]
 135  			j--
 136  		}
 137  		matches[j+1] = key
 138  	}
 139  }
 140  
 141  func costLess(a CostEntry, aHas bool, b CostEntry, bHas bool) bool {
 142  	if aHas && !bHas {
 143  		return true
 144  	}
 145  	if !aHas {
 146  		return false
 147  	}
 148  	return a.NsOp100 < b.NsOp100
 149  }
 150  
 151  func unqualifiedName(name string) string {
 152  	for i := len(name) - 1; i >= 0; i-- {
 153  		if name[i] == '.' {
 154  			return name[i+1:]
 155  		}
 156  	}
 157  	return name
 158  }
 159