1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
extern crate proc_macro;

use proc_macro::TokenStream;

use quote::{quote, ToTokens};
use syn::meta::parser;
use syn::Fields::{Named, Unnamed};
use syn::{parse_macro_input, parse_quote, parse_str, Field, ItemStruct, LitStr, Type, TypeTuple};

use cu29::config::read_configuration;
use cu29::config::CuConfig;
use cu29::curuntime::{compute_runtime_plan, CuExecutionStep, CuTaskType};
use format::{highlight_rust_code, rustfmt_generated_code};

mod format;
mod utils;

// TODO: this needs to be determined when the runtime is sizing itself.
const DEFAULT_CLNB: usize = 10;

#[inline]
fn int2index(i: u32) -> syn::Index {
    syn::Index::from(i as usize)
}

#[proc_macro]
pub fn gen_culist_payload(config_path_lit: TokenStream) -> TokenStream {
    let config = parse_macro_input!(config_path_lit as LitStr).value();
    println!("[gen culist payload with {:?}]", config);
    let cuconfig = read_config(&config);
    let runtime_plan: Vec<CuExecutionStep> =
        compute_runtime_plan(&cuconfig).expect("Could not compute runtime plan");
    let all_msgs_types_in_culist_order = extract_msg_types(&runtime_plan);
    build_culist_payload(&all_msgs_types_in_culist_order)
        .into_token_stream()
        .into()
}

