create_ladder_graph.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env python
  2. """A ladder graph creation program.
  3. This is a python program that creates c source code that will generate
  4. CFGs that are ladder graphs. Ladder graphs are generally the worst case
  5. for a lot of dominance related algorithms (Dominance frontiers, etc),
  6. and often generate N^2 or worse behavior.
  7. One good use of this program is to test whether your linear time algorithm is
  8. really behaving linearly.
  9. """
  10. import argparse
  11. def main():
  12. parser = argparse.ArgumentParser(description=__doc__)
  13. parser.add_argument('rungs', type=int,
  14. help="Number of ladder rungs. Must be a multiple of 2")
  15. args = parser.parse_args()
  16. if (args.rungs % 2) != 0:
  17. print "Rungs must be a multiple of 2"
  18. return
  19. print "int ladder(int *foo, int *bar, int x) {"
  20. rung1 = xrange(0, args.rungs, 2)
  21. rung2 = xrange(1, args.rungs, 2)
  22. for i in rung1:
  23. print "rung1%d:" % i
  24. print "*foo = x++;"
  25. if i != rung1[-1]:
  26. print "if (*bar) goto rung1%d;" % (i+2)
  27. print "else goto rung2%d;" % (i+1)
  28. else:
  29. print "goto rung2%d;" % (i+1)
  30. for i in rung2:
  31. print "rung2%d:" % i
  32. print "*foo = x++;"
  33. if i != rung2[-1]:
  34. print "goto rung2%d;" % (i+2)
  35. else:
  36. print "return *foo;"
  37. print "}"
  38. if __name__ == '__main__':
  39. main()