diff --git a/compiler/globals.m b/compiler/globals.m index 6716eb31e..b6a532e7e 100644 --- a/compiler/globals.m +++ b/compiler/globals.m @@ -1,5 +1,5 @@ %-----------------------------------------------------------------------------% -% Copyright (C) 1994-2000 The University of Melbourne. +% Copyright (C) 1994-2001 The University of Melbourne. % This file may only be copied under the terms of the GNU General % Public License - see the file COPYING in the Mercury distribution. %-----------------------------------------------------------------------------% @@ -26,12 +26,11 @@ ; il % Generate IL assembler code % IL is the Microsoft .NET Intermediate Language ; java % Generate Java - % (this target is not yet implemented) + % (Work in progress) ; asm. % Compile directly to assembler via the GCC back-end. % Do not go via C, instead generate GCC's internal % `tree' data structure. % (Work in progress.) - :- type gc_method ---> none ; conservative diff --git a/compiler/handle_options.m b/compiler/handle_options.m index a446b5679..539b24657 100644 --- a/compiler/handle_options.m +++ b/compiler/handle_options.m @@ -1,5 +1,5 @@ %-----------------------------------------------------------------------------% -% Copyright (C) 1994-2000 The University of Melbourne. +% Copyright (C) 1994-2001 The University of Melbourne. % This file may only be copied under the terms of the GNU General % Public License - see the file COPYING in the Mercury distribution. %-----------------------------------------------------------------------------% @@ -85,13 +85,15 @@ handle_options(MaybeError, Args, Link) --> TargetCodeOnly), globals__io_get_target(Target), { GenerateIL = (if Target = il then yes else no) }, + { GenerateJava = (if Target = java then yes else no) }, globals__io_lookup_bool_option(compile_only, CompileOnly), globals__io_lookup_bool_option(aditi_only, AditiOnly), { bool__or_list([GenerateDependencies, MakeInterface, MakePrivateInterface, MakeShortInterface, MakeOptimizationInt, MakeTransOptInt, ConvertToMercury, ConvertToGoedel, TypecheckOnly, - ErrorcheckOnly, TargetCodeOnly, GenerateIL, + ErrorcheckOnly, TargetCodeOnly, + GenerateIL, GenerateJava, CompileOnly, AditiOnly], NotLink) }, { bool__not(NotLink, Link) } @@ -216,7 +218,7 @@ postprocess_options(ok(OptionTable), Error) --> { Error = yes("Invalid GC option (must be `none', `conservative' or `accurate')") } ) ; - { Error = yes("Invalid target option (must be `c' or `il')") } + { Error = yes("Invalid target option (must be `c', `il', or `java')") } ). @@ -293,7 +295,21 @@ postprocess_options_2(OptionTable, Target, GC_Method, TagsMethod, ; [] ), - + % Generating Java implies high-level code, turning off nested functions, + % using copy-out for both det and nondet output arguments, + % using no tags and no static ground terms. + % XXX no static ground terms should be eliminated in a later + % version. + ( { Target = java } -> + globals__io_set_option(highlevel_code, bool(yes)), + globals__io_set_option(gcc_nested_functions, bool(no)), + globals__io_set_option(nondet_copy_out, bool(yes)), + globals__io_set_option(det_copy_out, bool(yes)), + globals__io_set_option(num_tag_bits, int(0)), + globals__io_set_option(static_ground_terms, bool(no)) + ; + [] + ), % Generating assembler via the gcc back-end requires % using high-level code. ( { Target = asm } -> @@ -310,7 +326,7 @@ postprocess_options_2(OptionTable, Target, GC_Method, TagsMethod, ; [] ), - + % --high-level-code disables the use of low-level gcc extensions option_implies(highlevel_code, gcc_non_local_gotos, bool(no)), option_implies(highlevel_code, gcc_global_registers, bool(no)), @@ -936,6 +952,14 @@ grade_component_table("ilc", gcc_ext, [ gcc_nested_functions - bool(no), highlevel_data - bool(no), target - string("il")]). +grade_component_table("java", gcc_ext, [ + asm_labels - bool(no), + gcc_non_local_gotos - bool(no), + gcc_global_registers - bool(no), + gcc_nested_functions - bool(no), + highlevel_code - bool(yes), + highlevel_data - bool(yes), + target - string("java")]). % Parallelism/multithreading components. grade_component_table("par", par, [parallel - bool(yes)]). diff --git a/compiler/mercury_compile.m b/compiler/mercury_compile.m index b6b36e65b..ebbfb4296 100644 --- a/compiler/mercury_compile.m +++ b/compiler/mercury_compile.m @@ -62,6 +62,7 @@ :- import_module ml_elim_nested, ml_tailcall. % MLDS -> MLDS :- import_module ml_optimize. % MLDS -> MLDS :- import_module mlds_to_c. % MLDS -> C +:- import_module mlds_to_java. % MLDS -> Java :- import_module mlds_to_ilasm. % MLDS -> IL assembler :- import_module maybe_mlds_to_gcc. % MLDS -> GCC back-end @@ -485,6 +486,14 @@ mercury_compile(Module) --> mercury_compile__il_assemble(ModuleName, HasMain) ) + ; { Target = java } -> + mercury_compile__mlds_backend(HLDS50, MLDS), + mercury_compile__mlds_to_java(MLDS), + ( { TargetCodeOnly = yes } -> + [] + ; + mercury_compile__compile_java_file(ModuleName) + ) ; { Target = asm } -> % compile directly to assembler using the gcc back-end mercury_compile__mlds_backend(HLDS50, MLDS), @@ -2582,6 +2591,18 @@ mercury_compile__mlds_to_high_level_c(MLDS) --> maybe_write_string(Verbose, "% Finished converting MLDS to C.\n"), maybe_report_stats(Stats). +:- pred mercury_compile__mlds_to_java(mlds, io__state, io__state). +:- mode mercury_compile__mlds_to_java(in, di, uo) is det. + +mercury_compile__mlds_to_java(MLDS) --> + globals__io_lookup_bool_option(verbose, Verbose), + globals__io_lookup_bool_option(statistics, Stats), + + maybe_write_string(Verbose, "% Converting MLDS to Java...\n"), + mlds_to_java__output_mlds(MLDS), + maybe_write_string(Verbose, "% Finished converting MLDS to Java.\n"), + maybe_report_stats(Stats). + :- pred mercury_compile__maybe_mlds_to_gcc(mlds, bool, io__state, io__state). :- mode mercury_compile__maybe_mlds_to_gcc(in, out, di, uo) is det. @@ -2921,6 +2942,48 @@ mercury_compile__single_c_to_obj(C_File, O_File, Succeeded) --> [] ). +:- pred mercury_compile__compile_java_file(module_name, io__state, io__state). +:- mode mercury_compile__compile_java_file(in, di, uo) is det. + +mercury_compile__compile_java_file(ModuleName) --> + module_name_to_file_name(ModuleName, ".java", no, JavaFile), + globals__io_lookup_bool_option(verbose, Verbose), + maybe_write_string(Verbose, "% Compiling `"), + maybe_write_string(Verbose, JavaFile), + maybe_write_string(Verbose, "':\n"), + globals__io_lookup_string_option(java_compiler, JavaCompiler), + globals__io_lookup_accumulating_option(java_flags, JavaFlagsList), + { join_string_list(JavaFlagsList, "", "", " ", JAVAFLAGS) }, + + globals__io_lookup_accumulating_option(java_classpath, + Java_Incl_Dirs), + ( { Java_Incl_Dirs = [] } -> + { InclOpt = "" } + ; + % XXX PathSeparator should be ";" on Windows + { PathSeparator = ":" }, + { join_string_list(Java_Incl_Dirs, "", "", + PathSeparator, ClassPath) }, + { InclOpt = string__append_list([ + "-classpath ", ClassPath, " "]) } + ), + globals__io_lookup_bool_option(target_debug, Target_Debug), + { Target_Debug = yes -> + Target_DebugOpt = "-g " + ; + Target_DebugOpt = "" + }, + % Be careful with the order here! Some options may override others. + % Also be careful that each option is separated by spaces. + { string__append_list([JavaCompiler, " ", InclOpt, + Target_DebugOpt, JAVAFLAGS, JavaFile], Command) }, + invoke_system_command(Command, Succeeded), + ( { Succeeded = no } -> + report_error("problem compiling Java file.") + ; + [] + ). + :- pred mercury_compile__asm_to_obj(string, string, bool, io__state, io__state). :- mode mercury_compile__asm_to_obj(in, in, out, di, uo) is det. diff --git a/compiler/mlds_to_java.m b/compiler/mlds_to_java.m new file mode 100644 index 000000000..8cd8073c1 --- /dev/null +++ b/compiler/mlds_to_java.m @@ -0,0 +1,34 @@ +%----------------------------------------------------------------------------% +% Copyright (C) 2000-2001 The University of Melbourne. +% This file may only be copied under the terms of the GNU General +% Public License - see the file COPYING in the Mercury distribution. +%-----------------------------------------------------------------------------% +% +% mlds_to_java - Convert MLDS to Java code. +% Main author: juliensf +% +% XXX Not yet implemented. +%-----------------------------------------------------------------------------% +:- module mlds_to_java. +:- interface. + +:- import_module mlds. +:- import_module io. + +:- pred mlds_to_java__output_mlds(mlds, io__state, io__state). +:- mode mlds_to_java__output_mlds(in, di, uo) is det. + +%-----------------------------------------------------------------------------% + +:- implementation. + + % + % the mlds to java code generator + % + +mlds_to_java__output_mlds(_MLDS) --> + io__stderr_stream(Stream), + io__write_string(Stream, "mlds_to_java.m: Java backend not yet implemented.\n"). + +:- end_module mlds_to_java. +%-----------------------------------------------------------------------------% diff --git a/compiler/options.m b/compiler/options.m index 16f8fd805..8c7e8f81f 100644 --- a/compiler/options.m +++ b/compiler/options.m @@ -135,6 +135,8 @@ ; il % target il ; il_only % target il + target_code_only ; compile_to_c % target c + target_code_only + ; java % target java + ; java_only % target java + target_code_only ; gcc_non_local_gotos ; gcc_global_registers ; asm_labels @@ -284,6 +286,12 @@ ; c_include_directory ; c_flag_to_name_object_file ; object_file_extension + + ; java_compiler + ; java_flags + ; java_classpath + ; java_object_file_extension + ; max_jump_table_size ; compare_specialization ; fact_table_max_array_size @@ -544,6 +552,8 @@ option_defaults_2(compilation_model_option, [ il - special, il_only - special, compile_to_c - special, + java - special, + java_only - special, gcc_non_local_gotos - bool(yes), gcc_global_registers - bool(yes), asm_labels - bool(yes), @@ -652,6 +662,12 @@ option_defaults_2(code_gen_option, [ % the `mmc' script will override the % above default with a value determined % at configuration time + + java_compiler - string("javac"), + java_flags - accumulating([]), + java_classpath - accumulating([]), + java_object_file_extension - string(".class"), + max_jump_table_size - int(0), compare_specialization - int(4), % 0 indicates any size. @@ -946,6 +962,10 @@ long_option("il-only", il_only). long_option("IL-only", il_only). long_option("compile-to-c", compile_to_c). long_option("compile-to-C", compile_to_c). +long_option("java", java). +long_option("Java", java). +long_option("java-only", java_only). +long_option("Java-only", java_only). long_option("gcc-non-local-gotos", gcc_non_local_gotos). long_option("gcc-global-registers", gcc_global_registers). long_option("asm-labels", asm_labels). @@ -1019,6 +1039,7 @@ long_option("num-real-f-regs", num_real_f_regs). long_option("num-real-r-temps", num_real_r_temps). long_option("num-real-f-temps", num_real_f_temps). long_option("num-real-temps", num_real_r_temps). % obsolete + long_option("cc", cc). long_option("cflags", cflags). long_option("cflags-for-regs", cflags_for_regs). @@ -1033,6 +1054,18 @@ long_option("target-debug", target_debug). long_option("c-include-directory", c_include_directory). long_option("c-flag-to-name-object-file", c_flag_to_name_object_file). long_option("object-file-extension", object_file_extension). + +long_option("java-compiler", java_compiler). +long_option("javac", java_compiler). +long_option("java-flags", cflags). + % XXX we should consider the relationship between java_debug and + % target_debug more carefully. Perhaps target_debug could imply + % Java debug if the target is Java. However for the moment they are + % just synonyms. +long_option("java-debug", target_debug). +long_option("java-classpath", java_classpath). +long_option("java-object-file-extension", java_object_file_extension). + long_option("max-jump-table-size", max_jump_table_size). long_option("compare-specialization", compare_specialization). long_option("fact-table-max-array-size",fact_table_max_array_size). @@ -1238,6 +1271,11 @@ special_handler(il_only, none, OptionTable0, ok(OptionTable)) :- special_handler(compile_to_c, none, OptionTable0, ok(OptionTable)) :- map__set(OptionTable0, target, string("c"), OptionTable1), map__set(OptionTable1, target_code_only, bool(yes), OptionTable). +special_handler(java, none, OptionTable0, ok(OptionTable)) :- + map__set(OptionTable0, target, string("java"), OptionTable). +special_handler(java_only, none, OptionTable0, ok(OptionTable)) :- + map__set(OptionTable0, target, string("java"), OptionTable1), + map__set(OptionTable1, target_code_only, bool(yes), OptionTable). special_handler(profiling, bool(Value), OptionTable0, ok(OptionTable)) :- map__set(OptionTable0, profile_time, bool(Value), OptionTable1), map__set(OptionTable1, profile_calls, bool(Value), OptionTable2), @@ -1681,7 +1719,8 @@ options_help_output --> "\tCheck the module for errors, but do not generate any code.", "-C, --target-code-only", "\tGenerate target code (i.e. C code in `.c',", - "\t\tor IL code in `.il') but not object code.", + "\t\tIL code in `.il', or Java code in", + "\t\t`.java'), but not object code.", "-c, --compile-only", "\tGenerate C code in `.c' and object code in `.o'", "\tbut do not attempt to link the named modules.", @@ -1874,17 +1913,28 @@ options_help_compilation_model --> "-s , --grade ", "\tSelect the compilation model. The should be one of", "\t`none', `reg', `jump', `asm_jump', `fast', `asm_fast', `hlc'", - "--target {c, il}", - "\tSpecify the target language: C or IL (default: C).", - "\tThe IL target implies `--high-level-code' (see below).", + "--target {c, il, java}", + "\tSpecify the target language: C, IL or Java (default: C).", + "\tThe IL and Java targets imply `--high-level-code' (see below).", "--il", "\tAn abbreviation for `--target il'.", "--il-only", - "\tAn abbreviation for `--target il --intermediate-code-only'.", - "\tGenerate IL code in `.il', but do not generate object code.", + "\tAn abbreviation for `--target il --target-code-only'.", + "\tGenerate IL code in `.il', but do not generate", + "\tobject code.", + + "--java", + "\tAn abbreviation for `--target java'.", + "--java-only", + "\tAn abbreviation for `--target java --target-code-only'.", + "\tGenerate Java code in `.java', but do not generate", + "\tobject code.", + "--compile-to-c", - "\tAn abbreviation for `--target c --intermediate-code-only'.", + "\tAn abbreviation for `--target c --target-code-only'.", "\tGenerate C code in `.c', but do not generate object code.", + + % These grades (hl, hl_nest, and hlc_nest) are not yet documented, because % the --high-level-data option is not yet implemented, % and the --gcc-nested-functions option is not yet documented. @@ -2120,6 +2170,20 @@ options_help_code_generation --> "\t(This has the same effect as", "\t`--cflags ""-g"" --link-flags ""--no-strip""'.)", + "--javac", + "--java-compiler", + "\tSpecify which Java compiler to use. The default is javac.", + + "--java-flags", + "\tSpecify options to be passed to the Java compiler.", + + "--java-classpath", + "\tSet the classpath for the Java compiler.", + + "--java-object-file-extension", + "\tSpecify an extension for Java object (bytecode) files", + "\tBy default this is `.class'.", + "--no-trad-passes", "\tThe default `--trad-passes' completely processes each predicate", "\tbefore going on to the next predicate.", diff --git a/doc/user_guide.texi b/doc/user_guide.texi index 0f46c507d..e6203c16d 100644 --- a/doc/user_guide.texi +++ b/doc/user_guide.texi @@ -3125,7 +3125,8 @@ Check the module for errors, but do not generate any code. @item -C @itemx --target-code-only Generate target code (i.e. C in @file{@var{module}.c}, -or IL in @file{@var{module}.il}, but not object code. +IL in @file{@var{module}.il} or Java in @file{@var{module}.java}), +but not object code. @sp 1 @item -c @@ -3424,7 +3425,7 @@ The set of aspects and their alternatives are: @table @asis @item What target language to use, and (for C) what combination of GNU C extensions to use: @samp{none}, @samp{reg}, @samp{jump}, @samp{asm_jump}, -@samp{fast}, @samp{asm_fast}, @samp{hlc}, and @samp{ilc} +@samp{fast}, @samp{asm_fast}, @samp{hlc}, @samp{ilc} and @samp{java} (the default is system dependent). @item What garbage collection strategy to use: @@ -3488,6 +3489,9 @@ and grade modifier; they are followed by descriptions of those options. @item @samp{il} @code{--target il --high-level-code}. +@item @samp{java} +@code{--target java --high-level-code}. + @item @samp{.gc} @code{--gc conservative}. @@ -3526,10 +3530,12 @@ and grade modifier; they are followed by descriptions of those options. @sp 1 @item @code{--target c} (grades: none, reg, jump, fast, asm_jump, asm_fast, hlc) @itemx @code{--il}, @code{--target il} (grades: ilc) -Specify the target language used for compilation: C or IL. +@itemx @code{--java}, @code{--target java} (grades: java) +Specify the target language used for compilation: C, IL or Java. C means ANSI/ISO C, optionally with GNU C extensions (see below). IL means the Microsoft .NET Intermediate Language. @samp{--target il} implies @samp{--high-level-code}. +@samp{--target java} implies @samp{--high-level-code}. @sp 1 @item @code{--il-only} @@ -3544,6 +3550,12 @@ An abbreviation for @samp{--target c --target-code-only}. Generate C code in @file{@var{module}.c}, but do not invoke the C compiler to generate object code. +@sp 1 +@item @code{--java-only} +An abbreviation for @samp{--target java --target-code-only}. +Generate Java code in @file{@var{module}.java}, but do not invoke +the Java compiler to produce Java bytecode. + @sp 1 @item @code{--gcc-global-registers} (grades: reg, fast, asm_fast) @itemx @code{--no-gcc-global-registers} (grades: none, jump, asm_jump) @@ -3750,6 +3762,24 @@ Since the generated C code is very low-level, this option is not likely to be useful to anyone except the Mercury implementors, except perhaps for debugging code that uses Mercury's C interface extensively. +@sp 1 +@item --javac @var{compiler-name} +@item --java-compiler @var{compiler-name} +Specify which Java compiler to use. The default is @samp{javac}. + +@sp 1 +@item --java-flags @var{options} +Specify options to be passed to the Java compiler. + +@sp 1 +@item --java-classpath @var{dir} +Set the classpath for the Java compiler. + +@sp 1 +@item --java-object-file-extension @var{extension} +Specify an extension for Java object (bytecode) files. By default this +is @samp{.class}. + @sp 1 @item --fact-table-max-array-size @var{size} Specify the maximum number of elements in a single diff --git a/scripts/final_grade_options.sh-subr b/scripts/final_grade_options.sh-subr index 3b66b9fb7..34ac6e2da 100644 --- a/scripts/final_grade_options.sh-subr +++ b/scripts/final_grade_options.sh-subr @@ -39,9 +39,9 @@ case $stack_trace,$require_tracing,$use_minimal_model in esac # -# --target asm or IL implies --high-level-code +# --target asm, IL or Java implies --high-level-code # -case $target in asm|il) +case $target in asm|il|java) highlevel_code=true ;; esac diff --git a/scripts/init_grade_options.sh-subr b/scripts/init_grade_options.sh-subr index d731b5d12..14ea804ab 100644 --- a/scripts/init_grade_options.sh-subr +++ b/scripts/init_grade_options.sh-subr @@ -23,7 +23,7 @@ grade_usage="\ Grade options: -s , --grade - --target {il, c, asm} + --target {il, c, java, asm} --il --asm-labels --gcc-non-local-gotos diff --git a/scripts/parse_grade_options.sh-subr b/scripts/parse_grade_options.sh-subr index 0e8839178..dafafabc3 100644 --- a/scripts/parse_grade_options.sh-subr +++ b/scripts/parse_grade_options.sh-subr @@ -28,6 +28,8 @@ target=c ;; il|IL) target=il ;; + java|Java) + target=java ;; *) echo "$0: invalid target \`$1'" 1>&2 exit 1 @@ -208,6 +210,15 @@ gcc_nested_functions=false highlevel_data=false ;; + java) + target=java + asm_labels=false + non_local_gotos=false + global_regs=false + highlevel_code=true + gcc_nested_functions=false + highlevel_data=true + ;; hl) asm_labels=false non_local_gotos=false