Advertisement
chemoelectric

Inline ATS support for Pure

Oct 12th, 2014
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. diff --git a/pure/examples/bitcode/inline_ats_examp.pure b/pure/examples/bitcode/inline_ats_examp.pure
  2. new file mode 100644
  3. --- /dev/null
  4. +++ b/pure/examples/bitcode/inline_ats_examp.pure
  5. @@ -0,0 +1,56 @@
  6. +
  7. +/* Example of inline code written in ATS. */
  8. +
  9. +%< -*- ATS -*-
  10. +
  11. +// A factorial function that verifies its result is the factorial;
  12. +// based on an example given in ‘Introduction to Programming in ATS’
  13. +// (which is available at http://www.ats-lang.org).
  14. +//
  15. +// The ifact function returns both the factorial and a proof, but the
  16. +// proof code is ‘erased’ after type-checking, so the Pure program
  17. +// sees only a function returning the factorial.
  18. +
  19. +#include "share/atspre_staload.hats"
  20. +
  21. +// A recursive definition of the factorial relation.
  22. +// FACT (n, r) means ‘the factorial of n equals r’.
  23. +// MUL (i, j, k) means ‘the product of i and j equals k’.
  24. +dataprop FACT (int, int) =
  25. + | FACT_base (0, 1)
  26. + | {n : nat} {r1, r : int}
  27. + FACT_induction (n, r) of
  28. + (FACT (n-1, r1), MUL (n, r1, r))
  29. +
  30. +// Declare ifact as an ATS function that is referred to in C by the
  31. +// name ‘ifact_nonneg’. The ‘ifact_nonneg’ function returns an integer
  32. +// equal to r, where r satisfies the relation FACT (n, r).
  33. +extern fun ifact :
  34. + {n : nat} (int n) -<> [r : int] (FACT (n, r) | int r) =
  35. + "ext#ifact_nonneg"
  36. +
  37. +implement ifact (n) =
  38. + let
  39. + fun fact {n : nat} .<n>. (n : int n) :<>
  40. + [r : int] (FACT (n, r) | int r) =
  41. + begin
  42. + if n > 0 then
  43. + let
  44. + val (pf1 | r1) = ifact (n - 1)
  45. + val (pfmul | r) = g1int_mul2 (n, r1)
  46. + in
  47. + (FACT_induction (pf1, pfmul) | r)
  48. + end
  49. + else
  50. + (FACT_base () | 1)
  51. + end
  52. + in
  53. + fact n
  54. + end
  55. +
  56. +%>
  57. +
  58. +ifact n::int = ifact_nonneg n if 0 <= n;
  59. +
  60. +map ifact (0..9);
  61. +
  62. diff --git a/pure/interpreter.cc b/pure/interpreter.cc
  63. --- a/pure/interpreter.cc
  64. +++ b/pure/interpreter.cc
  65. @@ -2570,6 +2570,9 @@
  66. standards). If this is omitted, the default is old-style (fixed form)
  67. Fortran.
  68.  
  69. + - "-*- ats -*-" selects ATS (PURE_ATSCC environment variable,
  70. + patscc -fplugin=dragonegg by default).
  71. +
  72. - "-*- dsp:name -*-" selects Faust (PURE_FAUST environment variable, faust
  73. by default), where 'name' denotes the name of the Faust dsp, which is
  74. used as the namespace for the dsp interface functions.
  75. @@ -2633,8 +2636,10 @@
  76. {
  77. // Get the language tag and configure accordingly.
  78. string modname, tag = lang_tag(code, modname), ext = "", optargs = "";
  79. + string intermediate_ext = "";
  80. const char *env, *drv, *args;
  81. char *asmargs = 0;
  82. + bool remove_intermediate = false;
  83. if (tag == "c") {
  84. env = "PURE_CC";
  85. drv = "clang";
  86. @@ -2652,6 +2657,15 @@
  87. // gfortran doesn't understand -x, so we have to do some trickery with
  88. // filename extensions instead.
  89. args = " -emit-llvm -c "; ext = ".f"+std;
  90. + } else if (tag == "ats") {
  91. + env = "PURE_ATSCC";
  92. + // The default command is for ATS2-Postiats with gcc as the C
  93. + // compiler.
  94. + drv = "patscc -fplugin=dragonegg";
  95. + args = " -emit-llvm -c ";
  96. + ext = ".dats";
  97. + intermediate_ext = "_dats.c";
  98. + remove_intermediate = true;
  99. } else if (tag == "dsp") {
  100. env = "PURE_FAUST"; drv = "faust -double";
  101. args = " -lang llvm ";
  102. @@ -2729,6 +2743,10 @@
  103. if (vflag) std::cerr << cmd << '\n';
  104. int status = system(cmd.c_str());
  105. unlink(nm.c_str());
  106. + if (remove_intermediate) {
  107. + string intermediate_name = string(fnm)+intermediate_ext;
  108. + unlink(intermediate_name.c_str());
  109. + }
  110. if (asmargs) free(asmargs);
  111. if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
  112. if (ext == ".ll") {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement