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 3903 - (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 :    
20 : jhr 3903 (* generate source code that is common to both libraries and standalone executables *)
21 :     fun outputSrc (out, prog, strand, substitutions, genInputCode) = let
22 :     val Prog{props, globals, topDecls, nAxes, initially, ...} = prog
23 :     (* does the program require barrier synchronization to implement BSP semantics? *)
24 :     val noBSP = Properties.noBSP props
25 :     val Strand{output, ...} = strand
26 :     val outputs = GenOutput.gen (props, !nAxes) output
27 :     fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
28 :     in
29 :     if (#parallel props)
30 :     then ppDecl (CL.verbatimDcl [CHeadParExtraFrag.text] substitutions)
31 :     else ();
32 :     if Properties.dualState props
33 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_DUAL_STATE\n"])
34 :     else ();
35 :     if not(#hasGlobals props)
36 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_GLOBALS\n"])
37 :     else ();
38 :     if not(#hasInputs props)
39 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_NO_INPUTS\n"])
40 :     else ();
41 :     List.app ppDecl (genGlobalStruct (props, !globals));
42 :     ppDecl (genWorldStruct(prog, strand));
43 :     List.app ppDecl (genInputCode());
44 :     List.app ppDecl (!topDecls);
45 :     List.app ppDecl (genStrand strand);
46 :     List.app ppDecl outputs;
47 :     ppStrandTable (out, [strand]);
48 :     ppDecl (CL.verbatimDcl [InitFrag.text] substitutions);
49 :     ppDecl (CL.verbatimDcl [AllocFrag.text] substitutions);
50 :     ppDecl (!initially);
51 :     ppDecl (runFrag (noBSP, props, substitutions));
52 :     ppDecl (CL.verbatimDcl [ShutdownFrag.text] substitutions)
53 :     end
54 :    
55 :     fun outputLibSrc (baseName, prog as Prog{props, inputs, strands, ...}) = let
56 :     val [strand] = AtomTable.listItems strands
57 :     val substitutions = mkSubs (props, strand)
58 :     (* output to C file *)
59 :     val out = openOut baseName
60 :     fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
61 :     in
62 :     ppDecl (CL.verbatimDcl [LibHdrFrag.text] substitutions);
63 :     List.app ppDecl (GenInputs.genDefinedInpStruct (props, !inputs));
64 :     outputSrc (out, prog, strand, substitutions,
65 :     fn () => GenInputs.genInputFuns(props, !inputs));
66 :     ppDecl (CL.verbatimDcl [CBodyFrag.text] substitutions);
67 :     closeOut out
68 :     end
69 :    
70 :     fun generateLib (prog as Prog{props, inputs, strands, ...}) = let
71 :     val {outDir, outBase, exec, double, parallel, debug, ...} = props
72 :     val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
73 :     val [Strand{state, output, ...}] = AtomTable.listItems strands
74 :     in
75 :     (* generate the library .h file *)
76 :     GenLibraryInterface.gen {
77 :     props = props,
78 :     rt = if #parallel props
79 :     then SOME LibInterfaceParFrag.text
80 :     else NONE,
81 :     inputs = !inputs,
82 :     outputs = output
83 :     };
84 :     (* *)
85 :     outputLibSrc (basename, prog);
86 :     (* compile and link *)
87 :     compile (props, basename);
88 :     RunCC.linkLib (basename, ldFlags props)
89 :     end
90 :    
91 : jhr 3902 fun exec (spec : TargetSpec.t, prog) = let
92 :     val IR.Program{
93 :     props, target, consts, inputs, constInit,
94 :     globals, globalInit, strand, create, update
95 :     } = prog
96 : jhr 3903 val {outDir, outBase, exec, double, parallel, debug, ...} = spec
97 :     val baseName = OS.Path.joinDirFile{dir=outDir, file=outBase}
98 :     val substitutions = mkSubs (props, strand)
99 :     (* output to C++ file *)
100 :     val out = openOut baseName
101 :     fun ppDecl dcl = PrintAsC.output(#ppStrm out, dcl)
102 : jhr 3902 in
103 : jhr 3903 ppDecl (CL.verbatimDcl [ExecHdrFrag.text] substitutions);
104 :     ppDecl (CL.verbatimDcl [NrrdSaveHelperFrag.text] substitutions);
105 :     outputSrc (out, prog, strand, substitutions,
106 :     fn () => (
107 :     GenInputs.genInputsStruct (props, !inputs) @
108 :     GenInputs.genExecInputFuns (props, !inputs) @
109 :     OutputUtil.genRegisterOutputOpts output));
110 :     ppDecl (verbFrag (props, ParMainFrag.text, SeqMainFrag.text, substitutions));
111 :     closeOut out;
112 :     compile (props, basename);
113 :     RunCC.linkExec (basename, ldFlags props)
114 : jhr 3902 end
115 :    
116 :     fun library (spec : TargetSpec.t, prog) = let
117 :     val IR.Program{
118 :     props, target, consts, inputs, constInit,
119 :     globals, globalInit, strand, create, update
120 :     } = prog
121 : jhr 3903 val {outDir, outBase, exec, double, parallel, debug, ...} = spec
122 : jhr 3902 in
123 :     raise Fail "FIXME"
124 :     end
125 :    
126 :     end

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