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 3974 - (view) (download)

1 : jhr 3864 (* gen-strand.sml
2 :     *
3 : jhr 3974 * Code generation for strands.
4 : jhr 3864 *
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 : jhr 3974 * NOTE: the calling convention generated here must match that used in GenWorld.genInitiallyFun!
38 : jhr 3866 *)
39 : jhr 3952 fun genStateInit (env, strandName, strandTy, params, def) = let
40 :     val IR.Method{usesGlobals, needsWorld, body} = def
41 : jhr 3866 val fName = strandName ^ "_init"
42 : jhr 3927 val (env, params) = List.foldr
43 :     (fn (x, (env, ps)) => let
44 :     val (env, p) = TreeToCxx.trParam(env, x)
45 :     in
46 :     (env, p::ps)
47 :     end)
48 :     (env, []) params
49 : jhr 3908 val selfParam = if TargetSpec.dualState(Env.target env)
50 :     then RN.selfOutVar
51 :     else RN.selfVar
52 : jhr 3866 val params =
53 : jhr 3917 CL.PARAM([], strandTy, selfParam) :: params
54 : jhr 3952 val params = if usesGlobals
55 : jhr 3908 then RN.globalsParam :: params
56 : jhr 3866 else params
57 : jhr 3952 val params = if needsWorld
58 :     then RN.worldParam :: params
59 :     else params
60 : jhr 3908 val body = TreeToCxx.trBlock (env, body)
61 : jhr 3866 in
62 :     CL.D_Func(["static"], CL.voidTy, fName, params, body)
63 :     end
64 :    
65 : jhr 3864 (* generate a function definition for a strand method *)
66 : jhr 3952 fun genMethodDef (env, strandName, strandTy, methTy, methName, usesGlobals, needsWorld, body) = let
67 : jhr 3864 val fName = concat[strandName, "_", methName]
68 : jhr 3955 val params = if TargetSpec.dualState(Env.target env)
69 : jhr 3864 then [
70 : jhr 3908 CL.PARAM(["const"], strandTy, RN.selfVar),
71 :     CL.PARAM([], strandTy, RN.selfOutVar)
72 : jhr 3864 ]
73 : jhr 3908 else [CL.PARAM([], strandTy, RN.selfVar)]
74 : jhr 3952 val params = if usesGlobals
75 : jhr 3955 then RN.globalsParam :: params
76 :     else params
77 : jhr 3952 val params = if needsWorld
78 : jhr 3955 then RN.worldParam :: params
79 :     else params
80 : jhr 3864 in
81 : jhr 3917 CL.D_Func(["static"], methTy, fName, params, body)
82 : jhr 3864 end
83 :    
84 : jhr 3924 (* generate a function definition for a strand method *)
85 : jhr 3952 fun genMethod (env, strandName, strandTy, methTy, methName, IR.Method{usesGlobals, needsWorld, body}) =
86 :     genMethodDef (
87 :     env, strandName, strandTy, methTy, methName,
88 :     usesGlobals, needsWorld, TreeToCxx.trBlock (env, body))
89 : jhr 3924
90 : jhr 3908 fun genInitiallyMethod (_, _, _, NONE) = []
91 : jhr 3952 | genInitiallyMethod (env, strandName, strandTy, SOME meth) =
92 :     [genMethod (env, strandName, strandTy, strandStatusTy, "initially", meth)]
93 : jhr 3864
94 : jhr 3952 fun genUpdateMethod (env, strandName, strandTy, meth) =
95 :     genMethod (env, strandName, strandTy, strandStatusTy, "update", meth)
96 : jhr 3864
97 : jhr 3924 fun genStabilizeMethod (env, strandName, strandTy, NONE) =
98 :     if TargetSpec.dualState(Env.target env)
99 :     then raise Fail "FIXME: default stabilize for dual state"
100 : jhr 3952 else [
101 :     genMethodDef (
102 :     env, strandName, strandTy, CL.voidTy, "stabilize", false, false, CL.mkBlock[])
103 :     ]
104 :     | genStabilizeMethod (env, strandName, strandTy, SOME meth) =
105 :     [genMethod (env, strandName, strandTy, CL.voidTy, "stabilize", meth)]
106 : jhr 3864
107 : jhr 3908 fun gen (env, strand) = let
108 : jhr 3864 val IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
109 : jhr 3907 val name = Atom.toString name
110 : jhr 3908 val strandTy = CL.T_Ptr(CL.T_Named(name ^ "_strand"))
111 : jhr 3918 in {
112 :     structDef = genStrandStruct (env, name, state),
113 :     methods = genStateInit (env, name, strandTy, params, stateInit) ::
114 :     genInitiallyMethod (env, name, strandTy, initM) @
115 :     genUpdateMethod (env, name, strandTy, updateM) ::
116 :     genStabilizeMethod (env, name, strandTy, stabilizeM)
117 :     } end
118 : jhr 3864
119 :     end

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