[m-rev.] trivial diff: rename mlds.target_lang type

Julien Fischer juliensf at csse.unimelb.edu.au
Wed Oct 4 17:14:05 AEST 2006


Estimated hours taken: 0.2
Branches: main

Switch on the foreign_language type rather than using an if-then-else
wherever we can.  This makes it easier for the compiler to identify 
spots that might need to be changed when adding support for new backends
or foreign languages.

compiler/mlds.m:
 	Rename the type target_lang to mlds_target_lang.

 	Rename the constructors of the above type so they don't clash with
 	those of the foreign_language type.  Also comment out some of those
 	that don't correspond to any language that we currently target.

compiler/code_gen.m:
compiler/export.m:
compiler/llds_out.m:
compiler/ml_closure_gen.m:
compiler/ml_code_gen.m:
compiler/mlds_to_c.m:
compiler/mlds_to_gcc.m:
compiler/mlds_to_java.m:
 	Switch on foreign languages rather than using if then-elses.

 	Conform to the above change.

compiler/mlds_to_io.m:
 	As above, also abort if we encounter any MLDS trail ops rather
 	than generating a comment.  (Trail ops for the MLDS backends are
 	currently handled as an HLDS->HLDS pass.)

Julien.

Index: compiler/code_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/code_gen.m,v
retrieving revision 1.165
diff -u -r1.165 code_gen.m
--- compiler/code_gen.m	22 Aug 2006 05:03:39 -0000	1.165
+++ compiler/code_gen.m	4 Oct 2006 06:30:45 -0000
@@ -250,11 +250,17 @@
      ;
          GoalExpr = call_foreign_proc(Attributes, PredId, ProcId,
              Args, ExtraArgs, MaybeTraceRuntimeCond, PragmaCode),
