Skip to content

Add raylib bindings and fix generic import rewriting - #2

Merged
CierCier merged 3 commits into
CierCier:mainfrom
jR4dh3y:feature/raylib-bindings
May 21, 2026
Merged

CierCier merged 3 commits into
CierCier:mainfrom
jR4dh3y:feature/raylib-bindings

Conversation

@jR4dh3y

@jR4dh3y jR4dh3y commented May 21, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add a new std/gfx/raylib.ag binding module covering raylib constants, structs, enums, and a broad set of C FFI declarations.
  • Add examples/gfx_raylib.ag to demonstrate drawing a spinning cube with raylib.
  • Fix import alias rewriting for generic where-clauses so the rewriter updates an existing clause without looping incorrectly.

Testing

  • Not run
  • Reviewed the binding and example additions for FFI signatures and usage consistency
  • Confirmed the generic import rewriting change is limited to where-clause handling in agc/src/parser/import_hook.rs

- add std/gfx/raylib bindings and a spinning cube example
- fix where-clause rewriting to avoid looping over the clause
Copilot AI review requested due to automatic review settings May 21, 2026 04:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new std/gfx/raylib.ag FFI binding module for raylib, adds a raylib-based graphics example, and fixes a looping bug in the import-alias rewriter when processing generic where clauses.

Changes:

  • Add a comprehensive raylib binding module (std/gfx/raylib.ag) with structs/enums/constants and many extern "C" declarations.
  • Add a raylib example (examples/gfx_raylib.ag) intended to demonstrate rendering a spinning cube.
  • Fix ImportAliasRewriter::rewrite_generics to avoid an infinite loop by rewriting an existing where_clause once.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.

File Description
std/gfx/raylib.ag Adds raylib FFI types/constants/functions for use from Silver code.
examples/gfx_raylib.ag Adds a raylib-based demo program (currently re-declares FFI locally).
agc/src/parser/import_hook.rs Fixes generic where-clause alias rewriting to avoid looping.
Comments suppressed due to low confidence (3)

examples/gfx_raylib.ag:23

  • InitWindow is declared with only (width, height), but raylib’s C API takes a third const char* title parameter. With the current extern declaration, the call in main() will use the wrong ABI/stack layout and can crash immediately. Update the extern signature (and the call site) to include the window title, or import and use the std/gfx/raylib.ag binding which already declares the correct signature.
#[link(raylib)]
extern "C" void InitWindow(i32 width, i32 height);
extern "C" void SetTargetFPS(i32 fps);
extern "C" bool WindowShouldClose();
extern "C" f64 GetTime();

examples/gfx_raylib.ag:29

  • The extern declarations use u32 for ClearBackground/DrawRectangle/DrawLine/DrawTriangle color parameters, but raylib expects a Color struct passed by value. Declaring these parameters as u32 is an ABI mismatch and may lead to incorrect argument passing on some platforms. Prefer using the Color definition from std/gfx/raylib.ag (and its function declarations) instead of re-declaring these APIs with a different type.
extern "C" void BeginDrawing();
extern "C" void ClearBackground(u32 color);
extern "C" void DrawRectangle(i32 posX, i32 posY, i32 width, i32 height, u32 color);
extern "C" void DrawLine(i32 startPosX, i32 startPosY, i32 endPosX, i32 endPosY, u32 color);
extern "C" void DrawTriangle(Vec2 v1, Vec2 v2, Vec2 v3, u32 color);
extern "C" void EndDrawing();

examples/gfx_raylib.ag:68

  • fill_quad draws each triangle twice by submitting the same vertices in both winding orders (a,b,cc and cc,b,a, then a,cc,d and d,cc,a). For raylib’s 2D DrawTriangle (filled) this is redundant work and can noticeably increase overdraw in more complex scenes. If backface concerns aren’t relevant here, draw the quad using only two triangles (or use a dedicated quad helper if available).
    DrawTriangle(a, b, cc, color);
    DrawTriangle(cc, b, a, color);
    DrawTriangle(a, cc, d, color);
    DrawTriangle(d, cc, a, color);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread examples/gfx_raylib.ag Resolved
Comment on lines 416 to 420
fn rewrite_generics(&mut self, generics: &mut ast::Generics) {
while let Some(clause) = &mut generics.where_clause {
if let Some(clause) = &mut generics.where_clause {
for predicate in &mut clause.predicates {
match predicate {
ast::WherePredicate::Type {
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
@CierCier

Copy link
Copy Markdown
Owner

@copilot std/gfx/raylib current uses multiple extern statements. these can wrapped inside a single extern block for better readability. See std/io.ag for example

@CierCier
CierCier merged commit b52f801 into CierCier:main May 21, 2026
CierCier added a commit that referenced this pull request Aug 1, 2026
Add raylib bindings and fix generic import rewriting
CierCier added a commit that referenced this pull request Aug 16, 2026
The compiler already knows every function's source span, so the backtrace
symbol table now carries {addr, name, file, line} instead of {addr, name}:
- emit_function_body records (llvm name -> basename, declaration line) from
  the lexer source registry + span (works without -g).
- emit_backtrace_table emits the 4-field records with per-function file/line
  string globals.
- std/rt/backtrace.ag prints '#N  <name> at <file>:<line> (0x...)'.
- AGM magic bumped to v6: stale .agm library objects carry the old 2-field
  records and would misparse.

Frames now read like:
  #0  __silver_assert_failed at memory.ag:399 (0x000000000020e92d)
  #1  level3 at btprobe.ag:3 (0x00000000002124da)
  #2  level2 at btprobe.ag:8 (0x0000000000212505)

Verified: cargo 328/328, run_tests 132/132 (backtrace_test now greps for
'<name> at <file>:' positions), clippy at the single pre-existing warning.
CierCier added a commit that referenced this pull request Aug 16, 2026
The runtime now prints the exact source line of each frame and the values
of spilled arguments — no assembly diving needed to check whether a param
is garbled:

  assertion failed at /tmp/btprobe.ag:4: level3 guard
  backtrace:
    #0  __silver_assert_failed at memory.ag:425 (0x...)
    args: file=2099240 line=4 msg=2099256
    #1  level3 at btprobe.ag:4 (0x...)
    args: x=42
    #2  level2 at btprobe.ag:9 (0x...)
    args: x=42
    ...

DWARF sections are not allocated into the running binary, so the compiler
parses its own emitted object and folds the data into alloc'd,
link-time-resolved tables (agc/src/codegen/dwarf_bt.rs):
- ELF reader (section headers, symtab, RELA relocations — sh_info is the
  target section, addends are raw 64-bit two's complement).
- .debug_line v4/v5 decoder: peeked dir/file tables (name, dir idx, mtime,
  size order), line_base as a signed byte, standard opcodes applied
  (advance_pc/advance_line/set_file/copy/const_add_pc), special-opcode
  address advance divided by line_range, relocated set_address values.
- .debug_info formal parameters: abbrev tables, strp with R_X86_64_32
  relocations applied, exprloc DW_OP_fbreg (frame base is rbp) / DW_OP_reg.
- emit_bt_debug_tables: __silver_bt_lines ({fn, offset, line, file}) and
  __silver_bt_args ({fn, count, {name, fbreg, size}}) globals, linkonce_odr,
  always emitted (empty when no DWARF). The object is re-emitted with the
  tables; the parsed offsets stay valid because the debug sections are
  unchanged.

Unit test compiles a probe with -g and asserts line + param extraction
against the real object. backtrace_test now greps for args: x=.

Verified: cargo 329/329, run_tests 132/132, clippy at the single
pre-existing warning.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants