Project

General

Profile

Revision 52c5ba00

View differences:

include/arrow.cpp
1
#include <stdlib.h>
2
#include "arrow.hpp"
3

  
4
struct _arrow_mem *_arrow_alloc() {
5
  struct _arrow_mem *_alloc;
6
  _alloc = (struct _arrow_mem *) malloc(sizeof(struct _arrow_mem *));
7
  assert (_alloc);
8
  return _alloc;
9
}
include/arrow.hpp
1

  
2
#ifndef _ARROW_CPP
3
#define _ARROW_CPP
4

  
5
#include <stdlib.h>
6

  
7
struct _arrow_mem {
8
	struct _arrow_reg {
9
		bool _first;
10
	} _reg; 
11
};
12

  
13
extern struct _arrow_mem *_arrow_alloc ();
14

  
15
#define _arrow_DECLARE(attr, inst)\
16
  attr struct _arrow_mem inst;
17
  
18
#define _arrow_LINK(inst) do {\
19
  ;\
20
} while (0)
21

  
22
#define _arrow_ALLOC(attr, inst)\
23
  _arrow_DECLARE(attr, inst);\
24
  _arrow_LINK(inst)
25

  
26
#define _arrow_init(self) {}
27

  
28
#define _arrow_clear(self) {}
29

  
30
#define _arrow_step(x,y,output,self) ((self)->_reg._first?((self)->_reg._first=0,(*output = x)):(*output = y))
31

  
32
#define _arrow_reset(self) {(self)->_reg._first = 1;}
33

  
34
/* Step macro for specialized arrows of the form: (true -> false) */
35

  
36
#define _once_step(output,self) { *output = (self)->_reg._first; if ((self)->_reg._first) { (self)->_reg._first=0; }; }
37

  
38
#endif
include/io_frontend.hpp
1
#ifndef _IO_FRONTEND_HPP
2
#define _IO_FRONTEND_HPP
3

  
4
#include <stdlib.h> /* Provides exit */
5
#include <stdio.h> /* Provides printf, scanf, sscanf */
6
#include <unistd.h> /* Provides isatty */
7

  
8
int ISATTY;
9

  
10
/* Standard Input procedures **************/
11
bool _get_bool(FILE* file, char* n){
12
   char b[512];
13
   bool r = 0;
14
   int s = 1;
15
   char c;
16
   do {
17
      if(ISATTY) {
18
         if((s != 1)||(r == -1)) printf("\a");
19
         printf("%s (1,t,T/0,f,F) ? ", n);
20
      }
21
      if(scanf("%s", b)==EOF) exit(0);
22
      s = sscanf(b, "%c", &c);
23
      r = -1;
24
      if((c == '0') || (c == 'f') || (c == 'F')) r = 0;
25
      if((c == '1') || (c == 't') || (c == 'T')) r = 1;
26
   } while((s != 1) || (r == -1));
27
   fprintf(file, "%i\n",r);
28
   return r;
29
}
30

  
31
int _get_int(FILE* file, char* n){
32
   char b[512];
33
   int r;
34
   int s = 1;
35
   do {
36
      if(ISATTY) {
37
         if(s != 1) printf("\a");
38
         printf("%s (integer) ? ", n);
39
      }
40
      if(scanf("%s", b)==EOF) exit(0);
41
      s = sscanf(b, "%d", &r);
42
   } while(s != 1);
43
   fprintf(file, "%d\n", r);
44
   return r;
45
}
46

  
47
double _get_double(FILE* file, char* n){
48
   char b[512];
49
   double r;
50
   int s = 1;
51
   do {
52
      if(ISATTY) {
53
         if(s != 1) printf("\a");
54
         printf("%s (double) ? ", n);
55
      }
56
      if(scanf("%s", b)==EOF) exit(0);
57
      s = sscanf(b, "%lf", &r);
58
   } while(s != 1);
59
   fprintf(file, "%f\n", r);
60
   return r;
61
}
62
/* Standard Output procedures **************/
63
void _put_bool(FILE* file, char* n, bool _V){
64
  if(ISATTY) {
65
    printf("%s = ", n);
66
  } else {
67
    printf("'%s': ", n);
68
  };
69
  printf("'%i' ", (_V)? 1 : 0);
70
  printf("\n");
71
  fprintf(file, "%i\n", _V);
72
}
73
void _put_int(FILE* file, char* n, int _V){
74
  if(ISATTY) {
75
    printf("%s = ", n);
76
  } else {
77
    printf("'%s': ", n);
78
  };
79
  printf("'%d' ", _V);
80
  printf("\n");
81
  fprintf(file, "%d\n", _V);
82
}
83
void _put_double(FILE* file, char* n, double _V){
84
  if(ISATTY) {
85
    printf("%s = ", n);
86
  } else {
87
    printf("'%s': ", n);
88
  };
89
  printf("'%f' ", _V);
90
  printf("\n");
91
  fprintf(file, "%f\n", _V);
92
}
93

  
94
#endif
src/Makefile
1 1
OCAMLBUILD=/usr/bin/ocamlbuild -classic-display -use-ocamlfind -no-links 
2 2

  
3
prefix=/home/ploc/Local
3
prefix=/usr/local
4 4
exec_prefix=${prefix}
5 5
bindir=${exec_prefix}/bin
6 6
datarootdir = ${prefix}/share
src/backends/C/c_backend.ml
38 38
  close_out header_out;
