Skip to content
Snippets Groups Projects
Commit 52c5ba00 authored by David Doose's avatar David Doose
Browse files

adding c++ backend

parent e49b6d55
No related branches found
No related tags found
No related merge requests found
#include <stdlib.h>
#include "arrow.hpp"
struct _arrow_mem *_arrow_alloc() {
struct _arrow_mem *_alloc;
_alloc = (struct _arrow_mem *) malloc(sizeof(struct _arrow_mem *));
assert (_alloc);
return _alloc;
}
#ifndef _ARROW_CPP
#define _ARROW_CPP
#include <stdlib.h>
struct _arrow_mem {
struct _arrow_reg {
bool _first;
} _reg;
};
extern struct _arrow_mem *_arrow_alloc ();
#define _arrow_DECLARE(attr, inst)\
attr struct _arrow_mem inst;
#define _arrow_LINK(inst) do {\
;\
} while (0)
#define _arrow_ALLOC(attr, inst)\
_arrow_DECLARE(attr, inst);\
_arrow_LINK(inst)
#define _arrow_init(self) {}
#define _arrow_clear(self) {}
#define _arrow_step(x,y,output,self) ((self)->_reg._first?((self)->_reg._first=0,(*output = x)):(*output = y))
#define _arrow_reset(self) {(self)->_reg._first = 1;}
/* Step macro for specialized arrows of the form: (true -> false) */
#define _once_step(output,self) { *output = (self)->_reg._first; if ((self)->_reg._first) { (self)->_reg._first=0; }; }
#endif
#ifndef _IO_FRONTEND_HPP
#define _IO_FRONTEND_HPP
#include <stdlib.h> /* Provides exit */
#include <stdio.h> /* Provides printf, scanf, sscanf */
#include <unistd.h> /* Provides isatty */
int ISATTY;
/* Standard Input procedures **************/
bool _get_bool(FILE* file, char* n){
char b[512];
bool r = 0;
int s = 1;
char c;
do {
if(ISATTY) {
if((s != 1)||(r == -1)) printf("\a");
printf("%s (1,t,T/0,f,F) ? ", n);
}
if(scanf("%s", b)==EOF) exit(0);
s = sscanf(b, "%c", &c);
r = -1;
if((c == '0') || (c == 'f') || (c == 'F')) r = 0;
if((c == '1') || (c == 't') || (c == 'T')) r = 1;
} while((s != 1) || (r == -1));
fprintf(file, "%i\n",r);
return r;
}
int _get_int(FILE* file, char* n){
char b[512];
int r;
int s = 1;
do {
if(ISATTY) {
if(s != 1) printf("\a");
printf("%s (integer) ? ", n);
}
if(scanf("%s", b)==EOF) exit(0);
s = sscanf(b, "%d", &r);
} while(s != 1);
fprintf(file, "%d\n", r);
return r;
}
double _get_double(FILE* file, char* n){
char b[512];
double r;
int s = 1;
do {
if(ISATTY) {
if(s != 1) printf("\a");
printf("%s (double) ? ", n);
}
if(scanf("%s", b)==EOF) exit(0);
s = sscanf(b, "%lf", &r);
} while(s != 1);
fprintf(file, "%f\n", r);
return r;
}
/* Standard Output procedures **************/
void _put_bool(FILE* file, char* n, bool _V){
if(ISATTY) {
printf("%s = ", n);
} else {
printf("'%s': ", n);
};
printf("'%i' ", (_V)? 1 : 0);
printf("\n");
fprintf(file, "%i\n", _V);
}
void _put_int(FILE* file, char* n, int _V){
if(ISATTY) {
printf("%s = ", n);
} else {
printf("'%s': ", n);
};
printf("'%d' ", _V);
printf("\n");
fprintf(file, "%d\n", _V);
}
void _put_double(FILE* file, char* n, double _V){
if(ISATTY) {
printf("%s = ", n);
} else {
printf("'%s': ", n);
};
printf("'%f' ", _V);
printf("\n");
fprintf(file, "%f\n", _V);
}
#endif
OCAMLBUILD=/usr/bin/ocamlbuild -classic-display -use-ocamlfind -no-links OCAMLBUILD=/usr/bin/ocamlbuild -classic-display -use-ocamlfind -no-links
prefix=/home/ploc/Local prefix=/usr/local
exec_prefix=${prefix} exec_prefix=${prefix}
bindir=${exec_prefix}/bin bindir=${exec_prefix}/bin
datarootdir = ${prefix}/share datarootdir = ${prefix}/share
......
...@@ -38,7 +38,7 @@ let gen_files funs basename prog machines dependencies = ...@@ -38,7 +38,7 @@ let gen_files funs basename prog machines dependencies =
close_out header_out; close_out header_out;
(* Generating Lib C file *) (* Generating Lib C file *)
let source_lib_file = destname ^ ".c" in (* Could be changed *) let source_lib_file = (if !Options.cpp then destname ^ ".cpp" else destname ^ ".c") in (* Could be changed *)
let source_lib_out = open_out source_lib_file in let source_lib_out = open_out source_lib_file in
let source_lib_fmt = formatter_of_out_channel source_lib_out in let source_lib_fmt = formatter_of_out_channel source_lib_out in
print_lib_c source_lib_fmt basename prog machines dependencies; print_lib_c source_lib_fmt basename prog machines dependencies;
...@@ -54,7 +54,7 @@ let gen_files funs basename prog machines dependencies = ...@@ -54,7 +54,7 @@ let gen_files funs basename prog machines dependencies =
raise (Corelang.Error (Location.dummy_loc, LustreSpec.Main_not_found)) raise (Corelang.Error (Location.dummy_loc, LustreSpec.Main_not_found))
end end
| Some m -> begin | Some m -> begin
let source_main_file = destname ^ "_main.c" in (* Could be changed *) let source_main_file = (if !Options.cpp then destname ^ "_main.cpp" else destname ^ "_main.c") in (* Could be changed *)
let source_main_out = open_out source_main_file in let source_main_out = open_out source_main_file in
let source_main_fmt = formatter_of_out_channel source_main_out in let source_main_fmt = formatter_of_out_channel source_main_out in
......
...@@ -124,14 +124,17 @@ let is_basic_c_type t = ...@@ -124,14 +124,17 @@ let is_basic_c_type t =
| Types.Tbool | Types.Treal | Types.Tint -> true | Types.Tbool | Types.Treal | Types.Tint -> true
| _ -> false | _ -> false
let pp_basic_c_type fmt t = let pp_c_basic_type_desc t_dsec =
match (Types.repr t).Types.tdesc with match (t_dsec) with
| Types.Tbool -> fprintf fmt "_Bool" | Types.Tbool when !Options.cpp -> "bool"
| Types.Treal when !Options.mpfr -> fprintf fmt "%s" Mpfr.mpfr_t | Types.Tbool -> "_Bool"
| Types.Treal -> fprintf fmt "double" | Types.Tint -> !Options.int_type
| Types.Tint -> fprintf fmt "int" | Types.Treal when !Options.mpfr -> Mpfr.mpfr_t
| Types.Treal -> !Options.real_type
| _ -> assert false (* Not a basic C type. Do not handle arrays or pointers *) | _ -> assert false (* Not a basic C type. Do not handle arrays or pointers *)
let pp_basic_c_type fmt t = fprintf fmt "%s" (pp_c_basic_type_desc (Types.repr t).Types.tdesc)
let pp_c_type var fmt t = let pp_c_type var fmt t =
let rec aux t pp_suffix = let rec aux t pp_suffix =
match (Types.repr t).Types.tdesc with match (Types.repr t).Types.tdesc with
......
...@@ -39,7 +39,10 @@ let print_import_standard fmt = ...@@ -39,7 +39,10 @@ let print_import_standard fmt =
begin begin
fprintf fmt "#include <mpfr.h>@." fprintf fmt "#include <mpfr.h>@."
end; end;
fprintf fmt "#include \"%s/arrow.h\"@.@." !Options.include_dir if !Options.cpp then
fprintf fmt "#include \"%s/arrow.hpp\"@.@." !Options.include_dir
else
fprintf fmt "#include \"%s/arrow.h\"@.@." !Options.include_dir
end end
......
...@@ -179,7 +179,7 @@ let print_main_code fmt basename m = ...@@ -179,7 +179,7 @@ let print_main_code fmt basename m =
fprintf fmt "@]@ }@." fprintf fmt "@]@ }@."
let print_main_header fmt = let print_main_header fmt =
fprintf fmt "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/io_frontend.h\"@." 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\"@.")
!Options.include_dir !Options.include_dir
let print_main_c main_fmt main_machine basename prog machines _ (*dependencies*) = let print_main_c main_fmt main_machine basename prog machines _ (*dependencies*) =
......
...@@ -588,8 +588,9 @@ let print_step_code dependencies fmt m self = ...@@ -588,8 +588,9 @@ let print_step_code dependencies fmt m self =
let print_global_init_code fmt basename prog dependencies = let print_global_init_code fmt basename prog dependencies =
let baseNAME = file_to_module_name basename in let baseNAME = file_to_module_name basename in
let constants = List.map const_of_top (get_consts prog) in let constants = List.map const_of_top (get_consts prog) in
fprintf fmt "@[<v 2>%a {@,static _Bool init = 0;@,@[<v 2>if (!init) { @,init = 1;@,%a%t%a@]@,}@,return;@]@,}@.@." fprintf fmt "@[<v 2>%a {@,static %s init = 0;@,@[<v 2>if (!init) { @,init = 1;@,%a%t%a@]@,}@,return;@]@,}@.@."
print_global_init_prototype baseNAME print_global_init_prototype baseNAME
(pp_c_basic_type_desc Types.Tbool)
(* constants *) (* constants *)
(Utils.fprintf_list ~sep:"@," (pp_const_initialize (pp_c_var_read Machine_code.empty_machine))) constants (Utils.fprintf_list ~sep:"@," (pp_const_initialize (pp_c_var_read Machine_code.empty_machine))) constants
(Utils.pp_final_char_if_non_empty "@," dependencies) (Utils.pp_final_char_if_non_empty "@," dependencies)
...@@ -599,8 +600,9 @@ let print_global_init_code fmt basename prog dependencies = ...@@ -599,8 +600,9 @@ let print_global_init_code fmt basename prog dependencies =
let print_global_clear_code fmt basename prog dependencies = let print_global_clear_code fmt basename prog dependencies =
let baseNAME = file_to_module_name basename in let baseNAME = file_to_module_name basename in
let constants = List.map const_of_top (get_consts prog) in let constants = List.map const_of_top (get_consts prog) in
fprintf fmt "@[<v 2>%a {@,static _Bool clear = 0;@,@[<v 2>if (!clear) { @,clear = 1;@,%a%t%a@]@,}@,return;@]@,}@.@." fprintf fmt "@[<v 2>%a {@,static %s clear = 0;@,@[<v 2>if (!clear) { @,clear = 1;@,%a%t%a@]@,}@,return;@]@,}@.@."
print_global_clear_prototype baseNAME print_global_clear_prototype baseNAME
(pp_c_basic_type_desc Types.Tbool)
(* constants *) (* constants *)
(Utils.fprintf_list ~sep:"@," (pp_const_clear (pp_c_var_read Machine_code.empty_machine))) constants (Utils.fprintf_list ~sep:"@," (pp_const_clear (pp_c_var_read Machine_code.empty_machine))) constants
(Utils.pp_final_char_if_non_empty "@," dependencies) (Utils.pp_final_char_if_non_empty "@," dependencies)
......
...@@ -51,6 +51,10 @@ let horn_query = ref true ...@@ -51,6 +51,10 @@ let horn_query = ref true
let salsa_enabled = ref true let salsa_enabled = ref true
let cpp = ref false
let int_type = ref "int"
let real_type = ref "double"
let sfunction = ref "" let sfunction = ref ""
let set_mpfr prec = let set_mpfr prec =
...@@ -89,7 +93,12 @@ let options = ...@@ -89,7 +93,12 @@ let options =
"-print_clocks", Arg.Set print_clocks, "prints node clocks"; "-print_clocks", Arg.Set print_clocks, "prints node clocks";
"-O", Arg.Set_int optimization, "changes optimization \x1b[4mlevel\x1b[0m <default: 2>"; "-O", Arg.Set_int optimization, "changes optimization \x1b[4mlevel\x1b[0m <default: 2>";
"-verbose", Arg.Set_int verbose_level, "changes verbose \x1b[4mlevel\x1b[0m <default: 1>"; "-verbose", Arg.Set_int verbose_level, "changes verbose \x1b[4mlevel\x1b[0m <default: 1>";
"-version", Arg.Unit print_version, " displays the version";] "-version", Arg.Unit print_version, " displays the version";
"-c++" , Arg.Set cpp , "c++ backend";
"-int" , Arg.Set_string int_type , "specifies the integer type (default=\"int\")";
"-real", Arg.Set_string real_type, "specifies the real type (default=\"double\" without mpfr option)";
]
let plugin_opt (name, activate, options) = let plugin_opt (name, activate, options) =
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment