[m-dev.] for review: compiler options for Java backend

Julien Fischer juliensf at students.cs.mu.oz.au
Thu Jan 25 13:15:20 AEDT 2001


Estimated hours taken: 4

Added new compiler options to support the Java backend and
updated documentation to reflect this.

compiler/globals.m:
	Removed comment about Java backend not being implemented.
	Replaced it by one say that it is work in progress.
compiler/mercury_compile.m:
	If Target = java then call the Java backend.
compiler/options.m:
	Added new options for compiling Java files:
	`--java'
	`--java-only'
	`--java-compiler' ('--javac')
	`--java-flags'
	`--java-classpath'
	`--java-object-file-extension'
compiler/handle_options.m:
	If compiling to Java then don't link.
	Added "java" grade.
doc/user_guide.texi:
	Documented new options for compiling Java files.
scripts/init_grade_options.sh-subr:
scripts/parse_grade_options.sh-subr:
scripts/final_grade_options.sh-subr:
	Added support for "java" grade.

Julien
	
Index: compiler/globals.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/globals.m,v
retrieving revision 1.39
diff -u -r1.39 globals.mD
--- compiler/globals.m	2001/01/10 10:53:50	1.39
+++ compiler/globals.m	2001/01/25 01:54:14
@@ -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
Index: compiler/handle_options.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/handle_options.m,v
retrieving revision 1.99
diff -u -r1.99 handle_options.m
--- compiler/handle_options.m	2001/01/10 10:53:50	1.99
+++ compiler/handle_options.m	2001/01/25 01:54:14
@@ -85,13 +85,15 @@
 			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 @@
             { 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 @@
 	;
 		[]
 	),
-
+	% Generating Java implies high-level code, turning off nested functions,
+	% using copy-out for both det and nondet output arguments,
+	% using no tags, boxing enums, disabling no_tag_types and no
+	% static ground terms.
+	( { 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(line_numbers, bool(no)),
+		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 @@
 	;
 		[]
 	),
-
+	
 	% --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 @@
 		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)]).
Index: compiler/mercury_compile.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mercury_compile.m,v
retrieving revision 1.189
diff -u -r1.189 mercury_compile.m
--- compiler/mercury_compile.m	2001/01/24 13:18:14	1.189
+++ compiler/mercury_compile.m	2001/01/25 01:54:14
@@ -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__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 @@
 	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.
 
@@ -2917,6 +2938,48 @@
 	invoke_system_command(Command, Succeeded),
 	( { Succeeded = no } ->
 		report_error("problem compiling C file.")
+	;
+		[]
+	).
+
+:- 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.")
 	;
 		[]
 	).
Index: compiler/options.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/options.m,v
retrieving revision 1.305
diff -u -r1.305 options.m
--- compiler/options.m	2001/01/09 04:06:43	1.305
+++ compiler/options.m	2001/01/25 01:54:15
@@ -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 @@
 	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 @@
 					% 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("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-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("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(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 @@
 		"\tCheck the module for errors, but do not generate any code.",
 		"-C, --target-code-only",
 		"\tGenerate target code (i.e. C code in `<module>.c',",
-		"\t\tor IL code in `<module>.il') but not object code.",
+		"\t\tIL code in `<module>.il', or Java code in",
+		"\t\t`<module>.java'), but not object code.",
 		"-c, --compile-only",
 		"\tGenerate C code in `<module>.c' and object code in `<module>.o'",
 		"\tbut do not attempt to link the named modules.",
@@ -1874,17 +1913,28 @@
 		"-s <grade>, --grade <grade>",
 		"\tSelect the compilation model. The <grade> 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 `<module>.il', but do not generate object code.",
+		"\tGenerate IL code in `<module>.il', but do not generate",
+		"\tobject code.",
+		
+		"--java",
+		"\tAn abbreviation for `--target java'.",
+		"--java-only",
+		"\tAn abbreviation for `--target java --intermediate-code-only'.",
+		"\tGenerate Java code in `<module>.java', but do not generate",
+		"\tobject code.",
+		
 		"--compile-to-c",
 		"\tAn abbreviation for `--target c --intermediate-code-only'.",
 		"\tGenerate C code in `<module>.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.
@@ -2119,6 +2169,20 @@
 		"\tEnable debugging of the generated C code.",
 		"\t(This has the same effect as",
 		"\t`--cflags ""-g"" --link-flags ""--no-strip""'.)",
+
+		"--javac",
+		"--java-compiler",
+		"\tSpecify which Java compiler to use.",
+		
+		"--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",
Index: doc/user_guide.texi
===================================================================
RCS file: /home/mercury1/repository/mercury/doc/user_guide.texi,v
retrieving revision 1.233
diff -u -r1.233 user_guide.texi
--- doc/user_guide.texi	2001/01/18 03:01:52	1.233
+++ doc/user_guide.texi	2001/01/25 01:54:15
@@ -3125,7 +3125,8 @@
 @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 @@
 @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},
- at samp{fast}, @samp{asm_fast}, @samp{hlc}, and @samp{ilc}
+ at 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 @@
 @item @samp{il}
 @code{--target il --high-level-code}.
 
+ at item @samp{java}
+ at code{--target java --high-level-code}.
+
 @item @samp{.gc}
 @code{--gc conservative}.
 
@@ -3526,10 +3530,12 @@
 @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.
+ at 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}.
+ at samp{--target java} implies @samp{--high-level-code}.
 
 @sp 1
 @item @code{--il-only}
