github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/src/cmd/compile/internal/gc/aghosn.go (about)

     1  package gc
     2  
     3  import (
     4  	"cmd/compile/internal/types"
     5  	"fmt"
     6  )
     7  
     8  type Loc int
     9  
    10  const (
    11  	Left Loc = iota
    12  	Right
    13  	Body
    14  	Top
    15  )
    16  
    17  //For stringer
    18  func (l Loc) String() string {
    19  	switch l {
    20  	case Left:
    21  		return "Left"
    22  	case Right:
    23  		return "Right"
    24  	case Body:
    25  		return "Body"
    26  	case Top:
    27  		return "Top"
    28  	default:
    29  		return "UNKNOWN"
    30  	}
    31  }
    32  
    33  type Pair struct {
    34  	l Loc
    35  	n *Node
    36  }
    37  
    38  func dumpEverything(n *Node) {
    39  	s := fmt.Sprintf("left: %v, right: %v, Ninit: %v, Nbody: %v, ", n.Left, n.Right, n.Ninit, n.Nbody)
    40  	s += fmt.Sprintf("List: %v, RList: %v\n", n.List, n.Rlist)
    41  	s += fmt.Sprintf("Type: %v, Orig: %v, Func; %v, Name: %v\n", n.Type, n.Orig, n.Func, n.Name)
    42  	s += fmt.Sprintf("Etype: %v, Op: %v\n", n.Etype, n.Op)
    43  
    44  	fmt.Println(s)
    45  }
    46  
    47  func (p Pair) String() string {
    48  	//s := fmt.Sprintf("(%v) %v, type: %v, name: %v, Op: %v\n", p.l, p.n, p.n.Type, p.n.funcname(), p.n.Op)
    49  	s := fmt.Sprintf("(%v) %+v\n", p.l, p.n)
    50  	return s
    51  }
    52  
    53  func walkGosecure(n *Node, path *[]*Pair) bool {
    54  	if n == nil {
    55  		return false
    56  	}
    57  	if n.Op == OGOSECURE {
    58  		if len(*path) != 0 {
    59  			*path = append([]*Pair{{Left, n.Left}}, *path...)
    60  		} else {
    61  			*path = []*Pair{{Left, n.Left}}
    62  		}
    63  		if n.Left == nil || n.Left.Op != OCALLFUNC {
    64  			panic("Not a call functiona argument")
    65  		}
    66  
    67  		if n.Left.Left == nil || n.Left.Left.Op != ONAME {
    68  			panic("Don't have the name for the call.")
    69  		}
    70  
    71  		return true
    72  	}
    73  
    74  	if walkGosecure(n.Left, path) {
    75  		*path = append([]*Pair{{Left, n.Left}}, *path...)
    76  		return true
    77  	}
    78  
    79  	if walkGosecure(n.Right, path) {
    80  		*path = append([]*Pair{{Right, n.Left}}, *path...)
    81  		return true
    82  	}
    83  
    84  	for _, b := range n.Nbody.Slice() {
    85  		if walkGosecure(b, path) {
    86  			*path = append([]*Pair{{Body, b}}, *path...)
    87  			return true
    88  		}
    89  	}
    90  
    91  	return false
    92  }
    93  
    94  func printPath(p []*Pair) {
    95  	for i, n := range p {
    96  		fmt.Printf("%v\n", n)
    97  		if i == len(p)-1 {
    98  			if n.n.Op != OCALLFUNC {
    99  				panic("I'm not looking at the correct entry.")
   100  			}
   101  			//what is left of the occalfunc.
   102  			if n.n.Left == nil {
   103  				panic("Left of OCALLFUNC is null.")
   104  			}
   105  
   106  			//What is left of left.
   107  			n1 := n.n.Left
   108  
   109  			//The function declaration.
   110  			decl := n1.Name.Defn
   111  
   112  			//TODO aghosn: check the Func Inldcl how it is generated.
   113  			//Can actually highjack inlining mech to do the copy.
   114  			fmt.Printf("The func arg %+v\n", decl.Func)
   115  		}
   116  	}
   117  }
   118  
   119  func PrintLast(path []*Pair) {
   120  	n := path[len(path)-1]
   121  	if n == nil {
   122  		panic("The last element is nil!")
   123  	}
   124  	fmt.Printf("The original node %+v\n", n)
   125  	oname := n.n.Left.Name
   126  	if oname == nil {
   127  		panic("The name is null.")
   128  	}
   129  	//fmt.Printf("The address of defn (%p): %+v", oname.Defn, oname)
   130  
   131  	decl := oname.Defn
   132  	if decl == nil {
   133  		fmt.Printf("%v\n", oname)
   134  		panic("The declaration is null.")
   135  	}
   136  	//TODO aghosn: check the Func Inldcl how it is generated.
   137  	//Can actually highjack inlining mech to do the copy.
   138  	//fmt.Printf("The func arg %+v\n", decl.Func)
   139  	//fmt.Printf("The name: %+v\n", decl.Func.Nname)
   140  	//fmt.Printf("The original: %+v\n\n", decl)
   141  
   142  	//TODO try to copy the shit out of the node.
   143  	ncpy := inlcopy(decl)
   144  
   145  	fmt.Printf("The oname: %v\n", *oname)
   146  	fmt.Printf("The copy: %+v\n", ncpy)
   147  	//fmt.Printf("The ninit and list %v, %v\n", ncpy.Ninit, ncpy.List)
   148  }
   149  
   150  func walking(n *Node, cond func(n *Node) bool, act func(n *Node)) {
   151  	if n == nil {
   152  		return
   153  	}
   154  
   155  	if cond(n) {
   156  		act(n)
   157  	}
   158  
   159  	walking(n.Left, cond, act)
   160  	walking(n.Right, cond, act)
   161  	for _, e := range n.Ninit.Slice() {
   162  		walking(e, cond, act)
   163  	}
   164  	for _, e := range n.Nbody.Slice() {
   165  		walking(e, cond, act)
   166  	}
   167  	for _, e := range n.List.Slice() {
   168  		walking(e, cond, act)
   169  	}
   170  	for _, e := range n.Rlist.Slice() {
   171  		walking(e, cond, act)
   172  	}
   173  }
   174  
   175  func conditionCALL(n *Node) bool {
   176  	if n == nil {
   177  		return false
   178  	}
   179  	return n.Op == OCALLFUNC
   180  }
   181  
   182  func actionDumpLCR(n *Node) {
   183  	fmt.Printf("%v:\n", n)
   184  	fmt.Printf("left: %v,right: %v\n", n.Left, n.Right)
   185  	//if n.Left != nil {
   186  	//	fmt.Printf("Op left: %v\n", n.Left.Op)
   187  	//	oname := n.Left.Name
   188  	//	if oname != nil {
   189  	//		definition := oname.Defn
   190  	//		if definition == nil {
   191  	//			fmt.Println("definition cannot be resolved for this one.")
   192  	//		} else {
   193  	//			fmt.Printf("definition is %v\n", definition)
   194  	//		}
   195  	//	}
   196  	//
   197  	//}
   198  }
   199  
   200  func conditionLiteral(n *Node) bool {
   201  	if n == nil {
   202  		return false
   203  	}
   204  
   205  	return n.Op == OLITERAL
   206  }
   207  
   208  func ff(cond bool, s string) {
   209  	if !cond {
   210  		panic(s)
   211  	}
   212  }
   213  
   214  func actionFindDef(n *Node) {
   215  	if n == nil || n.Left == nil || n.Left.Name == nil {
   216  		return
   217  	}
   218  
   219  	oname := n.Left.Name
   220  	def := oname.Defn
   221  	if def == nil {
   222  		fmt.Printf("Definition unavailable for %v\n", tostring(n.Sym))
   223  		return //TODO finish this after the course.
   224  	}
   225  	fmt.Println("------------------------------")
   226  	fmt.Println(tostring(def.Sym))
   227  
   228  }
   229  
   230  func printImports() {
   231  	fmt.Println("Printing the imports")
   232  	imports := types.ImportedPkgList()
   233  	for _, i := range imports {
   234  		fmt.Println(i.Path)
   235  	}
   236  }
   237  
   238  func tostring(s *types.Sym) string {
   239  	if s == nil {
   240  		return "<nil>"
   241  	}
   242  
   243  	return fmt.Sprintf("Name: %v, Linkname: %v", s.Name, s.Linkname)
   244  }
   245  
   246  func aghosnInspect(ttop []*Node) {
   247  	for _, n := range ttop {
   248  		walking(n, conditionCALL, actionFindDef)
   249  	}
   250  
   251  }
   252  
   253  func findGoSecure(ttop []*Node) {
   254  	for _, n := range ttop {
   255  		path := make([]*Pair, 0, 1)
   256  		if walkGosecure(n, &path) {
   257  			path = append([]*Pair{{Top, n}}, path...)
   258  			PrintLast(path)
   259  		}
   260  	}
   261  }
   262  
   263  // Saving old code.
   264  
   265  // getFnDecl returns the callee corresponding to a function call.
   266  //func getdFnDecl(n *Node) *Node {
   267  //	if n.Left == nil || n.Left.Op != OCALLFUNC {
   268  //		panic("GOSECURE: Not a call function argument.")
   269  //	}
   270  //	if n.Left.Left == nil || n.Left.Left.Op != ONAME {
   271  //		panic("GOSECURE: Missing name for the gosecure callee.")
   272  //	}
   273  //	oname := n.Left.Name
   274  //	if oname == nil || oname.Defn == nil {
   275  //		panic("GOSECURE: Name or Defn node is nul.")
   276  //	}
   277  //	return oname.Defn
   278  //}
   279  //
   280  //func walkerList(n Nodes, res *[]*Node) {
   281  //	for _, b := range n.Slice() {
   282  //		walker(b, res)
   283  //	}
   284  //}
   285  //
   286  //// walker walks an AST node and finds the gosecure calls.
   287  //// It appends a copy of the declaration nodes corresponding to the callee
   288  //// of the gosecure calls to the res slice.
   289  ////TODO aghosn should handle duplicates.
   290  //func walker(n *Node, res *[]*Node) {
   291  //	if n == nil {
   292  //		return
   293  //	}
   294  //	//Found a gosecure call.
   295  //	if n.Op == OGOSECURE {
   296  //		fnDecl := getdFnDecl(n)
   297  //		*res = append(*res, getCopy(fnDecl))
   298  //		return
   299  //	}
   300  //
   301  //	walker(n.Left, res)
   302  //	walkerList(n.Ninit, res)
   303  //	walkerList(n.Nbody, res)
   304  //	walkerList(n.List, res)
   305  //	walkerList(n.Rlist, res)
   306  //	walker(n.Right, res)
   307  //}
   308  
   309  //func dumpAllNodeFields(n *Node) {
   310  //	//fmt.Printf("%+v\n", n.Left)
   311  //	//fmt.Println(". . . . . . . .")
   312  //	//fmt.Printf("%+v\n", n.Right)
   313  //	//fmt.Println(". . . . . . . .")
   314  //	//fmt.Printf("%+v\n", n.Ninit)
   315  //	//fmt.Println(". . . . . . . .")
   316  //	//fmt.Printf("%+v\n", n.Nbody)
   317  //	//fmt.Println(". . . . . . . .")
   318  //	//fmt.Printf("%+v\n", n.List)
   319  //	//fmt.Println(". . . . . . . .")
   320  //	//fmt.Printf("%+v\n", n.List)
   321  //	//fmt.Println(". . . . . . . .")
   322  //	//fmt.Printf("%+v\n", n.Rlist)
   323  //	//fmt.Println(". . . . . . . .")
   324  //	//fmt.Printf("%+v\n", n.Type)
   325  //	//fmt.Println(". . . . . . . .")
   326  //	//fmt.Printf("%+v\n", n.Orig)
   327  //	//fmt.Println(". . . . . . . .")
   328  //	//fmt.Printf("%+v\n", n.Func)
   329  //	fmt.Println("1. . . . . . . .")
   330  //	fmt.Printf("%+v\n", n.Name)
   331  //	fmt.Println("2. . . . . . . .")
   332  //	fmt.Printf("%+v\n", n.Sym)
   333  //	//	fmt.Println(". . . . . . . .")
   334  //	//	fmt.Printf("%+v\n", n.E)
   335  //	//	fmt.Println(". . . . . . . .")
   336  //	//	fmt.Printf("%+v\n", n.Xoffset)
   337  //	//	fmt.Println(". . . . . . . .")
   338  //	//	fmt.Printf("%+v\n", n.Pos)
   339  //	//	fmt.Println(". . . . . . . .")
   340  //	//	fmt.Printf("%+v\n", n.flags)
   341  //	//	fmt.Println(". . . . . . . .")
   342  //	//	fmt.Printf("%+v\n", n.Esc)
   343  //	//	fmt.Println(". . . . . . . .")
   344  //	//	fmt.Printf("%+v\n", n.Op)
   345  //	//	fmt.Println(". . . . . . . .")
   346  //	//	fmt.Printf("%+v\n", n.Etype)
   347  //}