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 3916 - (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 :     fun runFrag (false, spec, subs) =
66 : jhr 3912 verbFrag (spec, Fragments.parallelRun, Fragments.sequentialRun, subs)
67 : jhr 3908 | runFrag (true, spec, subs) =
68 : jhr 3912 verbFrag (spec, Fragments.parallelRunNoBSP, Fragments.sequentialRunNoBSP, subs)
69 : jhr 3905
70 :     fun compile (spec : TargetSpec.t, basename) = let
71 :     (* generate the C compiler flags *)
72 :     val cflags = ["-I" ^ Paths.diderotInclude(), "-I" ^ Paths.teemInclude()]
73 : jhr 3914 val cflags = condCons (TargetSpec.isParallel spec, #pthread Paths.cxxflags, cflags)
74 : jhr 3906 val cflags = if #debug spec
75 : jhr 3914 then #debug Paths.cxxflags :: cflags
76 :     else #ndebug Paths.cxxflags :: cflags
77 :     val cflags = #base Paths.cxxflags :: cflags
78 : jhr 3905 in
79 :     RunCC.compile (basename, cflags)
80 :     end
81 :    
82 : jhr 3906 fun ldFlags (spec : TargetSpec.t) = if #exec spec
83 : jhr 3905 then let
84 : jhr 3906 val extraLibs = condCons (TargetSpec.isParallel spec, #pthread Paths.extraLibs, [])
85 : jhr 3905 val extraLibs = Paths.teemLinkFlags() @ #base Paths.extraLibs :: extraLibs
86 : jhr 3906 val rtLib = TargetSpec.runtimeLibName spec
87 : jhr 3905 in
88 : jhr 3914 condCons (TargetSpec.isParallel spec, #pthread Paths.cxxflags, rtLib :: extraLibs)
89 : jhr 3905 end
90 : jhr 3906 else [TargetSpec.runtimeLibName spec]
91 : jhr 3905
92 : jhr 3912
93 :     (* generate source code that is common to both libraries and standalone executables *)
94 :     fun outputSrc (outS, spec, prog, strand, substitutions, genInputCode) = ()
95 :     (*
96 :     fun outputSrc (outS, spec, prog, strand, substitutions, genInputCode) = let
97 :     val IR.Program{globals, create, ...} = prog
98 :     (* does the program require barrier synchronization to implement BSP semantics? *)
99 :     val noBSP = TargetSpec.noBSP spec
100 :     val Strand{output, ...} = strand
101 :     val outputs = GenOutput.gen (props, !nAxes) output
102 :     fun ppDecl dcl = Out.decl outS
103 :     in
104 :     if (TargetSpec.isParallel spec)
105 :     then ppDecl (CL.verbatimDcl [CHeadParExtraFrag.text] substitutions)
106 :     else ();
107 :     if TargetSpec.dualState spec
108 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_DUAL_STATE\n"])
109 :     else ();
110 :     if not(#hasGlobals spec)
111 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_GLOBALS\n"])
112 :     else ();
113 :     if not(#hasInputs spec)
114 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_INPUTS\n"])
115 :     else ();
116 :     List.app ppDecl (genGlobalStruct (props, !globals));
117 :     ppDecl (genWorldStruct(prog, strand));
118 :     List.app ppDecl (genInputCode());
119 :     List.app ppDecl (!topDecls);
120 :     List.app ppDecl (genStrand strand);
121 :     List.app ppDecl outputs;
122 :     ppStrandTable (out, [strand]);
123 :     ppDecl (CL.verbatimDcl [InitFrag.text] substitutions);
124 :     ppDecl (CL.verbatimDcl [AllocFrag.text] substitutions);
125 :     ppDecl (!initially);
126 :     ppDecl (runFrag (noBSP, props, substitutions));
127 :     ppDecl (CL.verbatimDcl [ShutdownFrag.text] substitutions)
128 :     end
129 :     *)
130 :    
131 : jhr 3905 (********************
132 : jhr 3903 fun outputLibSrc (baseName, prog as Prog{props, inputs, strands, ...}) = let
133 :     val [strand] = AtomTable.listItems strands
134 :     val substitutions = mkSubs (props, strand)
135 :     (* output to C file *)
136 :     val out = openOut baseName
137 :     fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
138 :     in
139 :     ppDecl (CL.verbatimDcl [LibHdrFrag.text] substitutions);
140 :     List.app ppDecl (GenInputs.genDefinedInpStruct (props, !inputs));
141 :     outputSrc (out, prog, strand, substitutions,
142 :     fn () => GenInputs.genInputFuns(props, !inputs));
143 :     ppDecl (CL.verbatimDcl [CBodyFrag.text] substitutions);
144 :     closeOut out
145 :     end
146 :    
147 :     fun generateLib (prog as Prog{props, inputs, strands, ...}) = let
148 : jhr 3906 val {outDir, outBase, exec, double, debug, ...} = props
149 : jhr 3903 val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
150 :     val [Strand{state, output, ...}] = AtomTable.listItems strands
151 :     in
152 :     (* generate the library .h file *)
153 :     GenLibraryInterface.gen {
154 :     props = props,
155 : jhr 3906 rt = if TargetSpec.isParallel spec
156 : jhr 3903 then SOME LibInterfaceParFrag.text
157 :     else NONE,
158 :     inputs = !inputs,
159 :     outputs = output
160 :     };
161 :     (* *)
162 :     outputLibSrc (basename, prog);
163 :     (* compile and link *)
164 :     compile (props, basename);
165 :     RunCC.linkLib (basename, ldFlags props)
166 :     end
167 : jhr 3905 ********************)
168 : jhr 3903
169 : jhr 3902 fun exec (spec : TargetSpec.t, prog) = let
170 : jhr 3912 val IR.Program{inputs, strand, ...} = prog
171 : jhr 3908 val env = mkEnv spec
172 : jhr 3905 val baseName = OS.Path.joinDirFile{dir = #outDir spec, file = #outBase spec}
173 : jhr 3906 val substitutions = mkSubs (spec, strand)
174 : jhr 3903 (* output to C++ file *)
175 : jhr 3905 val outS = openCxxOut baseName
176 :     val ppDecl = Out.decl outS
177 :     val fragment = Out.fragment substitutions outS
178 : jhr 3902 in
179 : jhr 3912 GenExecUtil.execHead (outS, substitutions);
180 :     outputSrc (outS, spec, prog, strand, substitutions,
181 : jhr 3903 fn () => (
182 : jhr 3906 GenInputs.genInputsStruct (env, inputs) @
183 : jhr 3912 GenInputs.genExecInputFuns (env, prog) @
184 :     OutputUtil.genRegisterOutputOpts (env, OutputUtil.gatherOutputs prog)));
185 :     ppDecl (verbFrag (spec, Fragments.parallelMain, Fragments.sequentialMain, substitutions));
186 : jhr 3916 GenExecUtil.execFoot (outS, substitutions);
187 : jhr 3906 Out.closeOut outS;
188 :     compile (spec, baseName);
189 :     RunCC.linkExec (baseName, ldFlags spec)
190 : jhr 3902 end
191 : jhr 3908
192 : jhr 3902 fun library (spec : TargetSpec.t, prog) = let
193 :     val IR.Program{
194 :     props, target, consts, inputs, constInit,
195 :     globals, globalInit, strand, create, update
196 :     } = prog
197 : jhr 3906 val {outDir, outBase, exec, double, debug, ...} = spec
198 : jhr 3908 val env = mkEnv spec
199 : jhr 3902 in
200 :     raise Fail "FIXME"
201 :     end
202 : jhr 3908
203 : jhr 3902 end

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