target.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (c) INRIA and Microsoft Corporation. All rights reserved.
  2. Licensed under the Apache 2.0 License. */
  3. #ifndef __KREMLIN_TARGET_H
  4. #define __KREMLIN_TARGET_H
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <stdbool.h>
  8. #include <inttypes.h>
  9. #include <limits.h>
  10. #include "kremlin/internal/callconv.h"
  11. /******************************************************************************/
  12. /* Macros that KreMLin will generate. */
  13. /******************************************************************************/
  14. /* For "bare" targets that do not have a C stdlib, the user might want to use
  15. * [-add-early-include '"mydefinitions.h"'] and override these. */
  16. #ifndef KRML_HOST_PRINTF
  17. # define KRML_HOST_PRINTF printf
  18. #endif
  19. #if ( \
  20. (defined __STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
  21. (!(defined KRML_HOST_EPRINTF)))
  22. # define KRML_HOST_EPRINTF(...) fprintf(stderr, __VA_ARGS__)
  23. #endif
  24. #ifndef KRML_HOST_EXIT
  25. # define KRML_HOST_EXIT exit
  26. #endif
  27. #ifndef KRML_HOST_MALLOC
  28. # define KRML_HOST_MALLOC malloc
  29. #endif
  30. #ifndef KRML_HOST_CALLOC
  31. # define KRML_HOST_CALLOC calloc
  32. #endif
  33. #ifndef KRML_HOST_FREE
  34. # define KRML_HOST_FREE free
  35. #endif
  36. #ifndef KRML_HOST_TIME
  37. # include <time.h>
  38. /* Prims_nat not yet in scope */
  39. inline static int32_t krml_time() {
  40. return (int32_t)time(NULL);
  41. }
  42. # define KRML_HOST_TIME krml_time
  43. #endif
  44. /* In statement position, exiting is easy. */
  45. #define KRML_EXIT \
  46. do { \
  47. KRML_HOST_PRINTF("Unimplemented function at %s:%d\n", __FILE__, __LINE__); \
  48. KRML_HOST_EXIT(254); \
  49. } while (0)
  50. /* In expression position, use the comma-operator and a malloc to return an
  51. * expression of the right size. KreMLin passes t as the parameter to the macro.
  52. */
  53. #define KRML_EABORT(t, msg) \
  54. (KRML_HOST_PRINTF("KreMLin abort at %s:%d\n%s\n", __FILE__, __LINE__, msg), \
  55. KRML_HOST_EXIT(255), *((t *)KRML_HOST_MALLOC(sizeof(t))))
  56. /* In FStar.Buffer.fst, the size of arrays is uint32_t, but it's a number of
  57. * *elements*. Do an ugly, run-time check (some of which KreMLin can eliminate).
  58. */
  59. #ifdef __GNUC__
  60. # define _KRML_CHECK_SIZE_PRAGMA \
  61. _Pragma("GCC diagnostic ignored \"-Wtype-limits\"")
  62. #else
  63. # define _KRML_CHECK_SIZE_PRAGMA
  64. #endif
  65. #define KRML_CHECK_SIZE(size_elt, sz) \
  66. do { \
  67. _KRML_CHECK_SIZE_PRAGMA \
  68. if (((size_t)(sz)) > ((size_t)(SIZE_MAX / (size_elt)))) { \
  69. KRML_HOST_PRINTF( \
  70. "Maximum allocatable size exceeded, aborting before overflow at " \
  71. "%s:%d\n", \
  72. __FILE__, __LINE__); \
  73. KRML_HOST_EXIT(253); \
  74. } \
  75. } while (0)
  76. #if defined(_MSC_VER) && _MSC_VER < 1900
  77. # define KRML_HOST_SNPRINTF(buf, sz, fmt, arg) _snprintf_s(buf, sz, _TRUNCATE, fmt, arg)
  78. #else
  79. # define KRML_HOST_SNPRINTF(buf, sz, fmt, arg) snprintf(buf, sz, fmt, arg)
  80. #endif
  81. #endif