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 3918 - (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 :     structure CL = CLang
22 : jhr 3908 structure RN = CxxNames
23 :     structure Env = CodeGenEnv
24 : jhr 3864
25 : jhr 3917 val strandStatusTy = CL.T_Named "diderot::strand_status"
26 :    
27 :     (* generate the strand-state struct declaration *)
28 :     fun genStrandStruct (env, strandName, state) = let
29 :     fun mkField sv = (TreeToCxx.trType(env, TreeStateVar.ty sv), TreeStateVar.name sv)
30 :     in
31 :     CL.D_StructDef(SOME(strandName ^ "_strand"), List.map mkField state, NONE)
32 :     end
33 :    
34 : jhr 3866 (* generate the strand-state initialization code. The variables are the strand
35 :     * parameters.
36 :     *)
37 : jhr 3908 fun genStateInit (env, strandName, strandTy, params, body) = let
38 : jhr 3866 val fName = strandName ^ "_init"
39 : jhr 3908 val selfParam = if TargetSpec.dualState(Env.target env)
40 :     then RN.selfOutVar
41 :     else RN.selfVar
42 : jhr 3866 val params =
43 : jhr 3917 CL.PARAM([], strandTy, selfParam) :: params
44 : jhr 3908 val params = if #hasGlobals(Env.target env)
45 :     then RN.globalsParam :: params
46 : jhr 3866 else params
47 : jhr 3908 val body = TreeToCxx.trBlock (env, body)
48 : jhr 3866 in
49 :     CL.D_Func(["static"], CL.voidTy, fName, params, body)
50 :     end
51 :    
52 : jhr 3864 (* generate a function definition for a strand method *)
53 : jhr 3908 fun genMethod (env, strandName, strandTy, methTy, methName, body) = let
54 : jhr 3864 val fName = concat[strandName, "_", methName]
55 : jhr 3908 val stateParams = if TargetSpec.dualState(Env.target env)
56 : jhr 3864 then [
57 : jhr 3908 CL.PARAM(["const"], strandTy, RN.selfVar),
58 :     CL.PARAM([], strandTy, RN.selfOutVar)
59 : jhr 3864 ]
60 : jhr 3908 else [CL.PARAM([], strandTy, RN.selfVar)]
61 :     val params = if #hasGlobals(Env.target env)
62 :     then RN.globalsParam :: stateParams
63 : jhr 3864 else stateParams
64 : jhr 3908 val body = TreeToCxx.trBlock (env, body)
65 : jhr 3864 in
66 : jhr 3917 CL.D_Func(["static"], methTy, fName, params, body)
67 : jhr 3864 end
68 :    
69 : jhr 3908 fun genInitiallyMethod (_, _, _, NONE) = []
70 :     | genInitiallyMethod (spec, strandName, strandTy, SOME body) =
71 : jhr 3917 [genMethod (spec, strandName, strandTy, strandStatusTy, "initially", body)]
72 : jhr 3864
73 : jhr 3908 fun genUpdateMethod (spec, strandName, strandTy, body) =
74 : jhr 3917 genMethod (spec, strandName, strandTy, strandStatusTy, "update", body)
75 : jhr 3864
76 : jhr 3908 fun genStabilizeMethod (_, _, _, NONE) = []
77 :     | genStabilizeMethod (spec, strandName, strandTy, SOME body) =
78 :     [genMethod (spec, strandName, strandTy, CL.voidTy, "stabilize", body)]
79 : jhr 3864
80 : jhr 3908 fun gen (env, strand) = let
81 : jhr 3864 val IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
82 : jhr 3907 val name = Atom.toString name
83 : jhr 3908 val strandTy = CL.T_Ptr(CL.T_Named(name ^ "_strand"))
84 : jhr 3917 val params = List.map (TreeToCxx.trParam env) params
85 : jhr 3918 in {
86 :     structDef = genStrandStruct (env, name, state),
87 :     methods = genStateInit (env, name, strandTy, params, stateInit) ::
88 :     genInitiallyMethod (env, name, strandTy, initM) @
89 :     genUpdateMethod (env, name, strandTy, updateM) ::
90 :     genStabilizeMethod (env, name, strandTy, stabilizeM)
91 :     } end
92 : jhr 3864
93 :     end

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