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