index-foreach.smpl 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // Copyright © 2019 Ariadne Devos
  3. // sHT -- introduce sHT_index_iterate
  4. // sHT_index_iterate does index speculative index clipping itself
  5. // (see Spectre), reduces work for formal analysis (is the correct
  6. // comparison operator used? Is the index an integer? Is it clipped?),
  7. // centralises optimisation decisions (branch predicition information),
  8. // and is informative.
  9. @ abstract_index_iterate0 @
  10. identifier i;
  11. expression n;
  12. iterator name sHT_index_iterate;
  13. expression a;
  14. type T;
  15. @@
  16. (
  17. - for (i = 0; \(i < n\|sHT_gt(n, i)\|!sHT_ge(i, n)\); i++)
  18. + sHT_index_iterate(i, n)
  19. {
  20. (
  21. ... sHT_index_nospec(i, n) ...
  22. |
  23. ... a[i] ...
  24. )
  25. }
  26. |
  27. + size_t i;
  28. - for (T i = 0; \(i < n\|sHT_gt(n, i)\|!sHT_ge(i, n)\); i++)
  29. + sHT_index_iterate(i, n)
  30. {
  31. (
  32. ... sHT_index_nospec(i, n) ...
  33. |
  34. ... a[i] ...
  35. )
  36. }
  37. )
  38. @ no_clip_assign @
  39. identifier i;
  40. expression n;
  41. @@
  42. sHT_index_iterate(i, n) {
  43. ...
  44. - i = sHT_index_nospec(i, n);
  45. ...
  46. }
  47. @ has_header @
  48. @@
  49. #include <sHT/index.h>
  50. @ use_index @
  51. statement S;
  52. @@
  53. sHT_index_iterate(...)
  54. S
  55. @ add_header depends on use_index && !has_header @
  56. @@
  57. #include <sHT/...>
  58. + #include <sHT/index.h>