Commit Graph

69 Commits

Author SHA1 Message Date
8290fecac2 tests: Add loop and AnnAssign xfail tests (#103)
- process_stmt now raises NotImplementedError on an unsupported statement
  instead of warning: a dropped statement silently changes what the
  program means (a for/while loop compiled to nothing).
- failing_tests/loops/: 8 xfail tests for for/while/break/continue/nested
  loops and map iteration
- failing_tests/assign/annotated_assign.py: xfail for `x: c_int64 = 0`
  (ast.AnnAssign is not handled)
2026-09-23 18:08:27 +05:30
d197a11deb Core: Shorten the unbound-local messages and drop a comment
Review on the previous round: the UnboundLocalError-shaped messages said
in five lines what one says, and the ast.Global branch of process_stmt
explained itself at length for a no-op. Both messages now name the
problem and the fix and stop; the branch keeps its debug log only.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-17 20:46:04 +05:30
3a3b4b2cc1 Core: Say what is unsupported instead of dropping it silently
Three review notes from the globals PR, all about being explicit:

- Augmented assignment on a struct field handles one level of
  dereference. Say so where the target is unpacked, and give
  `obj.inner.field += v` its own message instead of an ast.dump.
- The ast.Global branch of process_stmt is a no-op for a reason
  (declarations are collected before anything is lowered, because the
  binding is function-wide). Spell that out, and log the names at debug
  level rather than leaving a bare pass.
- process_stmt had no else, so any statement the compiler does not
  handle was dropped without a word, which makes a program mean
  something other than what it says. Warn, with the line number.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 15:13:56 +05:30
46062f3434 Core: Reject a name used before its global declaration, as Python does
`global x` covers the whole function body, so a body that reads x above
the declaration has two incompatible readings of that line. Python
rejects it with "name 'x' is used prior to global declaration"; say the
same thing rather than silently picking the global.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 15:13:23 +05:30
5948971469 Core: Shadow a @bpfglobal with a local when a write has no global statement
Assigning a global's name without declaring it `global` was a compile
error. Python makes it a local instead: the name is local for the whole
function body, the global keeps its value, and only a `global` statement
writes through to it. Do that, so the same program means the same thing
in Python and in BPF.

Python's scoping is function-wide and static, which the compiler gets
for free: allocation runs as a pre-pass over the whole body, so a name
bound anywhere is in local_sym_tab before the first statement is
lowered, and reads resolve local-first. What does not come for free is
the read above the binding, which Python answers with UnboundLocalError
rather than the global's value. There is no runtime here in which to
raise that, so the local records where its binding ends and a read above
it is rejected, with the message Python would have given. Silence there
would load an uninitialised stack slot through a name the author
expected to be the global.

`counter += 1` without `global counter` stays an error for the same
reason: it binds a local and reads it in the same statement. Only its
message changes, from a shadowing refusal to the UnboundLocalError it
actually is.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 15:12:56 +05:30
2b77c406de Core: Bind declared globals in local_sym_tab instead of a per-function set
A 'global x' statement is a fact about one function's scope, but it was
tracked as current_func_globals on the compilation-wide context -- per-function
state living at the wrong level, which had to be set before each body and
reset after, and which every read and write site then had to consult in a
specific order relative to local_sym_tab (the source of the parameter
collision fixed in f4ec12d).

The declaration now does what it means: it binds the name in local_sym_tab to
the @bpfglobal's storage, as a LocalSymbol whose var is the GlobalVariable and
whose declared_global flag records why. A GlobalVariable is a pointer to
storage exactly as an alloca is, so every existing path that loads from or
stores through a symbol's var works unchanged. That deletes the dedicated
global-store branch in handle_variable_assignment, the global branch in
handle_aug_assign, the allocation-pass skip, and the reset -- one table, one
lookup, no order to get wrong. Reads of an undeclared global (legal in Python)
still fall back to bpf_globals after local_sym_tab.

Python's rules fall out naturally: a parameter is already in the table when
the declarations are processed, so 'global ctx' on a parameter is detected by
the same membership check. LocalSymbol.__iter__ deliberately still yields
three fields, since several sites tuple-unpack a symbol.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
2026-09-03 11:14:14 +05:30
f4ec12df60 Core: Resolve assignment targets local-first, and reject global on a parameter
Reads resolve a name local-first (_handle_name_expr, get_operand_value, the
printk formatter), but both write paths checked the declared-global set
before local_sym_tab. Normally harmless because the two tables are disjoint
by construction -- except for a parameter whose name matches a @bpfglobal.
With 'global ctx' in a function taking ctx, the write path stored to the
global while a read of the same name picked the parameter and crashed on its
None slot: the same name resolving to different storage depending on which
side of an assignment it sat.

Python forbids the construct outright, and now so does the compiler, with
Python's own wording: SyntaxError: name 'ctx' is parameter and global. Both
write paths now resolve local-first like the reads, so all name lookups read
the same way; and an augmented assignment to the context parameter itself
gets a clear error instead of a store to None.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
2026-09-03 10:32:10 +05:30
aa321dbd47 Core: Lower augmented assignment directly instead of desugaring
The first version synthesised an Assign(BinOp(...)) node and fed it back
through handle_assign. That is a standard compiler move, but it is not how
this compiler works: every pass walks the tree the user wrote, and a node
invented mid-codegen is invisible to the allocation pass that already ran,
carries no source location for diagnostics, and shows up in logs as a
statement nobody typed.

handle_aug_assign now resolves the target slot (declared global, local, or
struct field), loads it, evaluates the right-hand side, applies the operator,
and stores back. Semantic agreement with "x = x op v" no longer rests on
shared AST but on shared value-level helpers: the RHS goes through
get_operand_value like any other read, and the operator table is extracted
from binary-op evaluation into apply_binop so it exists exactly once.

Two behavioural refinements fall out: augmented assignment to a global name
without a global statement is now the same loud SyntaxError plain assignment
gives (previously it slipped past the allocation pass, which only walks
Assign), and struct-field targets compute their GEP once rather than twice.
Target read precedes RHS evaluation, matching Python.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
2026-09-03 07:19:07 +05:30
7f5773e5d8 Core: Make XDP return names respect local and global shadowing
handle_return consulted the hardcoded XDP action table before any symbol
table, so with a local XDP_PASS = 55 in scope, "return XDP_PASS" silently
returned 2. clang treats the equivalent C (a local shadowing an enum constant)
as the local winning, and every other resolution site in the compiler already
resolves local, then global, then vmlinux -- the return fast path was the one
place with the order inverted. It now applies only when the name is bound in
neither table.

Also warn when a @bpfglobal shadows a vmlinux enum constant. C rejects that
outright as a redefinition; PythonBPF follows the rebinding semantics the
Python file itself has (the global wins, consistently), but the collision is
worth a compile-time warning rather than silence.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
2026-09-03 06:40:11 +05:30
4ee96e7461 Core: Support augmented assignment by desugaring to x = x op v
AugAssign previously raised 'not supported' outright. Desugaring to an
ordinary assignment with a BinOp reproduces its Python semantics for Name and
Attribute targets and reuses the whole existing assignment path -- locals,
struct fields, and the new globals all work, so 'counter += 1' (the canonical
global idiom in the kernel selftest corpus) compiles to load/add/store on
@counter.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
2026-09-01 16:43:00 +05:30
930594d932 Core: Support writes to @bpfglobal variables via the global statement
Python's own scoping marks the write: 'global cg_id' declares that assignments
to cg_id in this function mean the BPF global, and the assignment then emits
the plain 'store i64 %v, ptr @cg_id' of the C reference, with the same implicit
widening/truncation rules as local assignments.

Without the declaration, an assignment to a global's name is refused:

    SyntaxError: assignment to 'cg_id' shadows the BPF global of the same
    name -- add 'global cg_id' to write to it

That is deliberate. In real Python such an assignment creates a shadowing
local; silently compiling it as either a local or a global store would be wrong
in one direction or the other, so it is a loud error instead. 'global' naming
something that is not a @bpfglobal is also a compile error.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
2026-09-01 16:40:49 +05:30
559d0570ff regenerate and symlink a single master vmlinux.py for make test
- make test/test-cov/test-verifier now depend on a 'vmlinux' target that
  regenerates vmlinux.py from the running kernel's BTF and symlinks it
  into every directory under tests/, replacing the stale, duplicated
  vmlinux.py fixtures that used to be committed there.
- collector.py now skips the 'vmlinux.py' filename when collecting BPF
  test cases, since it's a fixture module, not a test program - it was
  previously being mis-collected and compiled as a bogus test.
- functions_pass.py: llvmlite >=0.49 renamed the 'nocapture' argument
  attribute to 'captures(none)'.
2026-08-30 19:49:28 +05:30
b47063c49c Core: Resolve plain-ctypes context parameter in helper args
process_func_body only entered the context parameter into local_sym_tab
when its annotation was a vmlinux struct, so a program declared as
`def prog(ctx: c_void_p)` had no `ctx` symbol at all. Passing `ctx` to a
helper that takes a raw pointer -- probe_read(dst, size, ctx) -- failed
with "Variable 'ctx' not found in local symbol table".

Register the parameter for plain ctypes annotations too, keeping `var`
as None: that is the established sentinel meaning "this symbol is the
context parameter, read it from func.args[0]" (see
VmlinuxHandler.handle_vmlinux_struct_field). Metadata holds the
annotated Python type, mirroring the vmlinux branch.

