CBMC
compile.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Compile and link source and object files.
4 
5 Author: CM Wintersteiger
6 
7 Date: June 2006
8 
9 \*******************************************************************/
10 
13 
14 #ifndef CPROVER_GOTO_CC_COMPILE_H
15 #define CPROVER_GOTO_CC_COMPILE_H
16 
17 #include <util/message.h>
18 #include <util/std_types.h>
19 #include <util/symbol.h>
20 
21 #include <list>
22 #include <map>
23 
24 class cmdlinet;
25 class goto_functionst;
26 class goto_modelt;
27 class language_filest;
28 class languaget;
29 class symbol_tablet;
30 
31 class compilet
32 {
33 public:
34  // configuration
36  bool validate_goto_model = false;
37 
38  enum { PREPROCESS_ONLY, // gcc -E
39  COMPILE_ONLY, // gcc -c
40  ASSEMBLE_ONLY, // gcc -S
41  LINK_LIBRARY, // ld -r
42  COMPILE_LINK, // gcc -shared
44  } mode;
45 
46  std::list<std::string> library_paths;
47  std::list<std::string> source_files;
48  std::list<std::string> object_files;
49  std::list<std::string> libraries;
50 
51  std::string object_file_extension;
53 
54  // the two options below are mutually exclusive -- use either or
56 
57  compilet(cmdlinet &_cmdline, message_handlert &mh, bool Werror);
58 
59  ~compilet();
60 
61  bool add_input_file(const std::string &);
62  bool find_library(const std::string &);
63  bool add_files_from_archive(const std::string &file_name, bool thin_archive);
64 
65  bool parse(const std::string &filename, language_filest &);
66  bool parse_stdin(languaget &);
67  bool doit();
68  std::optional<symbol_tablet> compile();
69  bool link(std::optional<symbol_tablet> &&symbol_table);
70 
71  std::optional<symbol_tablet> parse_source(const std::string &);
72 
80  static bool write_bin_object_file(
81  const std::string &file_name,
82  const goto_modelt &src_goto_model,
84  message_handlert &message_handler);
85 
87  bool wrote_object_files() const { return wrote_object; }
88 
95  std::size_t> &cprover_macros) const
96  {
98  for(const auto &pair : written_macros)
99  cprover_macros.insert({pair.first,
100  to_code_type(pair.second.type).parameters().size()});
101  }
102 
103 protected:
104  std::string working_directory;
105  std::string override_language;
106 
107  std::list<std::string> tmp_dirs;
108  std::list<irep_idt> seen_modes;
109 
113 
115  const bool keep_file_local;
116 
118  const std::string file_local_mangle_suffix;
119 
120  static std::size_t function_body_count(const goto_functionst &);
121 
123  const std::string &file_name,
124  const goto_modelt &src_goto_model)
125  {
127  file_name,
128  src_goto_model,
131  {
132  return true;
133  }
134 
135  wrote_object = true;
136 
137  return false;
138  }
139 
140  void add_compiler_specific_defines() const;
141 
143 
144  bool add_written_cprover_symbols(const symbol_tablet &symbol_table);
145  std::map<irep_idt, symbolt> written_macros;
146 
147  // clients must only call add_written_cprover_symbols() if an object
148  // file has been written. The case where an object file was written
149  // but there were no __CPROVER symbols in the goto-program is distinct
150  // from the case where the user forgot to write an object file before
151  // calling add_written_cprover_symbols().
153 };
154 
155 #endif // CPROVER_GOTO_CC_COMPILE_H
const parameterst & parameters() const
Definition: std_types.h:699
@ ASSEMBLE_ONLY
Definition: compile.h:40
@ LINK_LIBRARY
Definition: compile.h:41
@ PREPROCESS_ONLY
Definition: compile.h:38
@ COMPILE_LINK_EXECUTABLE
Definition: compile.h:43
@ COMPILE_ONLY
Definition: compile.h:39
@ COMPILE_LINK
Definition: compile.h:42
void add_compiler_specific_defines() const
Definition: compile.cpp:692
std::optional< symbol_tablet > compile()
Parses source files and writes object files, or keeps the symbols in the symbol_table if not compilin...
Definition: compile.cpp:371
cmdlinet & cmdline
Definition: compile.h:111
bool wrote_object
Definition: compile.h:152
bool parse_stdin(languaget &)
parses a source file (low-level parsing)
Definition: compile.cpp:538
bool parse(const std::string &filename, language_filest &)
parses a source file (low-level parsing)
Definition: compile.cpp:457
std::string working_directory
Definition: compile.h:104
std::list< std::string > tmp_dirs
Definition: compile.h:107
bool echo_file_name
Definition: compile.h:35
std::string override_language
Definition: compile.h:105
std::string output_file_object
Definition: compile.h:55
bool write_bin_object_file(const std::string &file_name, const goto_modelt &src_goto_model)
Definition: compile.h:122
bool add_input_file(const std::string &)
puts input file names into a list and does preprocessing for libraries.
Definition: compile.cpp:167
std::list< std::string > libraries
Definition: compile.h:49
std::list< irep_idt > seen_modes
Definition: compile.h:108
std::string output_directory_object
Definition: compile.h:55
void cprover_macro_arities(std::map< irep_idt, std::size_t > &cprover_macros) const
__CPROVER_... macros written to object files and their arities
Definition: compile.h:94
std::list< std::string > object_files
Definition: compile.h:48
bool link(std::optional< symbol_tablet > &&symbol_table)
parses object files and links them
Definition: compile.cpp:317
bool doit()
reads and source and object files, compiles and links them into goto program objects.
Definition: compile.cpp:54
std::list< std::string > source_files
Definition: compile.h:47
~compilet()
cleans up temporary files
Definition: compile.cpp:673
bool wrote_object_files() const
Has this compiler written any object files?
Definition: compile.h:87
bool add_written_cprover_symbols(const symbol_tablet &symbol_table)
Definition: compile.cpp:740
std::string object_file_extension
Definition: compile.h:51
compilet(cmdlinet &_cmdline, message_handlert &mh, bool Werror)
constructor
Definition: compile.cpp:647
bool validate_goto_model
Definition: compile.h:36
const bool keep_file_local
Whether to keep implementations of file-local symbols.
Definition: compile.h:115
bool warning_is_fatal
Definition: compile.h:112
static std::size_t function_body_count(const goto_functionst &)
Definition: compile.cpp:681
void convert_symbols(goto_modelt &)
Definition: compile.cpp:698
std::map< irep_idt, symbolt > written_macros
Definition: compile.h:145
bool add_files_from_archive(const std::string &file_name, bool thin_archive)
extracts goto binaries from AR archive and add them as input files.
Definition: compile.cpp:208
bool find_library(const std::string &)
tries to find a library object file that matches the given library name.
Definition: compile.cpp:273
enum compilet::@3 mode
messaget log
Definition: compile.h:110
std::optional< symbol_tablet > parse_source(const std::string &)
Parses and type checks a source file located at file_name.
Definition: compile.cpp:621
static bool write_bin_object_file(const std::string &file_name, const goto_modelt &src_goto_model, bool validate_goto_model, message_handlert &message_handler)
Writes the goto functions of src_goto_model to a binary format object file.
Definition: compile.cpp:574
const std::string file_local_mangle_suffix
String to include in all mangled names.
Definition: compile.h:118
std::list< std::string > library_paths
Definition: compile.h:46
std::string output_file_executable
Definition: compile.h:52
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
A collection of goto functions.
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:154
message_handlert & get_message_handler()
Definition: message.h:183
The symbol table.
Definition: symbol_table.h:14
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
Pre-defined types.
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:788
Symbol table entry.