Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/vis15/src/compiler/target-cpu/gen.sml
ViewVC logotype

View of /branches/vis15/src/compiler/target-cpu/gen.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3903 - (download) (annotate)
Mon May 23 18:41:05 2016 UTC (3 years, 1 month ago) by jhr
File size: 5215 byte(s)
working on merge: code generation
(* gen.sml
 *
 * Code generation for the sequential and parallel targets.
 *
 * COPYRIGHT (c) 2016 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *)

structure Gen : sig

    val exec : TargetSpec.t * TreeIR.program -> unit

    val library : TargetSpec.t * TreeIR.program -> unit

  end = struct

    structure IR = TreeIR
    structure CL = CLang

      (* generate source code that is common to both libraries and standalone executables *)
        fun outputSrc (out, prog, strand, substitutions, genInputCode) = let
              val Prog{props, globals, topDecls, nAxes, initially, ...} = prog
            (* does the program require barrier synchronization to implement BSP semantics? *)
              val noBSP = Properties.noBSP props
              val Strand{output, ...} = strand
              val outputs = GenOutput.gen (props, !nAxes) output
              fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
              in
                if (#parallel props)
                  then ppDecl (CL.verbatimDcl [CHeadParExtraFrag.text] substitutions)
                  else ();
                if Properties.dualState props
                  then ppDecl (CL.D_Verbatim ["#define DIDEROT_DUAL_STATE\n"])
                  else ();
                if not(#hasGlobals props)
                  then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_GLOBALS\n"])
                  else ();
                if not(#hasInputs props)
                  then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_INPUTS\n"])
                  else ();
                List.app ppDecl (genGlobalStruct (props, !globals));
                ppDecl (genWorldStruct(prog, strand));
                List.app ppDecl (genInputCode());
                List.app ppDecl (!topDecls);
                List.app ppDecl (genStrand strand);
                List.app ppDecl outputs;
                ppStrandTable (out, [strand]);
                ppDecl (CL.verbatimDcl [InitFrag.text] substitutions);
                ppDecl (CL.verbatimDcl [AllocFrag.text] substitutions);
                ppDecl (!initially);
                ppDecl (runFrag (noBSP, props, substitutions));
                ppDecl (CL.verbatimDcl [ShutdownFrag.text] substitutions)
              end

        fun outputLibSrc (baseName, prog as Prog{props, inputs, strands, ...}) = let
              val [strand] = AtomTable.listItems strands
              val substitutions = mkSubs (props, strand)
            (* output to C file *)
              val out = openOut baseName
              fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
              in
                ppDecl (CL.verbatimDcl [LibHdrFrag.text] substitutions);
                List.app ppDecl (GenInputs.genDefinedInpStruct (props, !inputs));
                outputSrc (out, prog, strand, substitutions,
                  fn () => GenInputs.genInputFuns(props, !inputs));
                ppDecl (CL.verbatimDcl [CBodyFrag.text] substitutions);
                closeOut out
              end

        fun generateLib (prog as Prog{props, inputs, strands, ...}) = let
              val {outDir, outBase, exec, double, parallel, debug, ...} = props
              val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
              val [Strand{state, output, ...}] = AtomTable.listItems strands
              in
              (* generate the library .h file *)
                GenLibraryInterface.gen {
                    props = props,
                    rt = if #parallel props
                      then SOME LibInterfaceParFrag.text
                      else NONE,
                    inputs = !inputs,
                    outputs = output
                  };
              (* *)
                outputLibSrc (basename, prog);
              (* compile and link *)
                compile (props, basename);
                RunCC.linkLib (basename, ldFlags props)
              end

    fun exec (spec : TargetSpec.t, prog) = let
	  val IR.Program{
		  props, target, consts, inputs, constInit,
		  globals, globalInit, strand, create, update
		} = prog
	  val {outDir, outBase, exec, double, parallel, debug, ...} = spec
	  val baseName = OS.Path.joinDirFile{dir=outDir, file=outBase}
	  val substitutions = mkSubs (props, strand)
	(* output to C++ file *)
	  val out = openOut baseName
	  fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
	  in
	    ppDecl (CL.verbatimDcl [ExecHdrFrag.text] substitutions);
	    ppDecl (CL.verbatimDcl [NrrdSaveHelperFrag.text] substitutions);
	    outputSrc (out, prog, strand, substitutions,
	      fn () => (
		GenInputs.genInputsStruct (props, !inputs) @
		GenInputs.genExecInputFuns (props, !inputs) @
		OutputUtil.genRegisterOutputOpts output));
	    ppDecl (verbFrag (props, ParMainFrag.text, SeqMainFrag.text, substitutions));
	    closeOut out;
	    compile (props, basename);
	    RunCC.linkExec (basename, ldFlags props)
	  end
   
    fun library (spec : TargetSpec.t, prog) = let
	  val IR.Program{
		  props, target, consts, inputs, constInit,
		  globals, globalInit, strand, create, update
		} = prog
	  val {outDir, outBase, exec, double, parallel, debug, ...} = spec
	  in
	    raise Fail "FIXME"
	  end
 
  end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0