Skip to main content

ptx_parser/
lib.rs

1#![recursion_limit = "512"]
2
3//! PTX (Parallel Thread Execution) parser for NVIDIA GPU assembly language.
4//!
5//! This library provides a complete parser for PTX assembly code, including:
6//! - Lexical analysis (tokenization)
7//! - Syntactic parsing into structured types
8//! - Unparsing back to PTX source code
9//!
10//! # Quick Start
11//!
12//! ```no_run
13//! use ptx_parser::{parse_ptx};
14//! use ptx_parser::r#type::{Module, ModuleDirective, Instruction};
15//!
16//! let source = r#"
17//!     .version 8.5
18//!     .target sm_90
19//!     .address_size 64
20//!     
21//!     .entry kernel() {
22//!         add.s32 %r1, %r2, %r3;
23//!         ret;
24//!     }
25//! "#;
26//!
27//! let module: Module = parse_ptx(source).expect("Failed to parse PTX");
28//! println!("Parsed {} directives", module.directives.len());
29//! ```
30//!
31//! # Type Organization
32//!
33//! All types are re-exported at `ptx_parser::r#type::*` for easy access:
34//!
35//! ```rust
36//! use ptx_parser::r#type::{
37//!     Module,              // Root AST node
38//!     Instruction,         // Instruction with label/predicate
39//!     Predicate,           // Predicate guard
40//!     Operand,             // Operand types
41//!     EntryFunctionDirective,
42//!     FuncFunctionDirective,
43//!     // ... all other types
44//! };
45//! ```
46//!
47//! Instruction variants are under `instruction::`:
48//!
49//! ```rust
50//! use ptx_parser::r#type::instruction::{Inst, add, mov};
51//! ```
52
53// Internal modules - not part of public API
54mod lexer;
55mod parser;
56pub mod span;
57mod unlexer;
58mod unparser;
59
60// Type definitions - AST nodes (public)
61pub mod r#type;
62
63// Pretty-print module - for displaying AST as tree (public)
64pub mod pretty_print;
65
66// Re-export derive macro for the `Spanned` trait so downstream crates can use it.
67pub use ptx_90_parser_span_derive::Spanned;
68
69// Re-export procedural macros for constructor mapping and error handling
70pub use ptx_90_parser_construct::{c, cclosure, err, func, ok, okmap};
71
72// Re-export convenience macros for parser combinators
73// Note: map! and try_map! are declarative macros defined in parser/util.rs
74// They automatically wrap patterns with cclosure! for cleaner syntax
75
76// Re-export commonly used items for convenience
77
78// Lexer exports
79pub use lexer::{LexError, PtxToken, tokenize};
80
81// Parser exports
82pub use parser::{
83    ParseErrorKind, PtxParseError, PtxParser, PtxTokenStream, Span, StreamPosition, parse_ptx,
84};
85
86/// Execute `f` on a dedicated thread with a larger stack in debug builds to
87/// avoid overflows from deep recursion. In release builds, run directly without
88/// the extra thread to reduce overhead.
89#[cfg(debug_assertions)]
90pub fn run_with_large_stack<F, R>(f: F) -> R
91where
92    F: FnOnce() -> R + Send + 'static,
93    R: Send + 'static,
94{
95    std::thread::Builder::new()
96        .stack_size(64 * 1024 * 1024)
97        .spawn(f)
98        .expect("failed to spawn large stack thread")
99        .join()
100        .unwrap_or_else(|panic| std::panic::resume_unwind(panic))
101}
102
103#[cfg(not(debug_assertions))]
104pub fn run_with_large_stack<F, R>(f: F) -> R
105where
106    F: FnOnce() -> R + Send + 'static,
107    R: Send + 'static,
108{
109    f()
110}
111
112// Unlexer exports
113pub use unlexer::PtxUnlexer;
114
115// Unparser exports
116pub use unparser::PtxUnparser;