39 39
  
40 40
  (* Generating Lib C file *)
41
  let source_lib_file = destname ^ ".c" in (* Could be changed *)
41
  let source_lib_file = (if !Options.cpp then destname ^ ".cpp" else destname ^ ".c") in (* Could be changed *)
42 42
  let source_lib_out = open_out source_lib_file in
43 43
  let source_lib_fmt = formatter_of_out_channel source_lib_out in
44 44
  print_lib_c source_lib_fmt basename prog machines dependencies;
......
54 54
      raise (Corelang.Error (Location.dummy_loc, LustreSpec.Main_not_found))
55 55
    end
56 56
    | Some m -> begin
57
      let source_main_file = destname ^ "_main.c" in (* Could be changed *)
57
      let source_main_file = (if !Options.cpp then destname ^ "_main.cpp" else destname ^ "_main.c") in (* Could be changed *)
58 58
      let source_main_out = open_out source_main_file in
59 59
      let source_main_fmt = formatter_of_out_channel source_main_out in
60 60

  
src/backends/C/c_backend_common.ml
124 124
  | Types.Tbool | Types.Treal | Types.Tint  -> true
125 125
  | _                                       -> false
126 126

  
127
let pp_basic_c_type fmt t =
128
  match (Types.repr t).Types.tdesc with
129
  | Types.Tbool                    -> fprintf fmt "_Bool"
130
  | Types.Treal when !Options.mpfr -> fprintf fmt "%s" Mpfr.mpfr_t
131
  | Types.Treal                    -> fprintf fmt "double"
132
  | Types.Tint                     -> fprintf fmt "int"
127
let pp_c_basic_type_desc t_dsec =
128
  match (t_dsec) with
129
  | Types.Tbool when !Options.cpp  -> "bool"
130
  | Types.Tbool                    -> "_Bool"
131
  | Types.Tint                     -> !Options.int_type
132
  | Types.Treal when !Options.mpfr -> Mpfr.mpfr_t
133
  | Types.Treal                    -> !Options.real_type
133 134
  | _ -> assert false (* Not a basic C type. Do not handle arrays or pointers *)
134 135

  
136
let pp_basic_c_type fmt t = fprintf fmt "%s" (pp_c_basic_type_desc (Types.repr t).Types.tdesc)
137

  
135 138
let pp_c_type var fmt t =
136 139
  let rec aux t pp_suffix =
137 140
    match (Types.repr t).Types.tdesc with
src/backends/C/c_backend_header.ml
39 39
      begin
40 40
	fprintf fmt "#include <mpfr.h>@."
41 41
      end;
