@@ -28,6 +28,12 @@ public IEnumerable<CompiledFile> GenerateCode(ICompilerConfiguration job, Scope
28
28
/// This compiler has a compilation stage.
29
29
/// </summary>
30
30
public bool HasCompilationStage => true ;
31
+ private static List < Variable > _globalParams = [ ] ;
32
+
33
+ private string GetGlobalParamAndLocalVariableName ( Variable v )
34
+ {
35
+ return _globalParams . Contains ( v ) ? $ "({ ICodeGenerator . GlobalConfigName } .{ v . Name } )" : CompilationContext . GetVar ( v . Name ) ;
36
+ }
31
37
32
38
public void Compile ( ICompilerConfiguration job )
33
39
{
@@ -103,6 +109,10 @@ private CompiledFile GenerateSource(CompilationContext context, Scope globalScop
103
109
context . WriteLine ( source . Stream ) ;
104
110
105
111
context . WriteLine ( source . Stream ) ;
112
+
113
+ _globalParams = globalScope . GetGlobalVariables ( ) ;
114
+
115
+ DeclareGlobalParams ( context , source . Stream ) ;
106
116
107
117
//WriteMachineTagDefs(context, source.Stream, globalScope.Machines);
108
118
@@ -118,7 +128,7 @@ private CompiledFile GenerateSource(CompilationContext context, Scope globalScop
118
128
WriteDecl ( context , source . Stream , decl ) ;
119
129
}
120
130
121
- if ( ! hasSafetyTest ) WriteMainDriver ( context , source . Stream , globalScope , decls ) ;
131
+ if ( ! hasSafetyTest ) WriteImplementationDecl ( context , source . Stream , globalScope , decls ) ;
122
132
123
133
context . WriteLine ( source . Stream , "PTestDriver testDriver = null;" ) ;
124
134
context . WriteLine ( source . Stream , "@Generated" ) ;
@@ -131,20 +141,36 @@ private CompiledFile GenerateSource(CompilationContext context, Scope globalScop
131
141
132
142
return source ;
133
143
}
144
+
145
+ private const string InitGlobalParamsFunctionName = "InitializeGlobalParams" ;
146
+
147
+ private void WriteInitializeGlobalParams ( CompilationContext context , StringWriter output , IDictionary < Variable , IPExpr > dic )
148
+ {
149
+ context . WriteLine ( output , $ "public static void { InitGlobalParamsFunctionName } () {{") ;
150
+ foreach ( var ( v , value ) in dic )
151
+ {
152
+ var varName = GetGlobalParamAndLocalVariableName ( v ) ;
153
+ context . Write ( output , $ " { varName } = ") ;
154
+ WriteExpr ( context , output , value ) ;
155
+ context . WriteLine ( output , $ ";") ;
156
+ }
157
+ context . WriteLine ( output , "}" ) ;
158
+ }
134
159
135
- private void WriteDriver ( CompilationContext context , StringWriter output , string startMachine ,
136
- IEnumerable < IPDecl > decls , IDictionary < Interface , Machine > interfaceDef = null )
160
+ private void WriteTestFunction ( CompilationContext context , StringWriter output , string startMachine ,
161
+ IEnumerable < IPDecl > decls , bool ifInitGlobalParams , IDictionary < Interface , Machine > interfaceDef = null )
137
162
{
138
- WriteDriverConfigure ( context , output , startMachine , decls , interfaceDef ) ;
163
+ WriteTestFunctionConfigure ( context , output , startMachine , decls , interfaceDef , ifInitGlobalParams ) ;
139
164
context . WriteLine ( output ) ;
140
165
}
141
166
142
- private void WriteDriverConfigure ( CompilationContext context , StringWriter output , string startMachine ,
143
- IEnumerable < IPDecl > decls , IDictionary < Interface , Machine > interfaceDef )
167
+ private void WriteTestFunctionConfigure ( CompilationContext context , StringWriter output , string startMachine ,
168
+ IEnumerable < IPDecl > decls , IDictionary < Interface , Machine > interfaceDef , bool ifInitGlobalParams )
144
169
{
145
170
context . WriteLine ( output ) ;
146
171
context . WriteLine ( output , "@Generated" ) ;
147
172
context . WriteLine ( output , "public void configure() {" ) ;
173
+ if ( ifInitGlobalParams ) { context . WriteLine ( output , $ " { InitGlobalParamsFunctionName } ();") ; }
148
174
149
175
context . WriteLine ( output , $ " mainMachine = { startMachine } .class;") ;
150
176
@@ -182,7 +208,7 @@ private void WriteDriverConfigure(CompilationContext context, StringWriter outpu
182
208
context . WriteLine ( output , "}" ) ;
183
209
}
184
210
185
- private void WriteMainDriver ( CompilationContext context , StringWriter output , Scope globalScope ,
211
+ private void WriteImplementationDecl ( CompilationContext context , StringWriter output , Scope globalScope ,
186
212
IEnumerable < IPDecl > decls )
187
213
{
188
214
Machine mainMachine = null ;
@@ -204,7 +230,7 @@ private void WriteMainDriver(CompilationContext context, StringWriter output, Sc
204
230
205
231
context . WriteLine ( output , "@Generated" ) ;
206
232
context . WriteLine ( output , "public static class test_DefaultImpl extends PTestDriver {" ) ;
207
- WriteDriver ( context , output , mainMachine . Name , decls ) ;
233
+ WriteTestFunction ( context , output , mainMachine . Name , decls , false ) ;
208
234
context . WriteLine ( output , "}" ) ;
209
235
context . WriteLine ( output ) ;
210
236
}
@@ -235,19 +261,44 @@ private void WriteDecl(CompilationContext context, StringWriter output, IPDecl d
235
261
WriteEvent ( context , output , ev ) ;
236
262
break ;
237
263
case SafetyTest safety :
238
- WriteSafetyTestDecl ( context , output , safety ) ;
264
+ ParamAssignment . IterateAssignments ( safety , _globalParams ,
265
+ assignment => WriteSafetyTestDecl ( context , output , safety , assignment ) ) ;
266
+ break ;
267
+ case Variable _:
239
268
break ;
240
269
default :
241
270
context . WriteLine ( output , $ "// Skipping { decl . GetType ( ) . Name } '{ decl . Name } '\n ") ;
242
271
break ;
243
272
}
244
273
}
245
-
246
- private void WriteSafetyTestDecl ( CompilationContext context , StringWriter output , SafetyTest safety )
274
+
275
+ private void DeclareGlobalParams ( CompilationContext context , StringWriter output )
276
+ {
277
+ context . WriteLine ( output , "@Generated" ) ;
278
+ context . WriteLine ( output , $ "public static class { ICodeGenerator . GlobalConfigName } ") ;
279
+ context . WriteLine ( output , "{" ) ;
280
+ foreach ( var v in _globalParams )
281
+ {
282
+ if ( v . Role != VariableRole . GlobalParams )
283
+ {
284
+ throw context . Handler . InternalError ( v . SourceLocation , new ArgumentException ( "The role of global variable is not global." ) ) ;
285
+ }
286
+ context . Write ( output ,
287
+ $ " public static { GetPExType ( v . Type ) } { v . Name } = ") ;
288
+ context . Write ( output , $ "{ GetDefaultValue ( v . Type ) } ") ;
289
+ context . WriteLine ( output , $ ";") ;
290
+ }
291
+ context . WriteLine ( output , "}" ) ;
292
+ context . WriteLine ( output ) ;
293
+ }
294
+
295
+ private void WriteSafetyTestDecl ( CompilationContext context , StringWriter output , SafetyTest safety , Dictionary < Variable , IPExpr > assignment )
247
296
{
248
297
context . WriteLine ( output , "@Generated" ) ;
249
- context . WriteLine ( output , $ "public static class { context . GetNameForDecl ( safety ) } extends PTestDriver {{") ;
250
- WriteDriver ( context , output , safety . Main , safety . ModExpr . ModuleInfo . MonitorMap . Keys ,
298
+ var name = ParamAssignment . RenameSafetyTestByAssignment ( context . GetNameForDecl ( safety ) , assignment ) ;
299
+ context . WriteLine ( output , $ "public static class { name } extends PTestDriver {{") ;
300
+ WriteInitializeGlobalParams ( context , output , assignment ) ;
301
+ WriteTestFunction ( context , output , safety . Main , safety . ModExpr . ModuleInfo . MonitorMap . Keys , true ,
251
302
safety . ModExpr . ModuleInfo . InterfaceDef ) ;
252
303
context . WriteLine ( output , "}" ) ;
253
304
context . WriteLine ( output ) ;
@@ -1416,8 +1467,7 @@ private void WriteWithLValueMutationContext(
1416
1467
) ;
1417
1468
break ;
1418
1469
case VariableAccessExpr variableAccessExpr :
1419
- var name = variableAccessExpr . Variable . Name ;
1420
- var unguarded = CompilationContext . GetVar ( name ) ;
1470
+ var unguarded = GetGlobalParamAndLocalVariableName ( variableAccessExpr . Variable ) ;
1421
1471
var guardedTemp = context . FreshTempVar ( ) ;
1422
1472
1423
1473
context . Write ( output , $ "{ GetPExType ( variableAccessExpr . Type ) } { guardedTemp } ") ;
@@ -1685,7 +1735,7 @@ private void WriteExpr(CompilationContext context, StringWriter output, IPExpr e
1685
1735
break ;
1686
1736
}
1687
1737
case VariableAccessExpr variableAccessExpr :
1688
- context . Write ( output , $ "{ CompilationContext . GetVar ( variableAccessExpr . Variable . Name ) } ") ;
1738
+ context . Write ( output , $ "{ GetGlobalParamAndLocalVariableName ( variableAccessExpr . Variable ) } ") ;
1689
1739
break ;
1690
1740
case FunCallExpr _:
1691
1741
throw new InvalidOperationException (
0 commit comments