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 3924 - (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 :     fun runFrag (spec, subs) = if TargetSpec.noBSP spec
82 :     then verbFrag (spec, Fragments.parallelRunNoBSP, Fragments.sequentialRunNoBSP, subs)
83 :     else verbFrag (spec, Fragments.parallelRun, Fragments.sequentialRun, subs)
84 :    
85 : jhr 3905 fun compile (spec : TargetSpec.t, basename) = let
86 :     (* generate the C compiler flags *)
87 :     val cflags = ["-I" ^ Paths.diderotInclude(), "-I" ^ Paths.teemInclude()]
88 : jhr 3914 val cflags = condCons (TargetSpec.isParallel spec, #pthread Paths.cxxflags, cflags)
89 : jhr 3906 val cflags = if #debug spec
90 : jhr 3914 then #debug Paths.cxxflags :: cflags
91 :     else #ndebug Paths.cxxflags :: cflags
92 :     val cflags = #base Paths.cxxflags :: cflags
93 : jhr 3905 in
94 :     RunCC.compile (basename, cflags)
95 :     end
96 :    
97 : jhr 3906 fun ldFlags (spec : TargetSpec.t) = if #exec spec
98 : jhr 3905 then let
99 : jhr 3906 val extraLibs = condCons (TargetSpec.isParallel spec, #pthread Paths.extraLibs, [])
100 : jhr 3905 val extraLibs = Paths.teemLinkFlags() @ #base Paths.extraLibs :: extraLibs
101 : jhr 3906 val rtLib = TargetSpec.runtimeLibName spec
102 : jhr 3905 in
103 : jhr 3914 condCons (TargetSpec.isParallel spec, #pthread Paths.cxxflags, rtLib :: extraLibs)
104 : jhr 3905 end
105 : jhr 3906 else [TargetSpec.runtimeLibName spec]
106 : jhr 3905
107 : jhr 3912 (* generate source code that is common to both libraries and standalone executables *)
108 : jhr 3917 fun outputSrc (outS, env, spec, prog, strand, substitutions, genInputCode) = let
109 : jhr 3924 val IR.Program{consts, inputs, globals, create as IR.Create{dim, ...}, ...} = prog
110 : jhr 3917 val IR.Strand{name=strandName, ...} = strand
111 :     val ppDecl = Out.decl outS
112 : jhr 3918 val {structDef, methods} = GenStrand.gen (env, strand)
113 : jhr 3917 val outputs = OutputUtil.gatherOutputs prog
114 : jhr 3912 in
115 :     if (TargetSpec.isParallel spec)
116 : jhr 3917 then ppDecl (CL.verbatimDcl [Fragments.parallelExtras] substitutions)
117 : jhr 3912 else ();
118 :     if TargetSpec.dualState spec
119 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_DUAL_STATE\n"])
120 :     else ();
121 :     if not(#hasGlobals spec)
122 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_GLOBALS\n"])
123 :     else ();
124 :     if not(#hasInputs spec)
125 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_INPUTS\n"])
126 :     else ();
127 : jhr 3918 List.app ppDecl (genGlobalStruct (env, consts, inputs, globals));
128 :     ppDecl structDef;
129 : jhr 3924 ppDecl (GenWorld.genStruct(spec, strandName, Option.getOpt(dim, 1)));
130 : jhr 3912 List.app ppDecl (genInputCode());
131 : jhr 3917 (* FIXME: need to gather/generate topDecls
132 : jhr 3912 List.app ppDecl (!topDecls);
133 : jhr 3917 *)
134 : jhr 3918 List.app ppDecl methods;
135 : jhr 3917 List.app ppDecl (GenOutputs.gen (env, dim, outputs));
136 :     ppDecl (CL.verbatimDcl [Fragments.worldMethods] substitutions);
137 : jhr 3924 ppDecl (GenWorld.genInitiallyFun (env, strandName, create));
138 : jhr 3917 ppDecl (runFrag (spec, substitutions))
139 : jhr 3912 end
140 : jhr 3917
141 : jhr 3905 (********************
142 : jhr 3903 fun outputLibSrc (baseName, prog as Prog{props, inputs, strands, ...}) = let
143 :     val [strand] = AtomTable.listItems strands
144 :     val substitutions = mkSubs (props, strand)
145 :     (* output to C file *)
146 :     val out = openOut baseName
147 :     fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
148 :     in
149 :     ppDecl (CL.verbatimDcl [LibHdrFrag.text] substitutions);
150 :     List.app ppDecl (GenInputs.genDefinedInpStruct (props, !inputs));
151 :     outputSrc (out, prog, strand, substitutions,
152 :     fn () => GenInputs.genInputFuns(props, !inputs));
153 :     ppDecl (CL.verbatimDcl [CBodyFrag.text] substitutions);
154 :     closeOut out
155 :     end
156 :    
157 :     fun generateLib (prog as Prog{props, inputs, strands, ...}) = let
158 : jhr 3906 val {outDir, outBase, exec, double, debug, ...} = props
159 : jhr 3903 val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
160 :     val [Strand{state, output, ...}] = AtomTable.listItems strands
161 :     in
162 :     (* generate the library .h file *)
163 :     GenLibraryInterface.gen {
164 :     props = props,
165 : jhr 3906 rt = if TargetSpec.isParallel spec
166 : jhr 3903 then SOME LibInterfaceParFrag.text
167 :     else NONE,
168 :     inputs = !inputs,
169 :     outputs = output
170 :     };
171 :     (* *)
172 :     outputLibSrc (basename, prog);
173 :     (* compile and link *)
174 :     compile (props, basename);
175 :     RunCC.linkLib (basename, ldFlags props)
176 :     end
177 : jhr 3905 ********************)
178 : jhr 3903
179 : jhr 3902 fun exec (spec : TargetSpec.t, prog) = let
180 : jhr 3924 val IR.Program{inputs, strand, create, ...} = prog
181 : jhr 3908 val env = mkEnv spec
182 : jhr 3905 val baseName = OS.Path.joinDirFile{dir = #outDir spec, file = #outBase spec}
183 : jhr 3924 val substitutions = mkSubs (spec, strand, create)
184 : jhr 3903 (* output to C++ file *)
185 : jhr 3905 val outS = openCxxOut baseName
186 :     val ppDecl = Out.decl outS
187 :     val fragment = Out.fragment substitutions outS
188 : jhr 3902 in
189 : jhr 3912 GenExecUtil.execHead (outS, substitutions);
190 : jhr 3918 List.app ppDecl (GenTysAndOps.gen (env, CollectInfo.collect prog));
191 : jhr 3917 outputSrc (outS, env, spec, prog, strand, substitutions,
192 : jhr 3903 fn () => (
193 : jhr 3906 GenInputs.genInputsStruct (env, inputs) @
194 : jhr 3912 GenInputs.genExecInputFuns (env, prog) @
195 :     OutputUtil.genRegisterOutputOpts (env, OutputUtil.gatherOutputs prog)));
196 : jhr 3924 GenExecUtil.execFoot (outS, substitutions);
197 :     (* generate main function after closing off the namespace *)
198 : jhr 3912 ppDecl (verbFrag (spec, Fragments.parallelMain, Fragments.sequentialMain, substitutions));
199 : jhr 3906 Out.closeOut outS;
200 :     compile (spec, baseName);
201 :     RunCC.linkExec (baseName, ldFlags spec)
202 : jhr 3902 end
203 : jhr 3908
204 : jhr 3902 fun library (spec : TargetSpec.t, prog) = let
205 :     val IR.Program{
206 :     props, target, consts, inputs, constInit,
207 :     globals, globalInit, strand, create, update
208 :     } = prog
209 : jhr 3906 val {outDir, outBase, exec, double, debug, ...} = spec
210 : jhr 3908 val env = mkEnv spec
211 : jhr 3902 in
212 :     raise Fail "FIXME"
213 :     end
214 : jhr 3908
215 : jhr 3902 end

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