@@ -3545,6 +3551,12 @@
 C compiler to generate object code.
 
 @sp 1
+ at 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.
+
+ at sp 1
 @item @code{--gcc-global-registers} (grades: reg, fast, asm_fast)
 @itemx @code{--no-gcc-global-registers} (grades: none, jump, asm_jump)
 Specify whether or not to use GNU C's global register variables extension.
@@ -3749,6 +3761,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.
+
+ at sp 1
+ at item --javac @var{compiler-name}
+ at item --java-compiler @var{compiler-name}
+Specify which Java compiler to use.
+
+ at sp 1
+ at item --java-flags @var{options}
+Specify options to be passed to the Java compiler.
+
+ at sp 1
+ at item --java-classpath @var{dir}
+Set the classpath for the Java compiler.
+
+ at sp 1
+ at item --java-object-file-extension @var{extension}
+Specify an extension for Java object (bytecode) files.  By default this
+is `.class'. 
 
 @sp 1
 @item --fact-table-max-array-size @var{size}
Index: scripts/final_grade_options.sh-subr
===================================================================
RCS file: /home/mercury1/repository/mercury/scripts/final_grade_options.sh-subr,v
retrieving revision 1.6
diff -u -r1.6 final_grade_options.sh-subr
--- scripts/final_grade_options.sh-subr	2001/01/17 02:45:07	1.6
+++ scripts/final_grade_options.sh-subr	2001/01/25 01:54:16
@@ -39,9 +39,9 @@
 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
 
Index: scripts/init_grade_options.sh-subr
===================================================================
RCS file: /home/mercury1/repository/mercury/scripts/init_grade_options.sh-subr,v
retrieving revision 1.13
diff -u -r1.13 init_grade_options.sh-subr
--- scripts/init_grade_options.sh-subr	2001/01/17 02:45:07	1.13
+++ scripts/init_grade_options.sh-subr	2001/01/25 01:54:16
@@ -23,7 +23,7 @@
 grade_usage="\
 Grade options:
 	-s <grade>, --grade <grade>
-	--target {il, c, asm}
+	--target {il, c, java, asm}
 	--il
 	--asm-labels
 	--gcc-non-local-gotos
Index: scripts/parse_grade_options.sh-subr
===================================================================
RCS file: /home/mercury1/repository/mercury/scripts/parse_grade_options.sh-subr,v
retrieving revision 1.18
diff -u -r1.18 parse_grade_options.sh-subr
--- scripts/parse_grade_options.sh-subr	2001/01/17 02:45:07	1.18
+++ scripts/parse_grade_options.sh-subr	2001/01/25 01:54:16
@@ -28,6 +28,8 @@
 				target=c ;;
 			il|IL)
 				target=il ;;
+			java|Java)
+				target=java ;;
 			*)
 				echo "$0: invalid target \`$1'" 1>&2
 				exit 1
@@ -207,6 +209,15 @@
 					highlevel_code=true
 					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

--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list