Teach the consumer side accordingly: get_or_create_ptr_from_arg now
recognises the sentinel and hands back the function's first argument
instead of an alloca. The special case lives at the call site rather
than inside get_var_ptr_from_name so that helper stays a pure symbol
table lookup -- the call site already has `func` and `builder` in scope,
and get_var_ptr_from_name has no other callers to update.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 04:38:49 +05:30
c04e32bd24 Core: Remove spurious comments from functions_pass 2026-03-28 12:15:41 +05:30
ec4a6852ec PythonBPF: Add Compilation Context to allow parallel compilation of multiple bpf programs 2026-02-21 18:59:33 +05:30
19b42b9a19 Allocate hashmap lookup return vars based on the value type of said hashmap 2025-11-19 04:09:51 +05:30
5a8b64f1d9 Merge pull request #64 from pythonbpf/all_helpers
Add support for all eBPF helpers
2025-11-07 19:26:55 +05:30
338d4994d8 Fix count_temps_in_call to only look for Pointer args of a helper_sig 2025-11-05 17:36:37 +05:30
963e2a8171 Change ScratchPoolManager to use typed scratch space 2025-11-04 14:16:44 +05:30
123a92af1d Change allocation pass to generate typed temp variables 2025-11-04 06:20:39 +05:30
752f564d3f Change count_temps_in_call to return hashmap of types 2025-11-04 05:40:22 +05:30
7bf6f9c48c add function_debug_info.py and format 2025-10-26 15:12:36 +05:30
a1fe2ed4bc change to 64 bit pointers. May be an issue. revert this commit if issues arise 2025-10-26 15:00:53 +05:30
c6b5ecb47e find global variable ir and field data from metadata 2025-10-24 03:34:27 +05:30
30bcfcbbd0 remove compile error on normal c_void_p in arg and separate localsymbol to avoid circular dep 2025-10-24 03:08:39 +05:30
f18a4399ea format chore 2025-10-24 02:40:07 +05:30
64674cf646 add alloc for only i64 2025-10-24 02:06:39 +05:30
36a1a0903e Merge branch 'master' into vmlinux-handler 2025-10-22 12:02:51 +05:30
f2bc7f1434 pass context to memory allocation 2025-10-22 12:01:52 +05:30
b3921c424d parse context from first function argument to local symbol table 2025-10-22 11:40:49 +05:30
84fdf52658 Merge branch 'master' into bcc_examples 2025-10-22 04:09:07 +05:30
c3f3d1e564 format chore 2025-10-21 03:43:22 +05:30
5955db88cf add vmlinux expressions to eval expr 2025-10-21 03:24:27 +05:30
e499c29d42 float vmlinux_symtab till process_func_body 2025-10-20 22:13:38 +05:30
c56928bc8a Add create_targets_and_rvals, use it in handle_assign to enable tuple assignment 2025-10-14 23:30:59 +05:30
5d0a888542 Remove deadcode and seperate modules for pythonbpf.functions 2025-10-13 04:41:46 +05:30
0042280ff1 Rename public API and remove deadcode in return_utils 2025-10-13 04:23:58 +05:30
e0ad1bfb0f Move bulk of allocation logic to allocation_pass 2025-10-12 12:14:46 +05:30
0f6971bcc2 Refactor allocate_mem 2025-10-12 11:34:40 +05:30
08c0ccf0ac Pass map_sym_tab to handle_struct_field_assign 2025-10-12 10:37:20 +05:30
64e44d0d58 Use handle_struct_field_assignment in handle_assign 2025-10-12 10:30:46 +05:30
105c5a7bd0 Cleanup handle_assign 2025-10-12 10:12:45 +05:30
2cf68f6473 Allow map-based helpers to be used as helper args / within binops which are helper args 2025-10-12 07:57:55 +05:30
207f714027 Use scratch space to store consts passed to helpers 2025-10-12 04:17:37 +05:30
6bce29b90f Allocate scratch space for temp vars at the end of allocate_mem 2025-10-12 00:37:57 +05:30
321415fa28 Add update_max_temps_for_stmt in allocate_mem 2025-10-12 00:33:07 +05:30
8776d7607f Add count_temps_in_call to call scratch space needed in a helper call 2025-10-12 00:17:10 +05:30
99aacca94b WIP: allow pointer assignments to var 2025-10-10 13:48:40 +05:30
1d517d4e09 Add double_alloc in alloc_mem 2025-10-10 12:28:45 +05:30
047f361ea9 Allocate twice for map lookups 2025-10-10 06:09:46 +05:30