@@ -21,6 +21,7 @@ Author: Leonardo de Moura
2121#include " runtime/sstream.h"
2222#include " runtime/load_dynlib.h"
2323#include " runtime/array_ref.h"
24+ #include " runtime/object_ref.h"
2425#include " util/timer.h"
2526#include " util/macros.h"
2627#include " util/io.h"
@@ -172,7 +173,7 @@ using namespace lean; // NOLINT
172173extern " C" void *initialize_Lean_Compiler_IR_EmitLLVM (uint8_t builtin,
173174 lean_object *);
174175extern " C" object *lean_ir_emit_llvm (object *env, object *mod_name,
175- object *filepath, object *w);
176+ object *filepath, object *target_triple, object * w);
176177
177178static void display_header (std::ostream & out) {
178179 out << " Lean (version " << get_version_string () << " , " << LEAN_STR (LEAN_BUILD_TYPE) << " )\n " ;
@@ -198,6 +199,7 @@ static void display_help(std::ostream & out) {
198199 std::cout << " --i=iname -i create ilean file\n " ;
199200 std::cout << " --c=fname -c name of the C output file\n " ;
200201 std::cout << " --bc=fname -b name of the LLVM bitcode file\n " ;
202+ std::cout << " --target=target target triple of object file produced by LLVM\n " ;
201203 std::cout << " --stdin take input from stdin\n " ;
202204 std::cout << " --root=dir set package root directory from which the module name of the input file is calculated\n "
203205 << " (default: current working directory)\n " ;
@@ -249,6 +251,7 @@ static struct option g_long_options[] = {
249251 {" timeout" , optional_argument, 0 , ' T' },
250252 {" c" , optional_argument, 0 , ' c' },
251253 {" bc" , optional_argument, 0 , ' b' },
254+ {" target" , optional_argument, 0 , ' 3' },
252255 {" features" , optional_argument, 0 , ' f' },
253256 {" exitOnPanic" , no_argument, 0 , ' e' },
254257#if defined(LEAN_MULTI_THREAD)
@@ -486,6 +489,7 @@ extern "C" LEAN_EXPORT int lean_main(int argc, char ** argv) {
486489 std::string native_output;
487490 optional<std::string> c_output;
488491 optional<std::string> llvm_output;
492+ optional<std::string> target_triple;
489493 optional<std::string> root_dir;
490494 buffer<string_ref> forwarded_args;
491495
@@ -523,6 +527,10 @@ extern "C" LEAN_EXPORT int lean_main(int argc, char ** argv) {
523527 check_optarg (" b" );
524528 llvm_output = optarg;
525529 break ;
530+ case ' 3' :
531+ check_optarg (" target" );
532+ target_triple = optarg;
533+ break ;
526534 case ' s' :
527535 lean::lthread::set_thread_stack_size (
528536 static_cast <size_t >((atoi (optarg) / 4 ) * 4 ) * static_cast <size_t >(1024 ));
@@ -744,13 +752,25 @@ extern "C" LEAN_EXPORT int lean_main(int argc, char ** argv) {
744752 out.close ();
745753 }
746754
755+ // target triple is only used by the LLVM backend. Save users
756+ // a great deal of pain by erroring out if they misuse flags.
757+ if (target_triple && !llvm_output) {
758+ std::cerr << " ERROR: '--target' must be used with '--bc' to enable LLVM backend. Quitting code generation.\n " ;
759+ return 1 ;
760+ }
761+
747762 if (llvm_output && ok) {
763+ // marshal 'optional<string>' to 'lean_object*'
764+ lean_object* const target_triple_lean =
765+ target_triple ? mk_option_some (lean::string_ref (*target_triple).to_obj_arg ()) : mk_option_none ();
748766 initialize_Lean_Compiler_IR_EmitLLVM (/* builtin*/ false ,
749767 lean_io_mk_world ());
750768 time_task _ (" LLVM code generation" , opts);
751769 lean::consume_io_result (lean_ir_emit_llvm (
752770 env.to_obj_arg (), (*main_module_name).to_obj_arg (),
753- lean::string_ref (*llvm_output).to_obj_arg (), lean_io_mk_world ()));
771+ lean::string_ref (*llvm_output).to_obj_arg (),
772+ target_triple_lean,
773+ lean_io_mk_world ()));
754774 }
755775
756776 display_cumulative_profiling_times (std::cerr);
0 commit comments