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 3918 - (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 : jhr 3908 fun mkEnv spec = if TargetSpec.dualState spec
26 :     then Env.new {
27 :     global = RN.globalsVar,
28 :     selfIn = RN.selfInVar,
29 :     selfOut = RN.selfOutVar,
30 :     spec = spec
31 :     }
32 :     else Env.new {
33 :     global = RN.globalsVar,
34 :     selfIn = RN.selfVar,
35 :     selfOut = RN.selfVar,
36 :     spec = spec
37 :     }
38 :    
39 : jhr 3905 (* create the target-specific substitution list *)
40 :     fun mkSubs (spec, IR.Strand{name, ...}) = [
41 :     ("CFILE", OS.Path.joinBaseExt{base= #outBase spec, ext= SOME "c"}),
42 :     ("CXXFILE", OS.Path.joinBaseExt{base= #outBase spec, ext= SOME "cxx"}),
43 :     ("HDRFILE", OS.Path.joinBaseExt{base= #outBase spec, ext= SOME "h"}),
44 :     ("PREFIX", #namespace spec),
45 :     ("SRCFILE", #srcFile spec),
46 :     ("PROG_NAME", #outBase spec),
47 :     ("STRAND", Atom.toString name),
48 :     ("STRANDTY", Atom.toString name ^ "_strand"),
49 :     ("DIDEROTC_CMD", #diderotc spec),
50 :     ("DIDEROTC_ARGV", String.concatWith " " (#argv spec)),
51 :     ("DIDEROTC_VERSION", #version spec),
52 :     ("DIDEROT_FLOAT_PRECISION", TargetSpec.floatPrecisionDef spec),
53 :     ("DIDEROT_INT_PRECISION", TargetSpec.intPrecisionDef spec),
54 : jhr 3916 ("DIDEROT_TARGET", TargetSpec.targetDef spec),
55 :     ("REALTY", if #double spec then "double" else "float"),
56 :     ("INTTY", if #longint spec then "int64_t" else "int32_t")
57 : jhr 3905 ]
58 :    
59 :     fun condCons (true, x, xs) = x::xs
60 :     | condCons (false, _, xs) = xs
61 :    
62 : jhr 3906 fun verbFrag (spec, parFrag, seqFrag, subs) =
63 :     CL.verbatimDcl [if (TargetSpec.isParallel spec) then parFrag else seqFrag] subs
64 : jhr 3905
65 : jhr 3917 (* generate the struct declaration for the global variables *)
66 : jhr 3918 (* FIXME: what about constants? *)
67 :     fun genGlobalStruct (env, consts, inputs, globals) =
68 :     if #hasGlobals(Env.target env)
69 : jhr 3917 then let
70 : jhr 3918 fun mkField gv = (TreeToCxx.trType(env, TreeGlobalVar.ty gv), TreeGlobalVar.name gv)
71 :     val fields = List.map (mkField o Inputs.varOf) inputs
72 :     val fields = fields @ List.map mkField globals
73 : jhr 3917 in
74 :     [CL.D_StructDef(SOME "globals", fields, NONE)]
75 :     end
76 :     else []
77 :    
78 :     (* generate the struct declaration for the world representation *)
79 :     fun genWorldStruct (spec : TargetSpec.t, strandName) = let
80 :     fun memberVar (ty, name) = CL.D_Var([], ty, name, NONE)
81 :     val members = []
82 :     val statePtrTy = CL.T_Ptr(CL.T_Ptr(CL.T_Named(Atom.toString strandName ^ "_strand")))
83 :     val members = if TargetSpec.dualState spec
84 :     then memberVar(statePtrTy, "_inState") ::
85 :     memberVar(statePtrTy, "_inState") :: members
86 :     else memberVar(statePtrTy, "_state") :: members
87 :     val members = memberVar(CL.T_Ptr CL.uint8, "_status") :: members
88 :     val members = if #hasGlobals spec
89 :     then memberVar(RN.globalPtrTy, "_globals") :: members
90 :     else members
91 :     val members = if #exec spec orelse not(#hasInputs spec)
92 :     then members
93 :     else memberVar(CL.T_Named "inputs", "_definedInp") :: members
94 :     val members = if TargetSpec.isParallel spec
95 :     then memberVar(CL.T_Ptr(CL.T_Named "sched_info"), "_sched") :: members
96 :     else memberVar(CL.T_Named "uint32_t", "_nactive") ::
97 :     memberVar(CL.T_Named "uint32_t", "_nstable") :: members
98 :     (* add world method decls *)
99 :     fun memberFun (ty, name, params) = CL.D_Proto([], ty, name, params)
100 :     val members =
101 :     memberFun (CL.voidTy, "swap_state", []) ::
102 :     memberFun (CL.uint32, "run", [CL.PARAM([], CL.uint32, "max_nsteps")]) ::
103 :     memberFun (CL.voidTy, "initially", []) ::
104 :     CL.D_Destr([], NONE, "world", NONE) ::
105 :     CL.D_Constr([], NONE, "world", [], [], NONE) ::
106 :     members
107 :     in
108 :     CL.D_ClassDef{
109 :     name = "world",
110 :     from = SOME "public diderot::world_base",
111 :     public = List.rev members,
112 :     protected = [],
113 :     private = []
114 :     }
115 :     end
116 :    
117 :     fun runFrag (spec, subs) = if TargetSpec.noBSP spec
118 :     then verbFrag (spec, Fragments.parallelRunNoBSP, Fragments.sequentialRunNoBSP, subs)
119 :     else verbFrag (spec, Fragments.parallelRun, Fragments.sequentialRun, subs)
120 :    
121 : jhr 3905 fun compile (spec : TargetSpec.t, basename) = let
122 :     (* generate the C compiler flags *)
123 :     val cflags = ["-I" ^ Paths.diderotInclude(), "-I" ^ Paths.teemInclude()]
124 : jhr 3914 val cflags = condCons (TargetSpec.isParallel spec, #pthread Paths.cxxflags, cflags)
125 : jhr 3906 val cflags = if #debug spec
126 : jhr 3914 then #debug Paths.cxxflags :: cflags
127 :     else #ndebug Paths.cxxflags :: cflags
128 :     val cflags = #base Paths.cxxflags :: cflags
129 : jhr 3905 in
130 :     RunCC.compile (basename, cflags)
131 :     end
132 :    
133 : jhr 3906 fun ldFlags (spec : TargetSpec.t) = if #exec spec
134 : jhr 3905 then let
135 : jhr 3906 val extraLibs = condCons (TargetSpec.isParallel spec, #pthread Paths.extraLibs, [])
136 : jhr 3905 val extraLibs = Paths.teemLinkFlags() @ #base Paths.extraLibs :: extraLibs
137 : jhr 3906 val rtLib = TargetSpec.runtimeLibName spec
138 : jhr 3905 in
139 : jhr 3914 condCons (TargetSpec.isParallel spec, #pthread Paths.cxxflags, rtLib :: extraLibs)
140 : jhr 3905 end
141 : jhr 3906 else [TargetSpec.runtimeLibName spec]
142 : jhr 3905
143 : jhr 3912 (* generate source code that is common to both libraries and standalone executables *)
144 : jhr 3917 fun outputSrc (outS, env, spec, prog, strand, substitutions, genInputCode) = let
145 : jhr 3918 val IR.Program{consts, inputs, globals, create=IR.Create{dim, ...}, ...} = prog
146 : jhr 3917 val IR.Strand{name=strandName, ...} = strand
147 :     val ppDecl = Out.decl outS
148 : jhr 3918 val {structDef, methods} = GenStrand.gen (env, strand)
149 : jhr 3917 val outputs = OutputUtil.gatherOutputs prog
150 : jhr 3912 in
151 :     if (TargetSpec.isParallel spec)
152 : jhr 3917 then ppDecl (CL.verbatimDcl [Fragments.parallelExtras] substitutions)
153 : jhr 3912 else ();
154 :     if TargetSpec.dualState spec
155 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_DUAL_STATE\n"])
156 :     else ();
157 :     if not(#hasGlobals spec)
158 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_GLOBALS\n"])
159 :     else ();
160 :     if not(#hasInputs spec)
161 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_INPUTS\n"])
162 :     else ();
163 : jhr 3918 List.app ppDecl (genGlobalStruct (env, consts, inputs, globals));
164 :     ppDecl structDef;
165 : jhr 3917 ppDecl (genWorldStruct(spec, strandName));
166 : jhr 3912 List.app ppDecl (genInputCode());
167 : jhr 3917 (* FIXME: need to gather/generate topDecls
168 : jhr 3912 List.app ppDecl (!topDecls);
169 : jhr 3917 *)
170 : jhr 3918 List.app ppDecl methods;
171 : jhr 3917 List.app ppDecl (GenOutputs.gen (env, dim, outputs));
172 :     ppDecl (CL.verbatimDcl [Fragments.worldMethods] substitutions);
173 :     (* TODO
174 : jhr 3912 ppDecl (CL.verbatimDcl [InitFrag.text] substitutions);
175 :     ppDecl (CL.verbatimDcl [AllocFrag.text] substitutions);
176 :     ppDecl (!initially);
177 : jhr 3917 *)
178 :     ppDecl (runFrag (spec, substitutions))
179 : jhr 3912 end
180 : jhr 3917
181 : jhr 3905 (********************
182 : jhr 3903 fun outputLibSrc (baseName, prog as Prog{props, inputs, strands, ...}) = let
183 :     val [strand] = AtomTable.listItems strands
184 :     val substitutions = mkSubs (props, strand)
185 :     (* output to C file *)
186 :     val out = openOut baseName
187 :     fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
188 :     in
189 :     ppDecl (CL.verbatimDcl [LibHdrFrag.text] substitutions);
190 :     List.app ppDecl (GenInputs.genDefinedInpStruct (props, !inputs));
191 :     outputSrc (out, prog, strand, substitutions,
192 :     fn () => GenInputs.genInputFuns(props, !inputs));
193 :     ppDecl (CL.verbatimDcl [CBodyFrag.text] substitutions);
194 :     closeOut out
195 :     end
196 :    
197 :     fun generateLib (prog as Prog{props, inputs, strands, ...}) = let
198 : jhr 3906 val {outDir, outBase, exec, double, debug, ...} = props
199 : jhr 3903 val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
200 :     val [Strand{state, output, ...}] = AtomTable.listItems strands
201 :     in
202 :     (* generate the library .h file *)
203 :     GenLibraryInterface.gen {
204 :     props = props,
205 : jhr 3906 rt = if TargetSpec.isParallel spec
206 : jhr 3903 then SOME LibInterfaceParFrag.text
207 :     else NONE,
208 :     inputs = !inputs,
209 :     outputs = output
210 :     };
211 :     (* *)
212 :     outputLibSrc (basename, prog);
213 :     (* compile and link *)
214 :     compile (props, basename);
215 :     RunCC.linkLib (basename, ldFlags props)
216 :     end
217 : jhr 3905 ********************)
218 : jhr 3903
219 : jhr 3902 fun exec (spec : TargetSpec.t, prog) = let
220 : jhr 3912 val IR.Program{inputs, strand, ...} = prog
221 : jhr 3908 val env = mkEnv spec
222 : jhr 3905 val baseName = OS.Path.joinDirFile{dir = #outDir spec, file = #outBase spec}
223 : jhr 3906 val substitutions = mkSubs (spec, strand)
224 : jhr 3903 (* output to C++ file *)
225 : jhr 3905 val outS = openCxxOut baseName
226 :     val ppDecl = Out.decl outS
227 :     val fragment = Out.fragment substitutions outS
228 : jhr 3902 in
229 : jhr 3912 GenExecUtil.execHead (outS, substitutions);
230 : jhr 3918 List.app ppDecl (GenTysAndOps.gen (env, CollectInfo.collect prog));
231 : jhr 3917 outputSrc (outS, env, spec, prog, strand, substitutions,
232 : jhr 3903 fn () => (
233 : jhr 3906 GenInputs.genInputsStruct (env, inputs) @
234 : jhr 3912 GenInputs.genExecInputFuns (env, prog) @
235 :     OutputUtil.genRegisterOutputOpts (env, OutputUtil.gatherOutputs prog)));
236 :     ppDecl (verbFrag (spec, Fragments.parallelMain, Fragments.sequentialMain, substitutions));
237 : jhr 3916 GenExecUtil.execFoot (outS, substitutions);
238 : jhr 3906 Out.closeOut outS;
239 :     compile (spec, baseName);
240 :     RunCC.linkExec (baseName, ldFlags spec)
241 : jhr 3902 end
242 : jhr 3908
243 : jhr 3902 fun library (spec : TargetSpec.t, prog) = let
244 :     val IR.Program{
245 :     props, target, consts, inputs, constInit,
246 :     globals, globalInit, strand, create, update
247 :     } = prog
248 : jhr 3906 val {outDir, outBase, exec, double, debug, ...} = spec
249 : jhr 3908 val env = mkEnv spec
250 : jhr 3902 in
251 :     raise Fail "FIXME"
252 :     end
253 : jhr 3908
254 : jhr 3902 end

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