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 3912 - (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 : jhr 3912 verbFrag (spec, Fragments.parallelRun, Fragments.sequentialRun, subs)
65 : jhr 3908 | runFrag (true, spec, subs) =
66 : jhr 3912 verbFrag (spec, Fragments.parallelRunNoBSP, Fragments.sequentialRunNoBSP, subs)
67 : jhr 3905
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 : jhr 3912
91 :     (* generate source code that is common to both libraries and standalone executables *)
92 :     fun outputSrc (outS, spec, prog, strand, substitutions, genInputCode) = ()
93 :     (*
94 :     fun outputSrc (outS, spec, prog, strand, substitutions, genInputCode) = let
95 :     val IR.Program{globals, create, ...} = prog
96 :     (* does the program require barrier synchronization to implement BSP semantics? *)
97 :     val noBSP = TargetSpec.noBSP spec
98 :     val Strand{output, ...} = strand
99 :     val outputs = GenOutput.gen (props, !nAxes) output
100 :     fun ppDecl dcl = Out.decl outS
101 :     in
102 :     if (TargetSpec.isParallel spec)
103 :     then ppDecl (CL.verbatimDcl [CHeadParExtraFrag.text] substitutions)
104 :     else ();
105 :     if TargetSpec.dualState spec
106 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_DUAL_STATE\n"])
107 :     else ();
108 :     if not(#hasGlobals spec)
109 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_GLOBALS\n"])
110 :     else ();
111 :     if not(#hasInputs spec)
112 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_INPUTS\n"])
113 :     else ();
114 :     List.app ppDecl (genGlobalStruct (props, !globals));
115 :     ppDecl (genWorldStruct(prog, strand));
116 :     List.app ppDecl (genInputCode());
117 :     List.app ppDecl (!topDecls);
118 :     List.app ppDecl (genStrand strand);
119 :     List.app ppDecl outputs;
120 :     ppStrandTable (out, [strand]);
121 :     ppDecl (CL.verbatimDcl [InitFrag.text] substitutions);
122 :     ppDecl (CL.verbatimDcl [AllocFrag.text] substitutions);
123 :     ppDecl (!initially);
124 :     ppDecl (runFrag (noBSP, props, substitutions));
125 :     ppDecl (CL.verbatimDcl [ShutdownFrag.text] substitutions)
126 :     end
127 :     *)
128 :    
129 : jhr 3905 (********************
130 : jhr 3903 fun outputLibSrc (baseName, prog as Prog{props, inputs, strands, ...}) = let
131 :     val [strand] = AtomTable.listItems strands
132 :     val substitutions = mkSubs (props, strand)
133 :     (* output to C file *)
134 :     val out = openOut baseName
135 :     fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
136 :     in
137 :     ppDecl (CL.verbatimDcl [LibHdrFrag.text] substitutions);
138 :     List.app ppDecl (GenInputs.genDefinedInpStruct (props, !inputs));
139 :     outputSrc (out, prog, strand, substitutions,
140 :     fn () => GenInputs.genInputFuns(props, !inputs));
141 :     ppDecl (CL.verbatimDcl [CBodyFrag.text] substitutions);
142 :     closeOut out
143 :     end
144 :    
145 :     fun generateLib (prog as Prog{props, inputs, strands, ...}) = let
146 : jhr 3906 val {outDir, outBase, exec, double, debug, ...} = props
147 : jhr 3903 val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
148 :     val [Strand{state, output, ...}] = AtomTable.listItems strands
149 :     in
150 :     (* generate the library .h file *)
151 :     GenLibraryInterface.gen {
152 :     props = props,
153 : jhr 3906 rt = if TargetSpec.isParallel spec
154 : jhr 3903 then SOME LibInterfaceParFrag.text
155 :     else NONE,
156 :     inputs = !inputs,
157 :     outputs = output
158 :     };
159 :     (* *)
160 :     outputLibSrc (basename, prog);
161 :     (* compile and link *)
162 :     compile (props, basename);
163 :     RunCC.linkLib (basename, ldFlags props)
164 :     end
165 : jhr 3905 ********************)
166 : jhr 3903
167 : jhr 3902 fun exec (spec : TargetSpec.t, prog) = let
168 : jhr 3912 val IR.Program{inputs, strand, ...} = prog
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 3912 GenExecUtil.execHead (outS, substitutions);
178 :     outputSrc (outS, spec, prog, strand, substitutions,
179 : jhr 3903 fn () => (
180 : jhr 3906 GenInputs.genInputsStruct (env, inputs) @
181 : jhr 3912 GenInputs.genExecInputFuns (env, prog) @
182 :     OutputUtil.genRegisterOutputOpts (env, OutputUtil.gatherOutputs prog)));
183 :     ppDecl (verbFrag (spec, Fragments.parallelMain, Fragments.sequentialMain, substitutions));
184 :     GenExecUtil.execFoot outS;
185 : jhr 3906 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