create_ladder_graph.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. from __future__ import print_function
  11. import argparse
  12. def main():
  13. parser = argparse.ArgumentParser(description=__doc__)
  14. parser.add_argument('rungs', type=int,
  15. help="Number of ladder rungs. Must be a multiple of 2")
  16. args = parser.parse_args()
  17. if (args.rungs % 2) != 0:
  18. print("Rungs must be a multiple of 2")
  19. return
  20. print("int ladder(int *foo, int *bar, int x) {")
  21. rung1 = range(0, args.rungs, 2)
  22. rung2 = range(1, args.rungs, 2)
  23. for i in rung1:
  24. print("rung1%d:" % i)
  25. print("*foo = x++;")
  26. if i != rung1[-1]:
  27. print("if (*bar) goto rung1%d;" % (i+2))
  28. print("else goto rung2%d;" % (i+1))
  29. else:
  30. print("goto rung2%d;" % (i+1))
  31. for i in rung2:
  32. print("rung2%d:" % i)
  33. print("*foo = x++;")
  34. if i != rung2[-1]:
  35. print("goto rung2%d;" % (i+2))
  36. else:
  37. print("return *foo;")
  38. print("}")
  39. if __name__ == '__main__':
  40. main()