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 so recursive parsers don't overflow.
87pub fn run_with_large_stack<F, R>(f: F) -> R
88where
89 F: FnOnce() -> R + Send + 'static,
90 R: Send + 'static,
91{
92 std::thread::Builder::new()
93 .stack_size(64 * 1024 * 1024)
94 .spawn(f)
95 .expect("failed to spawn large stack thread")
96 .join()
97 .unwrap_or_else(|panic| std::panic::resume_unwind(panic))
98}
99
100// Unlexer exports
101pub use unlexer::PtxUnlexer;
102
103// Unparser exports
104pub use unparser::PtxUnparser;