You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
KernelScript functions support both traditional unnamed return types and modern named return values. The complete grammar is defined in Section 15 (Complete Formal Grammar).
2368
2362
2369
-
visibility = "pub" | "priv"
2370
-
parameter_list = [ parameter { "," parameter } ]
2371
-
parameter = identifier ":" type_annotation
2372
-
return_type = type_annotation
2373
-
```
2363
+
Key function types:
2364
+
-**eBPF program functions**: Attributed with `@xdp`, `@tc`, etc. - compile to eBPF bytecode
2365
+
-**Helper functions**: Attributed with `@helper` - shared across all eBPF programs
2366
+
-**Userspace functions**: No attributes - compile to native executable
Named return values automatically declare a local variable with the specified name and type. This variable can be used throughout the function, and naked returns (`return` without a value) will return the current value of the named variable.
2391
+
2392
+
#### 7.3.1 Named Return Syntax Examples
2393
+
2394
+
```kernelscript
2395
+
// Backward compatible unnamed return (unchanged)
2396
+
fn add_numbers(a: i32, b: i32) -> i32 {
2397
+
return a + b
2398
+
}
2399
+
2400
+
// Named return value - 'sum' becomes a local variable
type PacketProcessor = fn(*xdp_md) -> result: xdp_action
2480
+
```
2481
+
2482
+
#### 7.3.3 Code Generation
2483
+
2484
+
Named return values compile to clean, efficient C code with zero runtime overhead:
2485
+
2486
+
**KernelScript:**
2487
+
```kernelscript
2488
+
fn calculate_sum(a: i32, b: i32) -> result: i32 {
2489
+
result = a + b
2490
+
return
2491
+
}
2492
+
```
2493
+
2494
+
**Generated C:**
2495
+
```c
2496
+
staticintcalculate_sum(int a, int b) {
2497
+
int result; // Named return variable declared
2498
+
result = a + b;
2499
+
return result; // Naked return becomes explicit
2500
+
}
2501
+
```
2502
+
2503
+
### 7.4 Helper Functions
2391
2504
2392
2505
KernelScript supports two types of functions with different scoping rules:
2393
2506
@@ -2463,7 +2576,7 @@ fn main() -> i32 {
2463
2576
}
2464
2577
```
2465
2578
2466
-
### 7.4 eBPF Tail Calls
2579
+
### 7.5 eBPF Tail Calls
2467
2580
2468
2581
KernelScript provides transparent eBPF tail call support that automatically converts function calls to tail calls when appropriate. Tail calls enable efficient program chaining without stack overhead and are especially useful for packet processing pipelines.
0 commit comments