-        ( get_foreign_language(Attributes) = lang_c ->
-            pragma_c_gen.generate_pragma_c_code(CodeModel, Attributes,
+        Lang = get_foreign_language(Attributes),
+        (   Lang = lang_c,
+            generate_pragma_c_code(CodeModel, Attributes,
                  PredId, ProcId, Args, ExtraArgs, MaybeTraceRuntimeCond,
                  PragmaCode, GoalInfo, Code, !CI)
          ;
+            ( Lang = lang_java
+            ; Lang = lang_csharp
+            ; Lang = lang_managed_cplusplus
+            ; Lang = lang_il
+            ),
              unexpected(this_file,
                  "generate_goal_2: foreign code other than C unexpected")
          )
Index: compiler/export.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/export.m,v
retrieving revision 1.105
diff -u -r1.105 export.m
--- compiler/export.m	27 Sep 2006 06:16:51 -0000	1.105
+++ compiler/export.m	4 Oct 2006 05:05:29 -0000
@@ -708,7 +708,8 @@
  produce_header_file_2([], !IO).
  produce_header_file_2([E | ExportedProcs], !IO) :-
      E = foreign_export_decl(Lang, C_RetType, C_Function, ArgDecls),
-    ( Lang = lang_c ->
+    ( 
+        Lang = lang_c,
          % Output the function header.
          io.write_string(C_RetType, !IO),
          io.write_string(" ", !IO),
@@ -717,6 +718,11 @@
          io.write_string(ArgDecls, !IO),
          io.write_string(");\n", !IO)
      ;
+        ( Lang = lang_csharp
+        ; Lang = lang_java
+        ; Lang = lang_managed_cplusplus
+        ; Lang = lang_il
+        ),
          sorry(this_file, "foreign languages other than C unimplemented")
      ),
      produce_header_file_2(ExportedProcs, !IO).
Index: compiler/llds_out.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/llds_out.m,v
retrieving revision 1.293
diff -u -r1.293 llds_out.m
--- compiler/llds_out.m	29 Sep 2006 06:34:49 -0000	1.293
+++ compiler/llds_out.m	4 Oct 2006 05:07:54 -0000
@@ -1294,7 +1294,8 @@

  output_user_foreign_code(user_foreign_code(Lang, Foreign_Code, Context),
          !IO) :-
-    ( Lang = lang_c ->
+    ( 
+        Lang = lang_c,
          globals.io_lookup_bool_option(auto_comments, PrintComments, !IO),
          (
              PrintComments = yes,
@@ -1309,6 +1310,11 @@
          io.write_string("\n", !IO),
          output_reset_line_num(!IO)
      ;
+        ( Lang = lang_java
+        ; Lang = lang_csharp
+        ; Lang = lang_managed_cplusplus
+        ; Lang = lang_il
+        ),
          unexpected(this_file, "output_user_foreign_code: unimplemented: " ++
              "foreign code other than C")
      ).
@@ -1324,7 +1330,8 @@

  output_foreign_header_include_line(Decl, !AlreadyDone, !IO) :-
      Decl = foreign_decl_code(Lang, _IsLocal, Code, Context),
-    ( Lang = lang_c ->
+    ( 
+        Lang = lang_c,
          ( set.member(Code, !.AlreadyDone) ->
              true
          ;
@@ -1346,8 +1353,13 @@
              output_reset_line_num(!IO)
          )
      ;
+        ( Lang = lang_java
+        ; Lang = lang_csharp
+        ; Lang = lang_managed_cplusplus
+        ; Lang = lang_il
+        ),
          unexpected(this_file, "output_user_foreign_code: unexpected: " ++
-            "foreign code other than C")
+            "foreign decl code other than C")
      ).

  :- pred output_c_label_decls(map(label, data_addr)::in, list(label)::in,
Index: compiler/ml_closure_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_closure_gen.m,v
retrieving revision 1.46
diff -u -r1.46 ml_closure_gen.m
--- compiler/ml_closure_gen.m	22 Aug 2006 05:03:53 -0000	1.46
+++ compiler/ml_closure_gen.m	4 Oct 2006 06:41:42 -0000
@@ -1094,7 +1094,7 @@
              raw_target_code(")->MR_closure_layout;\n", [])
          ],
          ClosureLayoutPtrGCInit = statement(atomic(
-            inline_target_code(lang_C,
+            inline_target_code(ml_target_c,
              ClosureLayoutPtrGCInitFragments)), MLDS_Context),
          TypeParamsGCInitFragments = [
              target_code_output(TypeParamsLval),
@@ -1132,7 +1132,7 @@

      ),
      TypeParamsGCInit = statement(atomic(inline_target_code(
-        lang_C, TypeParamsGCInitFragments)), MLDS_Context),
+        ml_target_c, TypeParamsGCInitFragments)), MLDS_Context),
      GC_Decls = [ClosureLayoutPtrDecl, TypeParamsDecl].

  ml_gen_local_for_output_arg(VarName, Type, ArgNum, Context, LocalVarDefn,
@@ -1183,7 +1183,7 @@

      (
          MaybeGCTraceCode0 = yes(CallTraceFuncCode),
-        MakeTypeInfoCode = atomic(inline_target_code(lang_C, [
+        MakeTypeInfoCode = atomic(inline_target_code(ml_target_c, [
              raw_target_code("{\n", []),
              raw_target_code("MR_MemoryList allocated_mem = NULL;\n", []),
              target_code_output(TypeInfoLval),
@@ -1197,7 +1197,7 @@
                  "NULL, NULL, &allocated_mem);\n",
                  [i(ArgNum)]), [])
          ])),
-        DeallocateCode = atomic(inline_target_code(lang_C, [
+        DeallocateCode = atomic(inline_target_code(ml_target_c, [
              raw_target_code("MR_deallocate(allocated_mem);\n", []),
              raw_target_code("}\n", [])
          ])),
Index: compiler/ml_code_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_code_gen.m,v
retrieving revision 1.188
diff -u -r1.188 ml_code_gen.m
--- compiler/ml_code_gen.m	2 Oct 2006 05:21:14 -0000	1.188
+++ compiler/ml_code_gen.m	4 Oct 2006 06:43:05 -0000
@@ -2453,10 +2453,10 @@
          raw_target_code(HashUndefs, []),
          raw_target_code("}\n", [])
      ],
-    Starting_C_Code_Stmt = inline_target_code(lang_C, Starting_C_Code),
+    Starting_C_Code_Stmt = inline_target_code(ml_target_c, Starting_C_Code),
      Starting_C_Code_Statement = statement(
          atomic(Starting_C_Code_Stmt), mlds_make_context(Context)),
-    Ending_C_Code_Stmt = inline_target_code(lang_C, Ending_C_Code),
+    Ending_C_Code_Stmt = inline_target_code(ml_target_c, Ending_C_Code),
      Ending_C_Code_Statement = statement(
          atomic(Ending_C_Code_Stmt), mlds_make_context(Context)),
      Statements = list.condense([
@@ -2600,7 +2600,7 @@
          AssignInputsList,
          [user_target_code(JavaCode, yes(Context), [])]
      ]),
-    Java_Code_Stmt = inline_target_code(lang_java, Java_Code),
+    Java_Code_Stmt = inline_target_code(ml_target_java, Java_Code),
      Java_Code_Statement = statement(
          atomic(Java_Code_Stmt),
          mlds_make_context(Context)),
@@ -2772,7 +2772,7 @@
              ByRefOutputVars, CopiedOutputVars),
          ArgVars, VarLocals),

-    OutlineStmt = inline_target_code(lang_il, [
+    OutlineStmt = inline_target_code(ml_target_il, [
          user_target_code(ForeignCode, yes(Context),
              get_target_code_attributes(lang_il,
                  get_extra_attributes(Attributes)))
@@ -3033,8 +3033,8 @@
              raw_target_code("}\n", [])
          ]
      ),
-    Starting_C_Code_Stmt = inline_target_code(lang_C, Starting_C_Code),
-    Ending_C_Code_Stmt = inline_target_code(lang_C, Ending_C_Code),
+    Starting_C_Code_Stmt = inline_target_code(ml_target_c, Starting_C_Code),
+    Ending_C_Code_Stmt = inline_target_code(ml_target_c, Ending_C_Code),
      Starting_C_Code_Statement = statement(
          atomic(Starting_C_Code_Stmt), mlds_make_context(Context)),
      Ending_C_Code_Statement = statement(atomic(Ending_C_Code_Stmt),
Index: compiler/ml_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_util.m,v
retrieving revision 1.52
diff -u -r1.52 ml_util.m
--- compiler/ml_util.m	2 Oct 2006 05:21:15 -0000	1.52
+++ compiler/ml_util.m	4 Oct 2006 07:01:37 -0000
@@ -80,7 +80,8 @@
      % because it shouldn't be generated with target language different to the
      % native target language in the long run.
      %
-:- pred defn_contains_foreign_code(target_lang::in, mlds_defn::in) is semidet.
+:- pred defn_contains_foreign_code(mlds_target_lang::in,
+    mlds_defn::in) is semidet.

      % defn_contains_foreign_code(ForeignLang, Defn):
      %
Index: compiler/mlds.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mlds.m,v
retrieving revision 1.141
diff -u -r1.141 mlds.m
--- compiler/mlds.m	1 Oct 2006 06:05:03 -0000	1.141
+++ compiler/mlds.m	4 Oct 2006 07:01:11 -0000
@@ -1300,7 +1300,7 @@

      % Foreign language interfacing.

-    ;       inline_target_code(target_lang, list(target_code_component))
+    ;       inline_target_code(mlds_target_lang, list(target_code_component))
              % Do whatever is specified by the target_code_components, which
              % can be any piece of code in the specified target language (C,
              % assembler, or whatever) that does not have any non-local flow
@@ -1348,15 +1348,15 @@

      % This is just a random selection of possible languages
      % that we might want to target...
-:- type target_lang
-    --->    lang_C
-    ;       lang_GNU_C
-    ;       lang_C_minus_minus
-    ;       lang_asm
-    ;       lang_il
-    ;       lang_java
-    ;       lang_java_asm
-    ;       lang_java_bytecode.
+:- type mlds_target_lang
+    --->    ml_target_c
+    ;       ml_target_gnu_c
+%     ;       ml_target_c_minus_minus
+    ;       ml_target_asm
+    ;       ml_target_il
+    ;       ml_target_java.
+%     ;       ml_target_java_asm
+%     ;       ml_target_java_bytecode.

  :- type target_code_component
      --->    user_target_code(
Index: compiler/mlds_to_c.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mlds_to_c.m,v
retrieving revision 1.200
diff -u -r1.200 mlds_to_c.m
--- compiler/mlds_to_c.m	2 Oct 2006 05:21:16 -0000	1.200
+++ compiler/mlds_to_c.m	4 Oct 2006 06:44:50 -0000
@@ -770,7 +770,8 @@
  mlds_output_c_hdr_decl(_Indent, MaybeDesiredIsLocal, DeclCode, !IO) :-
      DeclCode = foreign_decl_code(Lang, IsLocal, Code, Context),
      % Only output C code in the C header file.
-    ( Lang = lang_c ->
+    ( 
+        Lang = lang_c,
          (
              (
                  MaybeDesiredIsLocal = no
@@ -785,6 +786,11 @@
              true
          )
      ;
+        ( Lang = lang_java
+        ; Lang = lang_csharp
+        ; Lang = lang_managed_cplusplus
+        ; Lang = lang_il
+        ),
          sorry(this_file, "foreign code other than C")
      ).

@@ -3140,11 +3146,17 @@

  mlds_output_atomic_stmt(_Indent, _FuncInfo,
      inline_target_code(TargetLang, Components), Context, !IO) :-
-    ( TargetLang = lang_C ->
+    ( 
+        TargetLang = ml_target_c,
          list.foldl(mlds_output_target_code_component(Context), Components,
              !IO)
      ;
-        sorry(this_file, "inline_target_code only works for lang_C")
+        ( TargetLang = ml_target_gnu_c
+        ; TargetLang = ml_target_asm
+        ; TargetLang = ml_target_il
+        ; TargetLang = ml_target_java
+        ),
+        sorry(this_file, "inline_target_code only works for language C")
      ).

  mlds_output_atomic_stmt(_Indent, _FuncInfo,
Index: compiler/mlds_to_gcc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mlds_to_gcc.m,v
retrieving revision 1.123
diff -u -r1.123 mlds_to_gcc.m
--- compiler/mlds_to_gcc.m	2 Oct 2006 05:21:16 -0000	1.123
+++ compiler/mlds_to_gcc.m	4 Oct 2006 06:54:52 -0000
@@ -253,7 +253,7 @@
  	% Handle output of any foreign code (C, Ada, Fortran, etc.)
  	% to appropriate files.
  	%
-	{ list__filter(defn_contains_foreign_code(lang_asm), Defns0,
+	{ list__filter(defn_contains_foreign_code(ml_target_asm), Defns0,
  		ForeignDefns, Defns) },
  		% We only handle C currently, so we just look up C
  	{ ForeignCode = map__lookup(AllForeignCode, lang_c) },
Index: compiler/mlds_to_il.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mlds_to_il.m,v
retrieving revision 1.171
diff -u -r1.171 mlds_to_il.m
--- compiler/mlds_to_il.m	2 Oct 2006 05:21:16 -0000	1.171
+++ compiler/mlds_to_il.m	4 Oct 2006 06:52:23 -0000
@@ -1968,25 +1968,26 @@
          Instrs = comment_node("outline foreign proc -- already called")
      ).

-atomic_statement_to_il(inline_target_code(lang_il, Code), Instrs, !Info) :-
+atomic_statement_to_il(inline_target_code(ml_target_il, Code), Instrs,
+        !Info) :-
      Instrs = inline_code_to_il_asm(Code).
-atomic_statement_to_il(inline_target_code(lang_C, _Code), _Instrs, !Info) :-
-    unexpected(this_file, "lang_C").
-atomic_statement_to_il(inline_target_code(lang_java, _Code), _Instrs, !Info) :-
-    unexpected(this_file, "lang_java").
-atomic_statement_to_il(inline_target_code(lang_java_bytecode, _), _, !Info) :-
-    unexpected(this_file, "lang_java_bytecode").
-atomic_statement_to_il(inline_target_code(lang_java_asm, _), _, !Info) :-
-    unexpected(this_file, "lang_java_asm").
-atomic_statement_to_il(inline_target_code(lang_asm, _), _, !Info) :-
-    unexpected(this_file, "lang_asm").
-atomic_statement_to_il(inline_target_code(lang_GNU_C, _), _, !Info) :-
-    unexpected(this_file, "lang_GNU_C").
-atomic_statement_to_il(inline_target_code(lang_C_minus_minus, _), _, !Info) :-
-    unexpected(this_file, "lang_C_minus_minus").
-
-atomic_statement_to_il(trail_op(_), node(Instrs), !Info) :-
-    Instrs = [comment("... some trail operation ... (unimplemented)")].
+atomic_statement_to_il(inline_target_code(ml_target_c, _Code), _Instrs,
+        !Info) :-
+    unexpected(this_file, "ml_target_c").
+atomic_statement_to_il(inline_target_code(ml_target_java, _Code), _Instrs,
+        !Info) :-
+    unexpected(this_file, "ml_target_java").
+atomic_statement_to_il(inline_target_code(ml_target_asm, _), _, !Info) :-
+    unexpected(this_file,  "ml_target_asm").
+atomic_statement_to_il(inline_target_code(ml_target_gnu_c, _), _, !Info) :-
+    unexpected(this_file, "ml_target_gnu_c").
+
+    % NOTE: for the MLDS backends trail ops are currently implemented by
+    % the HLDS->HLDS transformation in add_trail_ops.m.  If we encounter
+    % an MLDS trail op it's an error.
+    %
+atomic_statement_to_il(trail_op(_), _, _, _) :-
+    unexpected(this_file, "trail ops").

  atomic_statement_to_il(assign(Lval, Rval), Instrs, !Info) :-
      % Do assignments by loading the rval and storing to the lval.
Index: compiler/mlds_to_java.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mlds_to_java.m,v
retrieving revision 1.85
diff -u -r1.85 mlds_to_java.m
--- compiler/mlds_to_java.m	2 Oct 2006 05:21:17 -0000	1.85
+++ compiler/mlds_to_java.m	4 Oct 2006 06:53:20 -0000
@@ -473,12 +473,17 @@

  output_java_decl(Indent, DeclCode, !IO) :-
      DeclCode = foreign_decl_code(Lang, _IsLocal, Code, Context),
-    ( Lang = lang_java ->
+    (   Lang = lang_java,
          indent_line(mlds_make_context(Context), Indent, !IO),
          io.write_string(Code, !IO),
          io.nl(!IO)
      ;
-        sorry(this_file, "foreign code other than Java")
+        ( Lang = lang_c
+        ; Lang = lang_csharp
+        ; Lang = lang_managed_cplusplus
+        ; Lang = lang_il
+        ),
+        sorry(this_file, "foreign decl other than Java")
      ).

  :- pred output_java_body_code(indent::in, user_foreign_code::in, io::di,
@@ -486,11 +491,17 @@

  output_java_body_code(Indent, user_foreign_code(Lang, Code, Context), !IO) :-
      % Only output Java code.
-    ( Lang = lang_java ->
+    ( 
+        Lang = lang_java,
          indent_line(mlds_make_context(Context), Indent, !IO),
          io.write_string(Code, !IO),
          io.nl(!IO)
      ;
+        ( Lang = lang_c
+        ; Lang = lang_csharp
+        ; Lang = lang_managed_cplusplus
+        ; Lang = lang_il
+        ),
          sorry(this_file, "foreign code other than Java")
      ).

@@ -2827,22 +2838,28 @@
  output_atomic_stmt(_Indent, _, _FuncInfo, restore_hp(_Rval), _, _, _) :-
      unexpected(this_file, "restore_hp not implemented.").

-    % trail management
+    % Trail management.
      %
  output_atomic_stmt(_Indent, _, _FuncInfo, trail_op(_TrailOp), _, _, _) :-
      unexpected(this_file, "trail_ops not implemented.").

-    % foreign language interfacing
+    % Foreign language interfacing.
      %
  output_atomic_stmt(Indent, ModuleInfo, FuncInfo,
          inline_target_code(TargetLang, Components), Context, !IO) :-
-    ( TargetLang = lang_java ->
+    ( 
+        TargetLang = ml_target_java,
          indent_line(Indent, !IO),
          ModuleName = FuncInfo ^ func_info_name ^ mod_name,
          list.foldl(
              output_target_code_component(ModuleInfo, ModuleName, Context),
              Components, !IO)
      ;
+        ( TargetLang = ml_target_c
+        ; TargetLang = ml_target_gnu_c
+        ; TargetLang = ml_target_asm
+        ; TargetLang = ml_target_il
+        ),
          unexpected(this_file, "inline_target_code only works for lang_java")
      ).

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list