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

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