CUDA Source Backend

CUDA source backend exports for CrossGL Translator.

CUDA AST Node definitions

class crosstl.backend.CUDA.CudaAst.AtomicOperationNode(operation, args)[source]

Bases: FunctionCallNode

Node representing a CUDA atomic operation

class crosstl.backend.CUDA.CudaAst.ConstantMemoryNode(vtype, name, value=None)[source]

Bases: VariableNode

Node representing constant memory variable declaration

class crosstl.backend.CUDA.CudaAst.CudaBuiltinNode(builtin_name, component=None)[source]

Bases: ASTNode

Node representing CUDA built-in variables (threadIdx, blockIdx, etc.)

class crosstl.backend.CUDA.CudaAst.KernelLaunchNode(kernel_name, blocks, threads, shared_mem=None, stream=None, args=None)[source]

Bases: ASTNode

Node representing a kernel launch: kernel<<<blocks, threads>>>(args)

class crosstl.backend.CUDA.CudaAst.KernelNode(return_type, name, params, body, attributes=None)[source]

Bases: FunctionNode

Node representing a CUDA kernel function (marked with __global__)

class crosstl.backend.CUDA.CudaAst.SharedMemoryNode(vtype, name, size=None)[source]

Bases: VariableNode

Node representing shared memory variable declaration

class crosstl.backend.CUDA.CudaAst.TextureAccessNode(texture_name, coordinates)[source]

Bases: ASTNode

Node representing texture memory access

Lexer for importing CUDA source into CrossGL Translator.

class crosstl.backend.CUDA.CudaLexer.CudaLexer(code)[source]

Bases: object

Tokenize CUDA source for the CUDA backend parser.

classmethod from_file(filepath)[source]

Create a lexer instance from a file

Return type:

CudaLexer

token_generator()[source]

Generator function that yields tokens one at a time

Return type:

Iterator[Tuple[str, str]]

tokenize()[source]

Tokenize the input code and return list of tokens

Return type:

List[Tuple[str, str]]

class crosstl.backend.CUDA.CudaLexer.Lexer(input_str)[source]

Bases: object

Compatibility wrapper around CudaLexer

next()[source]

Return the next token and advance the cursor.

peek()[source]

Return the next token without advancing the cursor.

class crosstl.backend.CUDA.CudaLexer.TokenType(value)[source]

Bases: Enum

Token names emitted by the CUDA lexer.

CUDA Parser Implementation

class crosstl.backend.CUDA.CudaParser.CudaParser(tokens)[source]

Bases: object

Parse CUDA tokens into the CUDA backend shader AST.

eat(expected_type)[source]

Consume a token of the expected type

is_range_for_statement()[source]

Check if the current parenthesized for header is a range-for loop.

is_variable_declaration()[source]

Check if current position is a variable declaration

parse()[source]

Parse the entire CUDA program into a ShaderNode.

parse_additive_expression()[source]

Parse additive expression

parse_array_suffix()[source]

Parse one or more C-style array declarator suffixes.

parse_assignment_expression()[source]

Parse assignment expression

parse_bitwise_and_expression()[source]

Parse bitwise AND expression

parse_bitwise_or_expression()[source]

Parse bitwise OR expression

parse_bitwise_xor_expression()[source]

Parse bitwise XOR expression

parse_block()[source]

Parse a block of statements

parse_do_while_statement()[source]

Parse do-while loop

parse_equality_expression()[source]

Parse equality expression

parse_expression()[source]

Parse expression with precedence

parse_for_statement()[source]

Parse for loop

parse_function()[source]

Parse function declaration including kernels

parse_global_variable()[source]

Parse global variable declaration

parse_if_statement()[source]

Parse if statement

parse_kernel_launch(kernel_name)[source]

Parse CUDA kernel launch syntax

parse_logical_and_expression()[source]

Parse logical AND expression

parse_logical_or_expression()[source]

Parse logical OR expression

parse_multiplicative_expression()[source]

Parse multiplicative expression

parse_parameter()[source]

Parse a single parameter

parse_parameters()[source]

Parse function parameters

parse_postfix_expression()[source]

Parse postfix expression

parse_preprocessor()[source]

Parse preprocessor directives

parse_primary_expression()[source]

Parse primary expression

parse_range_for_statement()[source]

Parse a C++ range-based for loop after ‘for (’ has been consumed.

parse_relational_expression()[source]

Parse relational expression

parse_return_statement()[source]

Parse return statement

parse_shift_expression()[source]

Parse bit shift expression

parse_statement()[source]

Parse a single statement

parse_struct()[source]

Parse struct declaration

parse_switch_statement()[source]

Parse switch statement

parse_sync_statement()[source]

Parse CUDA synchronization statements

parse_ternary_expression()[source]

Parse ternary conditional operator

parse_type()[source]

Parse type specification

parse_unary_expression()[source]

Parse unary expression

parse_variable_declaration()[source]

Parse variable declaration

parse_variable_declaration_list()[source]

Parse one or more comma-separated variable declarations.

parse_while_statement()[source]

Parse while loop

peek_function()[source]

Check if the next tokens form a function declaration

peek_variable()[source]

Check if the next tokens form a variable declaration

CUDA to CrossGL Code Generator

class crosstl.backend.CUDA.CudaCrossGLCodeGen.CudaToCrossGLConverter[source]

Bases: object

Serialize CUDA backend AST nodes back into CrossGL source.

convert_cuda_builtin_function(func_name)[source]

Convert CUDA built-in functions to CrossGL equivalents.

convert_cuda_pointer_type(cuda_type)[source]

Convert a CUDA pointer type into nested CrossGL pointer syntax.

convert_cuda_type_to_crossgl(cuda_type)[source]

Convert CUDA types to CrossGL equivalents

emit(code)[source]

Append a line of CrossGL output using the current indentation level.

emit_statement(stmt)[source]

Render and append one converted statement.

generate(ast_node)[source]

Generate complete CrossGL source from a parsed CUDA AST.

generic_visit(node)[source]

Fallback converter for primitive values, lists, and unknown nodes.

visit(node)[source]

Dispatch a CUDA backend AST node to its converter method.

visit_FunctionNode(node)[source]

Render a CUDA function node as a CrossGL function.

visit_ShaderNode(node)[source]

Render a CUDA shader/program AST as a CrossGL shader block.

visit_kernel_as_compute_shader(kernel)[source]

Render a CUDA kernel as a CrossGL compute shader block.