/// Adds #[copper_runtime(config = "path")] to your application struct to generate the runtime.
/// This will add a "runtime" field to your struct and implement the "new" and "run" methods.
#[proc_macro_attribute]
pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream {
    println!("[entry]");
    let mut item_struct = parse_macro_input!(input as ItemStruct);

    let mut config_file: Option<LitStr> = None;
    let attribute_config_parser = parser(|meta| {
        if meta.path.is_ident("config") {
            config_file = Some(meta.value()?.parse()?);
            Ok(())
        } else {
            Err(meta.error("unsupported property"))
        }
    });

    println!("[parse]");
    parse_macro_input!(args with attribute_config_parser);
    let config_file = config_file
        .expect("Expected config file attribute like #[CopperRuntime(config = \"path\")]")
        .value();
    let copper_config = read_config(&config_file);

    println!("[runtime plan]");
    let runtime_plan: Vec<CuExecutionStep> =
        compute_runtime_plan(&copper_config).expect("Could not compute runtime plan");

    println!("[extract tasks types]");
    let (all_tasks_types_names, all_tasks_types) = extract_tasks_types(&copper_config);

    println!("[build task tuples]");
    // Build the tuple of all those types
    // note the extraneous , at the end is to make the tuple work even if this is only one element
    let task_types_tuple: TypeTuple = parse_quote! {
        (#(#all_tasks_types),*,)
    };

    println!("[build runtime field]");
    // add that to a new field
    let runtime_field: Field = parse_quote! {
        copper_runtime: _CuRuntime<CuTasks, CuPayload, #DEFAULT_CLNB>
    };

    let name = &item_struct.ident;

    println!("[match struct anonymity]");
    match &mut item_struct.fields {
        Named(fields_named) => {
            fields_named.named.push(runtime_field);
        }
        Unnamed(fields_unnamed) => {
            fields_unnamed.unnamed.push(runtime_field);
        }
        _ => (),
    };

    println!("[gen instances]");
    // Generate the code to create instances of the nodes
    // It maps the types to their index
    let task_instances_init_code: Vec<_> = all_tasks_types
        .iter()
        .enumerate()
        .map(|(index, ty)| {
            let ty_name = &all_tasks_types_names[index];
            let additional_error_info = format!(
                "Failed to get create instance for {}, instance index {}.",
                ty_name, index
            );
            quote! {
                #ty::new(all_instances_configs[#index]).map_err(|e| e.add_cause(#additional_error_info))?
            }
        })
        .collect();

    let start_calls: Vec<_> = all_tasks_types
        .iter()
        .enumerate()
        .map(|(index, _ty)| {
            let node_index = int2index(index as u32);
            quote! {
                {
                    let task_instance = &mut self.copper_runtime.task_instances.#node_index;
                    task_instance.start(&self.copper_runtime.clock)?;
                }
            }
        })
        .collect();

    let stop_calls: Vec<_> = all_tasks_types
        .iter()
        .enumerate()
        .map(|(index, _ty)| {
            let node_index = int2index(index as u32);
            quote! {
                {
                    let task_instance = &mut self.copper_runtime.task_instances.#node_index;
                    task_instance.stop(&self.copper_runtime.clock)?;
                }
            }
        })
        .collect();

    let preprocess_calls: Vec<_> = all_tasks_types
        .iter()
        .enumerate()
        .map(|(index, _ty)| {
            let node_index = int2index(index as u32);
            quote! {
                {
                    let task_instance = &mut self.copper_runtime.task_instances.#node_index;
                    task_instance.preprocess(&self.copper_runtime.clock)?;
                }
            }
        })
        .collect();

    let postprocess_calls: Vec<_> = all_tasks_types
        .iter()
        .enumerate()
        .map(|(index, _ty)| {
            let node_index = int2index(index as u32);
            quote! {
                {
                    let task_instance = &mut self.copper_runtime.task_instances.#node_index;
                    task_instance.postprocess(&self.copper_runtime.clock)?;
                }
            }
        })
        .collect();

    let runtime_plan_code: Vec<_> = runtime_plan
        .iter()
        .map(|step| {
            println!(
                "{} -> {} as {:?}. Input={:?}, Output={:?}",
                step.node.get_id(),
                step.node.get_type(),
                step.task_type,
                step.input_msg_type,
                step.output_msg_type
            );

            let node_index = int2index(step.node_id);
            let task_instance = quote! { self.copper_runtime.task_instances.#node_index };
            let comment_str = format!(
                "/// {} ({:?}) I:{:?} O:{:?}",
                step.node.get_id(),
                step.task_type,
                step.input_msg_type,
                step.output_msg_type
            );
            let comment_tokens: proc_macro2::TokenStream = parse_str(&comment_str).unwrap();


            let process_call = match step.task_type {
                CuTaskType::Source => {
                    let output_culist_index = int2index(
                        step.culist_output_index
                            .expect("Src task should have an output message index."),
                    );
                    quote! {
                        {
                            #comment_tokens
                            let cumsg_output = &mut payload.#output_culist_index;
                            cumsg_output.metadata.before_process = self.copper_runtime.clock.now().into();
                            #task_instance.process(&self.copper_runtime.clock, cumsg_output)?;
                            cumsg_output.metadata.after_process = self.copper_runtime.clock.now().into();
                        }
                    }
                }
                CuTaskType::Sink => {
                    let input_culist_index = int2index(
                        step.culist_input_index
                            .expect("Sink task should have an input message index."),
                    );
                    quote! {
                        {
                            #comment_tokens
                            let cumsg_input = &mut payload.#input_culist_index;
                            #task_instance.process(&self.copper_runtime.clock, cumsg_input)?;
                        }
                    }
                }
                CuTaskType::Regular => {
                    let input_culist_index = int2index(
                        step.culist_input_index
                            .expect("Sink task should have an input message index."),
                    );
                    let output_culist_index = int2index(
                        step.culist_output_index
                            .expect("Src task should have an output message index."),
                    );
                    quote! {
                    {
                        #comment_tokens
                        let cumsg_input = &mut payload.#input_culist_index;
                        let cumsg_output = &mut payload.#output_culist_index;
                        cumsg_output.metadata.before_process = self.copper_runtime.clock.now().into();
                        #task_instance.process(&self.copper_runtime.clock, cumsg_input, cumsg_output)?;
                        cumsg_output.metadata.after_process = self.copper_runtime.clock.now().into();
                    }
                    }
                }
            };

            process_call
        })
        .collect();

    println!("[extract msg types]");
    let all_msgs_types_in_culist_order: Vec<Type> = extract_msg_types(&runtime_plan);

    println!("[build the copper payload]");
    let msgs_types_tuple: TypeTuple = build_culist_payload(&all_msgs_types_in_culist_order);

    println!("[build the collect metadata function]");
    let culist_size = all_msgs_types_in_culist_order.len();
    let culist_indices = (0..(culist_size as u32)).map(int2index);
    let collect_metadata_function = quote! {
        pub fn collect_metadata<'a>(culist: &'a CuList) -> [&'a _CuMsgMetadata; #culist_size] {
            [#( &culist.payload.#culist_indices.metadata, )*]
        }
    };

    println!("[build the run method]");
    let run_method = quote! {

        pub fn start_all_tasks(&mut self) -> _CuResult<()> {
            #(#start_calls)*
            Ok(())
        }

        #[inline]
        pub fn run_one_iteration(&mut self) -> _CuResult<()> {
            #(#preprocess_calls)*
            {
                let mut culist = &mut self.copper_runtime.copper_lists_manager.create().expect("Ran out of space for copper lists"); // FIXME: error handling.
                let id = culist.id;
                culist.change_state(cu29::copperlist::CopperListState::Processing);
                {
                    let payload = &mut culist.payload;
                    #(#runtime_plan_code)*
                } // drop(payload);

                {
                    let md = collect_metadata(&culist);
                    let e2e = md.last().unwrap().after_process.unwrap() - md.first().unwrap().before_process.unwrap();
                    let e2en: u64 = e2e.into();
                    debug!("End to end latency {}, mean latency per node: {}", e2e, e2en / (md.len() as u64));
                } // drop(md);

                self.copper_runtime.end_of_processing(id);

           }// drop(culist); avoids a double mutable borrow
           #(#postprocess_calls)*
           Ok(())
        }

        pub fn stop_all_tasks(&mut self) -> _CuResult<()> {
            #(#stop_calls)*
            Ok(())
        }

        pub fn run(&mut self) -> _CuResult<()> {
            self.start_all_tasks()?;
            let error = loop {
                let error = self.run_one_iteration();
                if error.is_err() {
                    break error;
                }
            };
            debug!("A task errored out: {}", &error);
            self.stop_all_tasks()?;
            error
        }
    };

    println!("[build result]");
    // Convert the modified struct back into a TokenStream
    let result = quote! {
        // import everything with an _ to avoid clashes with the user's code
        use cu29::config::CuConfig as _CuConfig;
        use cu29::config::NodeInstanceConfig as _NodeInstanceConfig;
        use cu29::config::read_configuration as _read_configuration;
        use cu29::curuntime::CuRuntime as _CuRuntime;
        use cu29::CuResult as _CuResult;
        use cu29::CuError as _CuError;
        use cu29::cutask::CuTaskLifecycle as _CuTaskLifecycle; // Needed for the instantiation of tasks
        use cu29::cutask::CuSrcTask as _CuSrcTask;
        use cu29::cutask::CuSinkTask as _CuSinkTask;
        use cu29::cutask::CuTask as _CuTask;
        use cu29::cutask::CuMsg as _CuMsg;
        use cu29::cutask::CuMsgMetadata as _CuMsgMetadata;
        use cu29::copperlist::CopperList as _CopperList;
        use cu29::clock::RobotClock as _RobotClock;
        use cu29::clock::OptionCuTime as _OptionCuTime;
        use cu29::clock::ClockProvider as _ClockProvider;
        use std::sync::Arc as _Arc;
        use std::sync::Mutex as _Mutex;
        use cu29_unifiedlog::stream_write as _stream_write;
        use cu29_unifiedlog::UnifiedLoggerWrite as _UnifiedLoggerWrite;
        use cu29_traits::UnifiedLogType as _UnifiedLogType;

        // This is the heart of everything.
        // CuTasks is the list of all the tasks types.
        // CuList is a CopperList with the list of all the messages types as payload.
        pub type CuTasks = #task_types_tuple;
        pub type CuPayload = #msgs_types_tuple;
        pub type CuList = _CopperList<CuPayload>;

        // This generates a way to get the metadata of every single message of a culist at low cost
        #collect_metadata_function

        fn tasks_instanciator(all_instances_configs: Vec<Option<&_NodeInstanceConfig>>) -> _CuResult<CuTasks> {
            Ok(( #(#task_instances_init_code),*, ))
        }

        pub #item_struct

        impl #name {

            pub fn new(clock:_RobotClock, unified_logger: _Arc<_Mutex<_UnifiedLoggerWrite>>) -> _CuResult<Self> {
                let config = _read_configuration(#config_file)?;

                let copperlist_stream = _stream_write::<CuList>(
                    unified_logger.clone(),
                    _UnifiedLogType::CopperList,
                    60 * 1024, // FIXME: make this a config
                );

                Ok(#name {
                    copper_runtime: _CuRuntime::<CuTasks, CuPayload, #DEFAULT_CLNB>::new(clock, &config, tasks_instanciator, copperlist_stream)?
                })
            }

            #run_method
        }
    };
    let tokens: TokenStream = result.into();

    // Print and format the generated code using rustfmt
    // println!("Generated tokens: {}", tokens);
    let formatted_code = rustfmt_generated_code(tokens.to_string());
    println!("\n     ===    Gen. Runtime ===\n");
    println!("{}", highlight_rust_code(formatted_code));
    println!("\n     === === === === === ===\n");

    tokens
}

fn read_config(config_file: &String) -> CuConfig {
    let mut config_full_path = utils::caller_crate_root();
    config_full_path.push(&config_file);
    let filename = config_full_path
        .as_os_str()
        .to_str()
        .expect("Could not interpret the config file name");
    let copper_config = read_configuration(filename).expect(&format!(
        "Failed to read configuration file: {:?}",
        &config_full_path
    ));
    copper_config
}

/// Extract all the tasks types in their index order
fn extract_tasks_types(copper_config: &CuConfig) -> (Vec<String>, Vec<Type>) {
    let all_nodes = copper_config.get_all_nodes();

    // Collect all the type names used by our configs.
    let all_types_names: Vec<String> = all_nodes
        .iter()
        .map(|node_config| node_config.get_type().to_string())
        .collect();

    // Transform them as Rust types
    let all_types: Vec<Type> = all_types_names
        .iter()
        .map(|name| {
            parse_str(name)
                .expect(format!("Could not transform {} into a Task Rust type.", name).as_str())
        })
        .collect();
    (all_types_names, all_types)
}

fn extract_msg_types(runtime_plan: &Vec<CuExecutionStep>) -> Vec<Type> {
    runtime_plan
        .iter()
        .filter_map(|step| {
            if step.output_msg_type.is_none() {
                None
            } else {
                Some(parse_str::<Type>(step.output_msg_type.clone().unwrap().as_str()).unwrap())
            }
        })
        .collect()
}

fn build_culist_payload(all_msgs_types_in_culist_order: &Vec<Type>) -> TypeTuple {
    if all_msgs_types_in_culist_order.is_empty() {
        parse_quote! {()}
    } else {
        parse_quote! { (#(_CuMsg<#all_msgs_types_in_culist_order>),*,)}
    }
}