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-strand.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/target-cpu/gen-strand.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3924 - (view) (download)

1 : jhr 3864 (* gen-strand.sml
2 :     *
3 :     * Code generation for strands
4 :     *
5 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
6 :     *
7 :     * COPYRIGHT (c) 2016 The University of Chicago
8 :     * All rights reserved.
9 :     *)
10 :    
11 :     structure GenStrand : sig
12 :    
13 : jhr 3918 val gen : CodeGenEnv.t * TreeIR.strand -> {
14 :     structDef : CLang.decl,
15 :     methods : CLang.decl list
16 :     }
17 : jhr 3864
18 :     end = struct
19 :    
20 :     structure IR = TreeIR
21 : jhr 3924 structure SV = TreeStateVar
22 : jhr 3864 structure CL = CLang
23 : jhr 3908 structure RN = CxxNames
24 :     structure Env = CodeGenEnv
25 : jhr 3864
26 : jhr 3917 val strandStatusTy = CL.T_Named "diderot::strand_status"
27 :    
28 :     (* generate the strand-state struct declaration *)
29 :     fun genStrandStruct (env, strandName, state) = let
30 : jhr 3924 fun mkField sv = (TreeToCxx.trType(env, SV.ty sv), SV.qname sv)
31 : jhr 3917 in
32 :     CL.D_StructDef(SOME(strandName ^ "_strand"), List.map mkField state, NONE)
33 :     end
34 :    
35 : jhr 3866 (* generate the strand-state initialization code. The variables are the strand
36 :     * parameters.
37 :     *)
38 : jhr 3908 fun genStateInit (env, strandName, strandTy, params, body) = let
39 : jhr 3866 val fName = strandName ^ "_init"
40 : jhr 3908 val selfParam = if TargetSpec.dualState(Env.target env)
41 :     then RN.selfOutVar
42 :     else RN.selfVar
43 : jhr 3866 val params =
44 : jhr 3917 CL.PARAM([], strandTy, selfParam) :: params
45 : jhr 3908 val params = if #hasGlobals(Env.target env)
46 :     then RN.globalsParam :: params
47 : jhr 3866 else params
48 : jhr 3908 val body = TreeToCxx.trBlock (env, body)
49 : jhr 3866 in
50 :     CL.D_Func(["static"], CL.voidTy, fName, params, body)
51 :     end
52 :    
53 : jhr 3864 (* generate a function definition for a strand method *)
54 : jhr 3924 fun genMethodDef (env, strandName, strandTy, methTy, methName, body) = let
55 : jhr 3864 val fName = concat[strandName, "_", methName]
56 : jhr 3908 val stateParams = if TargetSpec.dualState(Env.target env)
57 : jhr 3864 then [
58 : jhr 3908 CL.PARAM(["const"], strandTy, RN.selfVar),
59 :     CL.PARAM([], strandTy, RN.selfOutVar)
60 : jhr 3864 ]
61 : jhr 3908 else [CL.PARAM([], strandTy, RN.selfVar)]
62 :     val params = if #hasGlobals(Env.target env)
63 :     then RN.globalsParam :: stateParams
64 : jhr 3864 else stateParams
65 :     in
66 : jhr 3917 CL.D_Func(["static"], methTy, fName, params, body)
67 : jhr 3864 end
68 :    
69 : jhr 3924 (* generate a function definition for a strand method *)
70 :     fun genMethod (env, strandName, strandTy, methTy, methName, body) =
71 :     genMethodDef (env, strandName, strandTy, methTy, methName, TreeToCxx.trBlock (env, body))
72 :    
73 : jhr 3908 fun genInitiallyMethod (_, _, _, NONE) = []
74 : jhr 3924 | genInitiallyMethod (env, strandName, strandTy, SOME body) =
75 :     [genMethod (env, strandName, strandTy, strandStatusTy, "initially", body)]
76 : jhr 3864
77 : jhr 3924 fun genUpdateMethod (env, strandName, strandTy, body) =
78 :     genMethod (env, strandName, strandTy, strandStatusTy, "update", body)
79 : jhr 3864
80 : jhr 3924 fun genStabilizeMethod (env, strandName, strandTy, NONE) =
81 :     if TargetSpec.dualState(Env.target env)
82 :     then raise Fail "FIXME: default stabilize for dual state"
83 :     else [genMethodDef (env, strandName, strandTy, CL.voidTy, "stabilize", CL.mkBlock[])]
84 :     | genStabilizeMethod (env, strandName, strandTy, SOME body) =
85 :     [genMethod (env, strandName, strandTy, CL.voidTy, "stabilize", body)]
86 : jhr 3864
87 : jhr 3908 fun gen (env, strand) = let
88 : jhr 3864 val IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
89 : jhr 3907 val name = Atom.toString name
90 : jhr 3908 val strandTy = CL.T_Ptr(CL.T_Named(name ^ "_strand"))
91 : jhr 3917 val params = List.map (TreeToCxx.trParam env) params
92 : jhr 3918 in {
93 :     structDef = genStrandStruct (env, name, state),
94 :     methods = genStateInit (env, name, strandTy, params, stateInit) ::
95 :     genInitiallyMethod (env, name, strandTy, initM) @
96 :     genUpdateMethod (env, name, strandTy, updateM) ::
97 :     genStabilizeMethod (env, name, strandTy, stabilizeM)
98 :     } end
99 : jhr 3864
100 :     end

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