[MLton-commit] r4360

Matthew Fluet MLton@mlton.org
Thu, 16 Feb 2006 10:25:09 -0800


Some refactoring from C-- branch
----------------------------------------------------------------------

U   mlton/trunk/mlton/main/main.fun

----------------------------------------------------------------------

Modified: mlton/trunk/mlton/main/main.fun
===================================================================
--- mlton/trunk/mlton/main/main.fun	2006-02-16 15:47:34 UTC (rev 4359)
+++ mlton/trunk/mlton/main/main.fun	2006-02-16 18:25:08 UTC (rev 4360)
@@ -770,7 +770,6 @@
                                (gcc,
                                 List.concat
                                 [targetOpts, 
-                                 ["-std=gnu99"],
                                  ["-o", output],
                                  if !debug then gccDebug else [],
                                  inputs,
@@ -796,6 +795,59 @@
                         in
                            ()
                         end
+                  fun mkOutputO (c: Counter.t, input: File.t): File.t =
+                     if stop = Place.O orelse !keepO
+                        then
+                           if !keepGenerated 
+                              orelse start = Place.Generated
+                              then
+                                 concat [File.base input,
+                                         ".o"]
+                              else 
+                                 suffix
+                                 (concat [".",
+                                          Int.toString
+                                          (Counter.next c),
+                                          ".o"])
+                        else temp ".o"
+                  fun compileC (c: Counter.t, input: File.t): File.t =
+                     let
+                        val (debugSwitches, switches) =
+                           (gccDebug @ ["-DASSERT=1"], ccOpts)
+                        val switches =
+                           if !debug
+                              then debugSwitches @ switches
+                              else switches
+                        val switches =
+                           targetOpts @ ("-std=gnu99" :: "-c" :: switches)
+                        val output = mkOutputO (c, input)
+                        val _ =
+                           System.system
+                           (gcc,
+                            List.concat [switches,
+                                         ["-o", output, input]])
+                     in
+                        output
+                     end
+                  fun compileS (c: Counter.t, input: File.t): File.t =
+                     let
+                        val (debugSwitches, switches) =
+                           ([asDebug], asOpts)
+                        val switches =
+                           if !debug
+                              then debugSwitches @ switches
+                              else switches
+                        val switches =
+                           targetOpts @ ("-c" :: switches)
+                        val output = mkOutputO (c, input)
+                        val _ =
+                           System.system
+                           (gcc,
+                            List.concat [switches,
+                                         ["-o", output, input]])
+                     in
+                        output
+                     end
                   fun compileCSO (inputs: File.t list): unit =
                      if List.forall (inputs, fn f =>
                                      SOME "o" = File.extension f)
@@ -804,7 +856,7 @@
                      let
                         val c = Counter.new 0
                         val oFiles =
-                           trace (Top, "Compile C and Assemble")
+                           trace (Top, "Compile and Assemble")
                            (fn () =>
                             List.fold
                             (inputs, [], fn (input, ac) =>
@@ -813,45 +865,15 @@
                              in
                                 if SOME "o" = extension
                                    then input :: ac
-                                else
-                                   let
-                                      val (debugSwitches, switches) =
-                                         if SOME "c" = extension
-                                            then
-                                               (gccDebug @ ["-DASSERT=1"],
-                                                ccOpts)
-                                         else ([asDebug], asOpts)
-                                      val switches =
-                                         if !debug
-                                            then debugSwitches @ switches
-                                         else switches
-                                      val switches =
-                                         targetOpts @ ("-std=gnu99" :: "-c" :: switches)
-                                      val output =
-                                         if stop = Place.O orelse !keepO
-                                            then
-                                               if !keepGenerated 
-                                                  orelse start = Place.Generated
-                                                  then
-                                                     concat [String.dropSuffix
-                                                             (input, 1),
-                                                             "o"]
-                                               else 
-                                                  suffix
-                                                  (concat [".",
-                                                           Int.toString
-                                                           (Counter.next c),
-                                                           ".o"])
-                                         else temp ".o"
-                                      val _ =
-                                         System.system
-                                         (gcc,
-                                          List.concat [switches,
-                                                       ["-o", output, input]])
-
-                                   in
-                                      output :: ac
-                                   end
+                                else if SOME "c" = extension
+                                   then (compileC (c, input)) :: ac
+                                else if SOME "s" = extension 
+                                        orelse SOME "S" = extension
+                                   then (compileS (c, input)) :: ac
+                                else Error.bug 
+                                     (concat
+                                      ["invalid extension: ",
+                                       Option.toString (fn s => s) extension])
                              end))
                            ()
                      in