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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3906 - (view) (download)

1 : jhr 3902 (* gen.sml
2 :     *
3 :     * Code generation for the sequential and parallel targets.
4 :     *
5 :     * COPYRIGHT (c) 2016 The Diderot Project (http://diderot-language.cs.uchicago.edu)
6 :     * All rights reserved.
7 :     *)
8 :    
9 :     structure Gen : sig
10 :    
11 :     val exec : TargetSpec.t * TreeIR.program -> unit
12 :    
13 :     val library : TargetSpec.t * TreeIR.program -> unit
14 :    
15 :     end = struct
16 :    
17 :     structure IR = TreeIR
18 :     structure CL = CLang
19 : jhr 3905 structure Env = CodeGenEnv
20 :     structure Out = CodeOutput
21 : jhr 3906 structure RN = CxxNames
22 : jhr 3902
23 : jhr 3905 val openCxxOut = Out.openOut {ext = "cxx", ppDecl = PrintAsCxx.output}
24 :    
25 :     (* create the target-specific substitution list *)
26 :     fun mkSubs (spec, IR.Strand{name, ...}) = [
27 :     ("CFILE", OS.Path.joinBaseExt{base= #outBase spec, ext= SOME "c"}),
28 :     ("CXXFILE", OS.Path.joinBaseExt{base= #outBase spec, ext= SOME "cxx"}),
29 :     ("HDRFILE", OS.Path.joinBaseExt{base= #outBase spec, ext= SOME "h"}),
30 :     ("PREFIX", #namespace spec),
31 :     ("SRCFILE", #srcFile spec),
32 :     ("PROG_NAME", #outBase spec),
33 :     ("STRAND", Atom.toString name),
34 :     ("STRANDTY", Atom.toString name ^ "_strand"),
35 :     ("DIDEROTC_CMD", #diderotc spec),
36 :     ("DIDEROTC_ARGV", String.concatWith " " (#argv spec)),
37 :     ("DIDEROTC_VERSION", #version spec),
38 :     ("DIDEROT_FLOAT_PRECISION", TargetSpec.floatPrecisionDef spec),
39 :     ("DIDEROT_INT_PRECISION", TargetSpec.intPrecisionDef spec),
40 :     ("DIDEROT_TARGET", TargetSpec.targetDef spec)
41 :     ]
42 :    
43 :     fun condCons (true, x, xs) = x::xs
44 :     | condCons (false, _, xs) = xs
45 :    
46 : jhr 3906 fun verbFrag (spec, parFrag, seqFrag, subs) =
47 :     CL.verbatimDcl [if (TargetSpec.isParallel spec) then parFrag else seqFrag] subs
48 : jhr 3905
49 :     fun runFrag (false, spec, subs) =
50 :     verbFrag (spec, ParRunFrag.text, SeqRunFrag.text, subs)
51 :     | runFrag (true, spec, subs) =
52 :     verbFrag (spec, ParRunNoBSPFrag.text, SeqRunNoBSPFrag.text, subs)
53 :    
54 :     fun compile (spec : TargetSpec.t, basename) = let
55 :     (* generate the C compiler flags *)
56 :     val cflags = ["-I" ^ Paths.diderotInclude(), "-I" ^ Paths.teemInclude()]
57 : jhr 3906 val cflags = condCons (TargetSpec.isParallel spec, #pthread Paths.cflags, cflags)
58 :     val cflags = if #debug spec
59 : jhr 3905 then #debug Paths.cflags :: cflags
60 :     else #ndebug Paths.cflags :: cflags
61 :     val cflags = #base Paths.cflags :: cflags
62 :     in
63 :     RunCC.compile (basename, cflags)
64 :     end
65 :    
66 : jhr 3906 fun ldFlags (spec : TargetSpec.t) = if #exec spec
67 : jhr 3905 then let
68 : jhr 3906 val extraLibs = condCons (TargetSpec.isParallel spec, #pthread Paths.extraLibs, [])
69 : jhr 3905 val extraLibs = Paths.teemLinkFlags() @ #base Paths.extraLibs :: extraLibs
70 : jhr 3906 val rtLib = TargetSpec.runtimeLibName spec
71 : jhr 3905 in
72 : jhr 3906 condCons (TargetSpec.isParallel spec, #pthread Paths.cflags, rtLib :: extraLibs)
73 : jhr 3905 end
74 : jhr 3906 else [TargetSpec.runtimeLibName spec]
75 : jhr 3905
76 :     (********************
77 : jhr 3903 (* generate source code that is common to both libraries and standalone executables *)
78 :     fun outputSrc (out, prog, strand, substitutions, genInputCode) = let
79 :     val Prog{props, globals, topDecls, nAxes, initially, ...} = prog
80 :     (* does the program require barrier synchronization to implement BSP semantics? *)
81 :     val noBSP = Properties.noBSP props
82 :     val Strand{output, ...} = strand
83 :     val outputs = GenOutput.gen (props, !nAxes) output
84 :     fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
85 :     in
86 : jhr 3906 if (TargetSpec.isParallel spec)
87 : jhr 3903 then ppDecl (CL.verbatimDcl [CHeadParExtraFrag.text] substitutions)
88 :     else ();
89 :     if Properties.dualState props
90 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_DUAL_STATE\n"])
91 :     else ();
92 :     if not(#hasGlobals props)
93 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_GLOBALS\n"])
94 :     else ();
95 :     if not(#hasInputs props)
96 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_INPUTS\n"])
97 :     else ();
98 :     List.app ppDecl (genGlobalStruct (props, !globals));
99 :     ppDecl (genWorldStruct(prog, strand));
100 :     List.app ppDecl (genInputCode());
101 :     List.app ppDecl (!topDecls);
102 :     List.app ppDecl (genStrand strand);
103 :     List.app ppDecl outputs;
104 :     ppStrandTable (out, [strand]);
105 :     ppDecl (CL.verbatimDcl [InitFrag.text] substitutions);
106 :     ppDecl (CL.verbatimDcl [AllocFrag.text] substitutions);
107 :     ppDecl (!initially);
108 :     ppDecl (runFrag (noBSP, props, substitutions));
109 :     ppDecl (CL.verbatimDcl [ShutdownFrag.text] substitutions)
110 :     end
111 :    
112 :     fun outputLibSrc (baseName, prog as Prog{props, inputs, strands, ...}) = let
113 :     val [strand] = AtomTable.listItems strands
114 :     val substitutions = mkSubs (props, strand)
115 :     (* output to C file *)
116 :     val out = openOut baseName
117 :     fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
118 :     in
119 :     ppDecl (CL.verbatimDcl [LibHdrFrag.text] substitutions);
120 :     List.app ppDecl (GenInputs.genDefinedInpStruct (props, !inputs));
121 :     outputSrc (out, prog, strand, substitutions,
122 :     fn () => GenInputs.genInputFuns(props, !inputs));
123 :     ppDecl (CL.verbatimDcl [CBodyFrag.text] substitutions);
124 :     closeOut out
125 :     end
126 :    
127 :     fun generateLib (prog as Prog{props, inputs, strands, ...}) = let
128 : jhr 3906 val {outDir, outBase, exec, double, debug, ...} = props
129 : jhr 3903 val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
130 :     val [Strand{state, output, ...}] = AtomTable.listItems strands
131 :     in
132 :     (* generate the library .h file *)
133 :     GenLibraryInterface.gen {
134 :     props = props,
135 : jhr 3906 rt = if TargetSpec.isParallel spec
136 : jhr 3903 then SOME LibInterfaceParFrag.text
137 :     else NONE,
138 :     inputs = !inputs,
139 :     outputs = output
140 :     };
141 :     (* *)
142 :     outputLibSrc (basename, prog);
143 :     (* compile and link *)
144 :     compile (props, basename);
145 :     RunCC.linkLib (basename, ldFlags props)
146 :     end
147 : jhr 3905 ********************)
148 : jhr 3903
149 : jhr 3902 fun exec (spec : TargetSpec.t, prog) = let
150 :     val IR.Program{
151 :     props, target, consts, inputs, constInit,
152 :     globals, globalInit, strand, create, update
153 :     } = prog
154 : jhr 3906 val {outDir, outBase, exec, double, debug, ...} = spec
155 :     val env = Env.new {
156 :     global = RN.globalsVar,
157 :     selfIn = RN.selfInVar,
158 :     selfOut = if TargetSpec.dualState spec then RN.selfOutVar else RN.selfInVar,
159 :     spec = spec
160 :     }
161 : jhr 3905 val baseName = OS.Path.joinDirFile{dir = #outDir spec, file = #outBase spec}
162 : jhr 3906 val substitutions = mkSubs (spec, strand)
163 : jhr 3903 (* output to C++ file *)
164 : jhr 3905 val outS = openCxxOut baseName
165 :     val ppDecl = Out.decl outS
166 :     val fragment = Out.fragment substitutions outS
167 : jhr 3902 in
168 : jhr 3903 ppDecl (CL.verbatimDcl [ExecHdrFrag.text] substitutions);
169 :     ppDecl (CL.verbatimDcl [NrrdSaveHelperFrag.text] substitutions);
170 : jhr 3906 outputSrc (outS, prog, strand, substitutions,
171 : jhr 3903 fn () => (
172 : jhr 3906 GenInputs.genInputsStruct (env, inputs) @
173 :     GenInputs.genExecInputFuns (env, inputs) @
174 : jhr 3903 OutputUtil.genRegisterOutputOpts output));
175 : jhr 3906 ppDecl (verbFrag (spec, ParMainFrag.text, SeqMainFrag.text, substitutions));
176 :     Out.closeOut outS;
177 :     compile (spec, baseName);
178 :     RunCC.linkExec (baseName, ldFlags spec)
179 : jhr 3902 end
180 :    
181 :     fun library (spec : TargetSpec.t, prog) = let
182 :     val IR.Program{
183 :     props, target, consts, inputs, constInit,
184 :     globals, globalInit, strand, create, update
185 :     } = prog
186 : jhr 3906 val {outDir, outBase, exec, double, debug, ...} = spec
187 : jhr 3902 in
188 :     raise Fail "FIXME"
189 :     end
190 :    
191 :     end

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