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 3905 - (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 3902
22 : jhr 3905 val openCxxOut = Out.openOut {ext = "cxx", ppDecl = PrintAsCxx.output}
23 :    
24 :     (* create the target-specific substitution list *)
25 :     fun mkSubs (spec, IR.Strand{name, ...}) = [
26 :     ("CFILE", OS.Path.joinBaseExt{base= #outBase spec, ext= SOME "c"}),
27 :     ("CXXFILE", OS.Path.joinBaseExt{base= #outBase spec, ext= SOME "cxx"}),
28 :     ("HDRFILE", OS.Path.joinBaseExt{base= #outBase spec, ext= SOME "h"}),
29 :     ("PREFIX", #namespace spec),
30 :     ("SRCFILE", #srcFile spec),
31 :     ("PROG_NAME", #outBase spec),
32 :     ("STRAND", Atom.toString name),
33 :     ("STRANDTY", Atom.toString name ^ "_strand"),
34 :     ("DIDEROTC_CMD", #diderotc spec),
35 :     ("DIDEROTC_ARGV", String.concatWith " " (#argv spec)),
36 :     ("DIDEROTC_VERSION", #version spec),
37 :     ("DIDEROT_FLOAT_PRECISION", TargetSpec.floatPrecisionDef spec),
38 :     ("DIDEROT_INT_PRECISION", TargetSpec.intPrecisionDef spec),
39 :     ("DIDEROT_TARGET", TargetSpec.targetDef spec)
40 :     ]
41 :    
42 :     fun condCons (true, x, xs) = x::xs
43 :     | condCons (false, _, xs) = xs
44 :    
45 :     fun verbFrag (spec : TargetSpec.t, parFrag, seqFrag, subs) =
46 :     CL.verbatimDcl [if (#parallel spec) then parFrag else seqFrag] subs
47 :    
48 :     fun runFrag (false, spec, subs) =
49 :     verbFrag (spec, ParRunFrag.text, SeqRunFrag.text, subs)
50 :     | runFrag (true, spec, subs) =
51 :     verbFrag (spec, ParRunNoBSPFrag.text, SeqRunNoBSPFrag.text, subs)
52 :    
53 :     fun compile (spec : TargetSpec.t, basename) = let
54 :     (* generate the C compiler flags *)
55 :     val cflags = ["-I" ^ Paths.diderotInclude(), "-I" ^ Paths.teemInclude()]
56 :     val cflags = condCons (#parallel spec, #pthread Paths.cflags, cflags)
57 :     val cflags = if #debug props
58 :     then #debug Paths.cflags :: cflags
59 :     else #ndebug Paths.cflags :: cflags
60 :     val cflags = #base Paths.cflags :: cflags
61 :     in
62 :     RunCC.compile (basename, cflags)
63 :     end
64 :    
65 :     fun ldFlags (spec : TargetSpec.t) = if #exec props
66 :     then let
67 :     val extraLibs = condCons (#parallel spec, #pthread Paths.extraLibs, [])
68 :     val extraLibs = Paths.teemLinkFlags() @ #base Paths.extraLibs :: extraLibs
69 :     val rtLib = Properties.runtimeName spec
70 :     in
71 :     condCons (#parallel spec, #pthread Paths.cflags, rtLib :: extraLibs)
72 :     end
73 :     else [Properties.runtimeName props]
74 :    
75 :     (********************
76 : jhr 3903 (* generate source code that is common to both libraries and standalone executables *)
77 :     fun outputSrc (out, prog, strand, substitutions, genInputCode) = let
78 :     val Prog{props, globals, topDecls, nAxes, initially, ...} = prog
79 :     (* does the program require barrier synchronization to implement BSP semantics? *)
80 :     val noBSP = Properties.noBSP props
81 :     val Strand{output, ...} = strand
82 :     val outputs = GenOutput.gen (props, !nAxes) output
83 :     fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
84 :     in
85 :     if (#parallel props)
86 :     then ppDecl (CL.verbatimDcl [CHeadParExtraFrag.text] substitutions)
87 :     else ();
88 :     if Properties.dualState props
89 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_DUAL_STATE\n"])
90 :     else ();
91 :     if not(#hasGlobals props)
92 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_GLOBALS\n"])
93 :     else ();
94 :     if not(#hasInputs props)
95 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_INPUTS\n"])
96 :     else ();
97 :     List.app ppDecl (genGlobalStruct (props, !globals));
98 :     ppDecl (genWorldStruct(prog, strand));
99 :     List.app ppDecl (genInputCode());
100 :     List.app ppDecl (!topDecls);
101 :     List.app ppDecl (genStrand strand);
102 :     List.app ppDecl outputs;
103 :     ppStrandTable (out, [strand]);
104 :     ppDecl (CL.verbatimDcl [InitFrag.text] substitutions);
105 :     ppDecl (CL.verbatimDcl [AllocFrag.text] substitutions);
106 :     ppDecl (!initially);
107 :     ppDecl (runFrag (noBSP, props, substitutions));
108 :     ppDecl (CL.verbatimDcl [ShutdownFrag.text] substitutions)
109 :     end
110 :    
111 :     fun outputLibSrc (baseName, prog as Prog{props, inputs, strands, ...}) = let
112 :     val [strand] = AtomTable.listItems strands
113 :     val substitutions = mkSubs (props, strand)
114 :     (* output to C file *)
115 :     val out = openOut baseName
116 :     fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
117 :     in
118 :     ppDecl (CL.verbatimDcl [LibHdrFrag.text] substitutions);
119 :     List.app ppDecl (GenInputs.genDefinedInpStruct (props, !inputs));
120 :     outputSrc (out, prog, strand, substitutions,
121 :     fn () => GenInputs.genInputFuns(props, !inputs));
122 :     ppDecl (CL.verbatimDcl [CBodyFrag.text] substitutions);
123 :     closeOut out
124 :     end
125 :    
126 :     fun generateLib (prog as Prog{props, inputs, strands, ...}) = let
127 :     val {outDir, outBase, exec, double, parallel, debug, ...} = props
128 :     val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
129 :     val [Strand{state, output, ...}] = AtomTable.listItems strands
130 :     in
131 :     (* generate the library .h file *)
132 :     GenLibraryInterface.gen {
133 :     props = props,
134 :     rt = if #parallel props
135 :     then SOME LibInterfaceParFrag.text
136 :     else NONE,
137 :     inputs = !inputs,
138 :     outputs = output
139 :     };
140 :     (* *)
141 :     outputLibSrc (basename, prog);
142 :     (* compile and link *)
143 :     compile (props, basename);
144 :     RunCC.linkLib (basename, ldFlags props)
145 :     end
146 : jhr 3905 ********************)
147 : jhr 3903
148 : jhr 3902 fun exec (spec : TargetSpec.t, prog) = let
149 :     val IR.Program{
150 :     props, target, consts, inputs, constInit,
151 :     globals, globalInit, strand, create, update
152 :     } = prog
153 : jhr 3903 val {outDir, outBase, exec, double, parallel, debug, ...} = spec
154 : jhr 3905 val env = Env.new spec
155 :     val baseName = OS.Path.joinDirFile{dir = #outDir spec, file = #outBase spec}
156 : jhr 3903 val substitutions = mkSubs (props, strand)
157 :     (* output to C++ file *)
158 : jhr 3905 val outS = openCxxOut baseName
159 :     val ppDecl = Out.decl outS
160 :     val fragment = Out.fragment substitutions outS
161 : jhr 3902 in
162 : jhr 3903 ppDecl (CL.verbatimDcl [ExecHdrFrag.text] substitutions);
163 :     ppDecl (CL.verbatimDcl [NrrdSaveHelperFrag.text] substitutions);
164 :     outputSrc (out, prog, strand, substitutions,
165 :     fn () => (
166 : jhr 3905 GenInputs.genInputsStruct (env, !inputs) @
167 :     GenInputs.genExecInputFuns (env, !inputs) @
168 : jhr 3903 OutputUtil.genRegisterOutputOpts output));
169 :     ppDecl (verbFrag (props, ParMainFrag.text, SeqMainFrag.text, substitutions));
170 :     closeOut out;
171 :     compile (props, basename);
172 :     RunCC.linkExec (basename, ldFlags props)
173 : jhr 3902 end
174 :    
175 :     fun library (spec : TargetSpec.t, prog) = let
176 :     val IR.Program{
177 :     props, target, consts, inputs, constInit,
178 :     globals, globalInit, strand, create, update
179 :     } = prog
180 : jhr 3903 val {outDir, outBase, exec, double, parallel, debug, ...} = spec
181 : jhr 3902 in
182 :     raise Fail "FIXME"
183 :     end
184 :    
185 :     end

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