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 3954 - (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 : jhr 3924 structure GV = TreeGlobalVar
19 : jhr 3902 structure CL = CLang
20 : jhr 3905 structure Env = CodeGenEnv
21 :     structure Out = CodeOutput
22 : jhr 3906 structure RN = CxxNames
23 : jhr 3902
24 : jhr 3905 val openCxxOut = Out.openOut {ext = "cxx", ppDecl = PrintAsCxx.output}
25 :    
26 : jhr 3908 fun mkEnv spec = if TargetSpec.dualState spec
27 :     then Env.new {
28 :     global = RN.globalsVar,
29 :     selfIn = RN.selfInVar,
30 :     selfOut = RN.selfOutVar,
31 :     spec = spec
32 :     }
33 :     else Env.new {
34 :     global = RN.globalsVar,
35 :     selfIn = RN.selfVar,
36 :     selfOut = RN.selfVar,
37 :     spec = spec
38 :     }
39 :    
40 : jhr 3905 (* create the target-specific substitution list *)
41 : jhr 3954 fun mkSubs (spec, strand, IR.Create{dim, ...}) = let
42 :     val IR.Strand{name, stateInit, initM, updateM, stabilizeM, ...} = strand
43 :     fun mkMethodArgs (name, NONE) = (name, "")
44 :     | mkMethodArgs (name, SOME(IR.Method{usesGlobals, needsWorld, ...})) = let
45 :     val value = if needsWorld then "this, " else ""
46 :     val value = if usesGlobals then value ^ "glob, " else value
47 :     in
48 :     (name, value)
49 :     end
50 :     in [
51 : jhr 3905 ("CFILE", OS.Path.joinBaseExt{base= #outBase spec, ext= SOME "c"}),
52 :     ("CXXFILE", OS.Path.joinBaseExt{base= #outBase spec, ext= SOME "cxx"}),
53 :     ("HDRFILE", OS.Path.joinBaseExt{base= #outBase spec, ext= SOME "h"}),
54 :     ("PREFIX", #namespace spec),
55 :     ("SRCFILE", #srcFile spec),
56 :     ("PROG_NAME", #outBase spec),
57 :     ("STRAND", Atom.toString name),
58 :     ("STRANDTY", Atom.toString name ^ "_strand"),
59 : jhr 3924 ("IS_ARRAY", Bool.toString(#isArray spec)),
60 :     ("NUM_AXES", Int.toString(Option.getOpt(dim, 1))),
61 : jhr 3905 ("DIDEROTC_CMD", #diderotc spec),
62 :     ("DIDEROTC_ARGV", String.concatWith " " (#argv spec)),
63 :     ("DIDEROTC_VERSION", #version spec),
64 :     ("DIDEROT_FLOAT_PRECISION", TargetSpec.floatPrecisionDef spec),
65 :     ("DIDEROT_INT_PRECISION", TargetSpec.intPrecisionDef spec),
66 : jhr 3916 ("DIDEROT_TARGET", TargetSpec.targetDef spec),
67 :     ("REALTY", if #double spec then "double" else "float"),
68 : jhr 3954 ("INTTY", if #longint spec then "int64_t" else "int32_t"),
69 :     mkMethodArgs ("STATE_INIT_ARGS", SOME stateInit),
70 :     mkMethodArgs ("INITIALLY_ARGS", initM),
71 :     mkMethodArgs ("UPDATE_ARGS", SOME updateM),
72 :     mkMethodArgs ("STABILIZE_ARGS", stabilizeM)
73 :     ] end
74 : jhr 3905
75 :     fun condCons (true, x, xs) = x::xs
76 :     | condCons (false, _, xs) = xs
77 :    
78 : jhr 3906 fun verbFrag (spec, parFrag, seqFrag, subs) =
79 :     CL.verbatimDcl [if (TargetSpec.isParallel spec) then parFrag else seqFrag] subs
80 : jhr 3905
81 : jhr 3917 (* generate the struct declaration for the global variables *)
82 : jhr 3918 (* FIXME: what about constants? *)
83 :     fun genGlobalStruct (env, consts, inputs, globals) =
84 :     if #hasGlobals(Env.target env)
85 : jhr 3917 then let
86 : jhr 3924 fun mkField gv = (TreeToCxx.trType(env, GV.ty gv), GV.qname gv)
87 : jhr 3918 val fields = List.map (mkField o Inputs.varOf) inputs
88 :     val fields = fields @ List.map mkField globals
89 : jhr 3917 in
90 :     [CL.D_StructDef(SOME "globals", fields, NONE)]
91 :     end
92 :     else []
93 :    
94 : jhr 3931 fun genInitGlobals (env, globalInit) =
95 :     CL.D_Func(["static"], CL.boolTy, "init_globals",
96 :     [CL.PARAM(["const"], RN.worldPtrTy, RN.worldVar)],
97 :     (* FIXME: need to generate code from globalInit *)
98 :     CL.mkReturn(SOME(CL.mkVar "false")))
99 :    
100 : jhr 3917 fun runFrag (spec, subs) = if TargetSpec.noBSP spec
101 :     then verbFrag (spec, Fragments.parallelRunNoBSP, Fragments.sequentialRunNoBSP, subs)
102 :     else verbFrag (spec, Fragments.parallelRun, Fragments.sequentialRun, subs)
103 :    
104 : jhr 3905 fun compile (spec : TargetSpec.t, basename) = let
105 :     (* generate the C compiler flags *)
106 :     val cflags = ["-I" ^ Paths.diderotInclude(), "-I" ^ Paths.teemInclude()]
107 : jhr 3914 val cflags = condCons (TargetSpec.isParallel spec, #pthread Paths.cxxflags, cflags)
108 : jhr 3906 val cflags = if #debug spec
109 : jhr 3914 then #debug Paths.cxxflags :: cflags
110 :     else #ndebug Paths.cxxflags :: cflags
111 :     val cflags = #base Paths.cxxflags :: cflags
112 : jhr 3905 in
113 :     RunCC.compile (basename, cflags)
114 :     end
115 :    
116 : jhr 3906 fun ldFlags (spec : TargetSpec.t) = if #exec spec
117 : jhr 3905 then let
118 : jhr 3906 val extraLibs = condCons (TargetSpec.isParallel spec, #pthread Paths.extraLibs, [])
119 : jhr 3905 val extraLibs = Paths.teemLinkFlags() @ #base Paths.extraLibs :: extraLibs
120 : jhr 3906 val rtLib = TargetSpec.runtimeLibName spec
121 : jhr 3905 in
122 : jhr 3914 condCons (TargetSpec.isParallel spec, #pthread Paths.cxxflags, rtLib :: extraLibs)
123 : jhr 3905 end
124 : jhr 3906 else [TargetSpec.runtimeLibName spec]
125 : jhr 3905
126 : jhr 3912 (* generate source code that is common to both libraries and standalone executables *)
127 : jhr 3917 fun outputSrc (outS, env, spec, prog, strand, substitutions, genInputCode) = let
128 : jhr 3931 val IR.Program{
129 :     consts, inputs, globals, globalInit, create as IR.Create{dim, ...}, ...
130 :     } = prog
131 : jhr 3917 val IR.Strand{name=strandName, ...} = strand
132 :     val ppDecl = Out.decl outS
133 : jhr 3918 val {structDef, methods} = GenStrand.gen (env, strand)
134 : jhr 3917 val outputs = OutputUtil.gatherOutputs prog
135 : jhr 3912 in
136 :     if (TargetSpec.isParallel spec)
137 : jhr 3917 then ppDecl (CL.verbatimDcl [Fragments.parallelExtras] substitutions)
138 : jhr 3912 else ();
139 :     if TargetSpec.dualState spec
140 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_DUAL_STATE\n"])
141 :     else ();
142 :     if not(#hasGlobals spec)
143 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_GLOBALS\n"])
144 :     else ();
145 :     if not(#hasInputs spec)
146 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_INPUTS\n"])
147 :     else ();
148 : jhr 3918 List.app ppDecl (genGlobalStruct (env, consts, inputs, globals));
149 :     ppDecl structDef;
150 : jhr 3924 ppDecl (GenWorld.genStruct(spec, strandName, Option.getOpt(dim, 1)));
151 : jhr 3912 List.app ppDecl (genInputCode());
152 : jhr 3931 ppDecl (genInitGlobals (env, globalInit));
153 : jhr 3918 List.app ppDecl methods;
154 : jhr 3917 List.app ppDecl (GenOutputs.gen (env, dim, outputs));
155 :     ppDecl (CL.verbatimDcl [Fragments.worldMethods] substitutions);
156 : jhr 3924 ppDecl (GenWorld.genInitiallyFun (env, strandName, create));
157 : jhr 3917 ppDecl (runFrag (spec, substitutions))
158 : jhr 3912 end
159 : jhr 3931
160 : jhr 3905 (********************
161 : jhr 3903 fun outputLibSrc (baseName, prog as Prog{props, inputs, strands, ...}) = let
162 :     val [strand] = AtomTable.listItems strands
163 :     val substitutions = mkSubs (props, strand)
164 :     (* output to C file *)
165 :     val out = openOut baseName
166 :     fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
167 :     in
168 :     ppDecl (CL.verbatimDcl [LibHdrFrag.text] substitutions);
169 :     List.app ppDecl (GenInputs.genDefinedInpStruct (props, !inputs));
170 :     outputSrc (out, prog, strand, substitutions,
171 :     fn () => GenInputs.genInputFuns(props, !inputs));
172 :     ppDecl (CL.verbatimDcl [CBodyFrag.text] substitutions);
173 :     closeOut out
174 :     end
175 :    
176 :     fun generateLib (prog as Prog{props, inputs, strands, ...}) = let
177 : jhr 3906 val {outDir, outBase, exec, double, debug, ...} = props
178 : jhr 3903 val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
179 :     val [Strand{state, output, ...}] = AtomTable.listItems strands
180 :     in
181 :     (* generate the library .h file *)
182 :     GenLibraryInterface.gen {
183 :     props = props,
184 : jhr 3906 rt = if TargetSpec.isParallel spec
185 : jhr 3903 then SOME LibInterfaceParFrag.text
186 :     else NONE,
187 :     inputs = !inputs,
188 :     outputs = output
189 :     };
190 :     (* *)
191 :     outputLibSrc (basename, prog);
192 :     (* compile and link *)
193 :     compile (props, basename);
194 :     RunCC.linkLib (basename, ldFlags props)
195 :     end
196 : jhr 3905 ********************)
197 : jhr 3903
198 : jhr 3902 fun exec (spec : TargetSpec.t, prog) = let
199 : jhr 3924 val IR.Program{inputs, strand, create, ...} = prog
200 : jhr 3908 val env = mkEnv spec
201 : jhr 3905 val baseName = OS.Path.joinDirFile{dir = #outDir spec, file = #outBase spec}
202 : jhr 3924 val substitutions = mkSubs (spec, strand, create)
203 : jhr 3903 (* output to C++ file *)
204 : jhr 3905 val outS = openCxxOut baseName
205 :     val ppDecl = Out.decl outS
206 :     val fragment = Out.fragment substitutions outS
207 : jhr 3902 in
208 : jhr 3912 GenExecUtil.execHead (outS, substitutions);
209 : jhr 3918 List.app ppDecl (GenTysAndOps.gen (env, CollectInfo.collect prog));
210 : jhr 3917 outputSrc (outS, env, spec, prog, strand, substitutions,
211 : jhr 3903 fn () => (
212 : jhr 3906 GenInputs.genInputsStruct (env, inputs) @
213 : jhr 3912 GenInputs.genExecInputFuns (env, prog) @
214 :     OutputUtil.genRegisterOutputOpts (env, OutputUtil.gatherOutputs prog)));
215 : jhr 3924 GenExecUtil.execFoot (outS, substitutions);
216 :     (* generate main function after closing off the namespace *)
217 : jhr 3912 ppDecl (verbFrag (spec, Fragments.parallelMain, Fragments.sequentialMain, substitutions));
218 : jhr 3906 Out.closeOut outS;
219 :     compile (spec, baseName);
220 :     RunCC.linkExec (baseName, ldFlags spec)
221 : jhr 3902 end
222 : jhr 3908
223 : jhr 3902 fun library (spec : TargetSpec.t, prog) = let
224 :     val IR.Program{
225 :     props, target, consts, inputs, constInit,
226 :     globals, globalInit, strand, create, update
227 :     } = prog
228 : jhr 3906 val {outDir, outBase, exec, double, debug, ...} = spec
229 : jhr 3908 val env = mkEnv spec
230 : jhr 3902 in
231 :     raise Fail "FIXME"
232 :     end
233 : jhr 3908
234 : jhr 3902 end

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