42
  fprintf fmt "#include \"%s/arrow.h\"@.@." !Options.include_dir
42
  if !Options.cpp then
43
    fprintf fmt "#include \"%s/arrow.hpp\"@.@." !Options.include_dir
44
  else
45
    fprintf fmt "#include \"%s/arrow.h\"@.@." !Options.include_dir
43 46

  
44 47
  end
45 48

  
src/backends/C/c_backend_main.ml
179 179
  fprintf fmt "@]@ }@."       
180 180

  
181 181
let print_main_header fmt =
182
  fprintf fmt "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/io_frontend.h\"@."
182
  fprintf fmt (if !Options.cpp then "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/io_frontend.hpp\"@." else "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/io_frontend.h\"@.")
183 183
    !Options.include_dir
184 184

  
185 185
let print_main_c main_fmt main_machine basename prog machines _ (*dependencies*) =
src/backends/C/c_backend_src.ml
588 588
let print_global_init_code fmt basename prog dependencies =
589 589
  let baseNAME = file_to_module_name basename in
590 590
  let constants = List.map const_of_top (get_consts prog) in
591
  fprintf fmt "@[<v 2>%a {@,static _Bool init = 0;@,@[<v 2>if (!init) { @,init = 1;@,%a%t%a@]@,}@,return;@]@,}@.@."
591
  fprintf fmt "@[<v 2>%a {@,static %s init = 0;@,@[<v 2>if (!init) { @,init = 1;@,%a%t%a@]@,}@,return;@]@,}@.@."
592 592
    print_global_init_prototype baseNAME
593
    (pp_c_basic_type_desc Types.Tbool)
593 594
    (* constants *) 
594 595
    (Utils.fprintf_list ~sep:"@," (pp_const_initialize (pp_c_var_read Machine_code.empty_machine))) constants
595 596
    (Utils.pp_final_char_if_non_empty "@," dependencies)
......
599 600
let print_global_clear_code  fmt basename prog dependencies =
600 601
  let baseNAME = file_to_module_name basename in
601 602
  let constants = List.map const_of_top (get_consts prog) in
602
  fprintf fmt "@[<v 2>%a {@,static _Bool clear = 0;@,@[<v 2>if (!clear) { @,clear = 1;@,%a%t%a@]@,}@,return;@]@,}@.@."
603
  fprintf fmt "@[<v 2>%a {@,static %s clear = 0;@,@[<v 2>if (!clear) { @,clear = 1;@,%a%t%a@]@,}@,return;@]@,}@.@."
603 604
    print_global_clear_prototype baseNAME
605
    (pp_c_basic_type_desc Types.Tbool)
604 606
    (* constants *) 
605 607
    (Utils.fprintf_list ~sep:"@," (pp_const_clear (pp_c_var_read Machine_code.empty_machine))) constants
606 608
    (Utils.pp_final_char_if_non_empty "@," dependencies)
src/options.ml
51 51

  
52 52
let salsa_enabled = ref true
53 53

  
54
let cpp       = ref false
55
let int_type  = ref "int"
56
let real_type = ref "double"
57

  
54 58
let sfunction = ref ""
55 59

  
56 60
let set_mpfr prec =
......
89 93
    "-print_clocks", Arg.Set print_clocks, "prints node clocks";
90 94
    "-O", Arg.Set_int optimization, "changes optimization \x1b[4mlevel\x1b[0m <default: 2>";
91 95
    "-verbose", Arg.Set_int verbose_level, "changes verbose \x1b[4mlevel\x1b[0m <default: 1>";
92
    "-version", Arg.Unit print_version, " displays the version";]
96
    "-version", Arg.Unit print_version, " displays the version";
97

  
98
    "-c++" , Arg.Set        cpp      , "c++ backend";
99
    "-int" , Arg.Set_string int_type , "specifies the integer type (default=\"int\")";
100
    "-real", Arg.Set_string real_type, "specifies the real type (default=\"double\" without mpfr option)";
101
]
93 102

  
94 103

  
95 104
let plugin_opt (name, activate, options) =

Also available in: Unified diff