Esempio n. 1
0
def header_ast(src=None, ppfname=PPFNAME):
    src = src or preprocessed_header()
    cp = pycparser.CParser()
    ast = cp.parse(src, ppfname)
    return ast
Esempio n. 2
0
def _get_parser():
    global _parser_cache
    if _parser_cache is None:
        _parser_cache = pycparser.CParser()
    return _parser_cache
Esempio n. 3
0
File: rpc.py Progetto: yao-zl/PYNQ
import collections
import math
import re
import os
from pycparser import c_ast
from pycparser import c_generator
from pycparser.plyparser import ParseError
from copy import deepcopy
from pynq.ps import ZU_ARCH, CPU_ARCH, ZYNQ_ARCH
from .compile import preprocess
from .streams import SimpleMBStream
from .streams import InterruptMBStream
from . import MicroblazeProgram

# Use a global parser and generator
_parser = pycparser.CParser()
_generator = c_generator.CGenerator()

if CPU_ARCH == ZYNQ_ARCH:
    PTR_OFFSET = "0x20000000"
elif CPU_ARCH == ZU_ARCH:
    PTR_OFFSET = "0x80000000"
else:
    PTR_OFFSET = "0x0"


# First we define a series of classes to represent types
# Each class is responsible for one particular type of C
# types
class PrimitiveWrapper:
    """ Wrapper for C primitives that can be represented by
Esempio n. 4
0
def parse_file_with_preprocessing(file_content, machine_model, includes=()):
    preprocessed_content = preprocess(file_content, machine_model, includes)
    preprocessed_content = _rewrite_cproblems(preprocessed_content)
    parser = pycparser.CParser()
    ast = parser.parse(preprocessed_content)
    return ast
Esempio n. 5
0
def _read_syscalls_h(path, include_path, defines):
    output = []

    text = get_header_text(path, include_path, defines=defines,
                           strip_includes=True)

    parser = pycparser.CParser()

    typedefs = [
        'typedef unsigned int qid_t;',
        'typedef long time_t;',
        'typedef unsigned int uid_t;',
        'typedef unsigned int gid_t;',
        'typedef unsigned short old_uid_t;',
        'typedef unsigned short old_gid_t;',
        'typedef int pid_t;',
        'typedef void *cap_user_header_t;',
        'typedef void *cap_user_data_t;',
        'typedef unsigned long old_sigset_t;',
        'typedef int timer_t;',
        'typedef int clockid_t;',
        'typedef unsigned int u32;',
        'typedef unsigned int __u32;',
        'typedef int __s32;',
        'typedef unsigned long long u64;',
        'typedef unsigned long sigset_t;',
        'typedef unsigned int size_t;',
        'typedef struct siginfo_t siginfo_t;',
        'typedef struct sigset_t sigset_t;',
        'typedef struct fd_set fd_set;',
        'typedef void *__sighandler_t;',
        'typedef long long off_t;',
        'typedef long long loff_t;',
        'typedef unsigned short umode_t;',
        'typedef unsigned long aio_context_t;',
        'typedef int key_t;',
        'typedef int mqd_t;',
        'typedef int key_serial_t;',
    ]

    text = '\n'.join(typedefs) + '\n' + text
    text = re.sub(r'asmlinkage|__user', '', text)
    text = re.sub(r'\*\s*\*\s*(\W)', '**__foo\\1', text)

    fileast = parser.parse(text, path)

    if not isinstance(fileast, c_ast.FileAST):
        die('could not parse syscalls.h')

    typedefs = parse_typedefs(fileast)

    for decl in fileast.ext:
        if (not isinstance(decl, c_ast.Decl) or
                not isinstance(decl.type, c_ast.FuncDecl)):
            continue

        if not decl.name.startswith('sys_'):
            continue

        name = decl.name[len('sys_'):]

        output.append('    {!r}: SysCallSig('.format(name))
        output.append('        {!r},'.format(name))
        output.append('        params=[')

        params = decl.type.args.params
        for param in params:
            pdecl = []
            pdecl.append('SysCallParamSig(')
            pdecl.append('    {!r},'.format(param.name))
            pdecl.append('    CType(')
            ctype, ptr_indirection = get_ctypes_type(param.type, typedefs)
            pdecl.append('        {!r},'.format(render_type(param.type)))
            pdecl.append('        {},'.format(ctype))
            pdecl.append('        {}'.format(ptr_indirection))
            pdecl.append('    )')
            pdecl.append('),')

            output.extend('            {}'.format(p) for p in pdecl)

        output.append('        ],')

        ctype, ptr_indirection = get_ctypes_type(decl.type.type, typedefs)
        output.append('        result=CType({!r}, {}, {})'.format(
            render_type(decl.type.type), ctype, ptr_indirection
        ))
        output.append('    ),')

    return output
Esempio n. 6
0
 def parser(self, src):
     src = self.comment_remover(src)
     return pycparser.CParser().parse(src)
Esempio n. 7
0
def convert_function_declarations(source, blacklist):
    return CFFIGenerator(blacklist).visit(pycparser.CParser().parse(source))
Esempio n. 8
0
 def __init__(self, source):
     self.funcs = set()
     self.visit(pycparser.CParser().parse(source))
Esempio n. 9
0
 def __init__(self, default_scopes=None):
     super().__init__(default_scopes)
     self._preprocessor = C99Preprocessor()
     self._parser = pycparser.CParser()
Esempio n. 10
0
 def setUp(self) -> None:
     self.parser = pycparser.CParser(lexer=ghcc.parse.CachedCLexer)
     self.generator = CGenerator()
     self.lexer = ghcc.parse.LexerWrapper()
Esempio n. 11
0
 def setUp(self) -> None:
     self.tempdir = tempfile.TemporaryDirectory()
     self.parser = pycparser.CParser(lexer=ghcc.parse.CachedCLexer)