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

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