Ejemplo n.º 1
0
def do_compile(filename, include_paths, arch, reporter):
    coptions = COptions()
    coptions.add_include_paths(include_paths)
    coptions.add_define("FPM_DEFAULT", "1")
    with open(filename, "r") as f:
        obj = cc(f, arch, coptions=coptions, reporter=reporter)
    return obj
Ejemplo n.º 2
0
def do_compile(filename):
    include_paths = [
        os.path.join(newlib_folder, 'libc', 'include'),
        libc_includes,
    ]
    coptions = COptions()
    coptions.add_include_paths(include_paths)
    coptions.add_define('HAVE_CONFIG_H')
    coptions.add_define('__MSP430__')
    with open(filename, 'r') as f:
        obj = cc(f, arch, coptions=coptions)
    return obj
Ejemplo n.º 3
0
def perform_test(filename):
    """ Try to compile the given snippet. """
    logger.info("Step 1: Compile %s!", filename)
    march = "x86_64"

    html_report = os.path.splitext(filename)[0] + "_report.html"

    coptions = COptions()
    root_folder = os.path.join(this_dir, "..", "..", "..")
    libc_folder = os.path.join(root_folder, "librt", "libc")
    libc_include = os.path.join(libc_folder, "include")
    coptions.add_include_path(libc_include)

    # TODO: this should be injected elsewhere?
    coptions.add_define('__LP64__', '1')
    # coptions.enable('freestanding')

    with html_reporter(html_report) as reporter:
        with open(filename, "r") as f:
            try:
                obj1 = api.cc(f, march, coptions=coptions, reporter=reporter)
            except CompilerError as ex:
                ex.print()
                raise
    logger.info("Compilation complete, %s", obj1)

    obj0 = api.asm(io.StringIO(STARTERCODE), march)
    obj2 = api.c3c([io.StringIO(BSP_C3_SRC)], [], march)
    with open(os.path.join(libc_include, "lib.c"), "r") as f:
        obj3 = api.cc(f, march, coptions=coptions)

    obj = api.link([obj0, obj1, obj2, obj3], layout=io.StringIO(ARCH_MMAP))

    logger.info("Step 2: Run it!")

    exe_filename = os.path.splitext(filename)[0] + "_executable.elf"
    with open(exe_filename, "wb") as f:
        write_elf(obj, f, type="executable")
    api.chmod_x(exe_filename)

    logger.info("Running %s", exe_filename)
    test_prog = subprocess.Popen(exe_filename, stdout=subprocess.PIPE)
    exit_code = test_prog.wait()
    assert exit_code == 0
    captured_stdout = test_prog.stdout.read().decode("ascii")

    with open(filename + ".expected", "r") as f:
        expected_stdout = f.read()

    # Compare stdout:
    assert captured_stdout == expected_stdout
Ejemplo n.º 4
0
def compile_8cc():
    """ Compile the 8cc compiler.

    8cc homepage:
    https://github.com/rui314/8cc
    """

    home = os.environ['HOME']
    _8cc_folder = os.path.join(home, 'GIT', '8cc')
    libc_includes = os.path.join(this_dir, '..', 'librt', 'libc', 'include')
    linux_include_dir = '/usr/include'
    arch = api.get_arch('x86_64')
    coptions = COptions()
    include_paths = [
        libc_includes,
        _8cc_folder,
        linux_include_dir,
    ]
    coptions.add_include_paths(include_paths)
    coptions.add_define('BUILD_DIR', '"{}"'.format(_8cc_folder))

    sources = [
        'cpp.c',
        'debug.c',
        'dict.c',
        'gen.c',
        'lex.c',
        'vector.c',
        'parse.c',
        'buffer.c',
        'map.c',
        'error.c',
        'path.c',
        'file.c',
        'set.c',
        'encoding.c',
    ]
    objs = []
    for filename in sources:
        source_path = os.path.join(_8cc_folder, filename)
        with open(source_path, 'r') as f:
            objs.append(api.cc(f, arch, coptions=coptions))
Ejemplo n.º 5
0
def main():
    t1 = time.time()
    failed = 0
    passed = 0
    include_paths = [
        # os.path.join(newlib_folder, 'libc', 'include'),
        # TODO: not sure about the include path below for stddef.h:
        # '/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/include'
        libc_includes,
        micropython_folder,
        port_folder,
    ]
    coptions = COptions()
    coptions.add_include_paths(include_paths)
    coptions.enable('freestanding')
    coptions.add_define('NO_QSTR', '1')
    file_pattern = os.path.join(micropython_folder, 'py', '*.c')
    objs = []
    for filename in glob.iglob(file_pattern):
        print('==> Compiling', filename)
        try:
            obj = do_compile(filename, coptions)
        except CompilerError as ex:
            print('Error:', ex.msg, ex.loc)
            ex.print()
            print_exc()
            failed += 1
            # break
        except Exception as ex:
            print('General exception:', ex)
            print_exc()
            failed += 1
            # break
        else:
            print('Great success!', obj)
            passed += 1
            objs.append(obj)

    t2 = time.time()
    elapsed = t2 - t1
    print('Passed:', passed, 'failed:', failed, 'in', elapsed, 'seconds')
Ejemplo n.º 6
0
"""

home = os.environ['HOME']
core_mark_folder = os.path.join(home, 'GIT', 'coremark')
this_dir = os.path.abspath(os.path.dirname(__file__))
port_folder = os.path.join(core_mark_folder, 'linux64')
libc_folder = os.path.join(this_dir, "..", "librt", "libc")
linux64_folder = os.path.join(this_dir, '..', 'examples', 'linux64')

opt_level = 2
march = api.get_arch('x86_64')
coptions = COptions()
coptions.add_include_path(core_mark_folder)
coptions.add_include_path(port_folder)
coptions.add_include_path(libc_folder)
coptions.add_define('COMPILER_VERSION', '"ppci {}"'.format(ppci_version))
coptions.add_define('FLAGS_STR', '"-O{}"'.format(opt_level))

# Prevent malloc / free usage:
coptions.add_define('MEM_METHOD', 'MEM_STATIC')

# TODO: Hack to enable %f formatting:
coptions.add_define('__x86_64__', '1')

objs = []

crt0_asm = os.path.join(linux64_folder, 'glue.asm')
crt0_c3 = os.path.join(linux64_folder, 'bsp.c3')
linker_script = os.path.join(linux64_folder, 'linux64.mmap')
objs.append(api.asm(crt0_asm, march))
objs.append(api.c3c([crt0_c3], [], march))
Ejemplo n.º 7
0
home = os.environ['HOME']
_8cc_folder = os.path.join(home, 'GIT', '8cc')
this_dir = os.path.abspath(os.path.dirname(__file__))
report_filename = os.path.join(this_dir, 'report_8cc.html')
libc_folder = os.path.join(this_dir, '..', 'librt', 'libc')
libc_includes = os.path.join(libc_folder, 'include')
linux_include_dir = '/usr/include'
arch = 'x86_64'
coptions = COptions()
include_paths = [
    libc_includes,
    _8cc_folder,
    linux_include_dir,
]
coptions.add_include_paths(include_paths)
coptions.add_define('BUILD_DIR', '"{}"'.format(_8cc_folder))


def do_compile(filename, reporter):
    with open(filename, 'r') as f:
        obj = api.cc(f, arch, coptions=coptions, reporter=reporter)
    print(filename, 'compiled into', obj)
    return obj


def main():
    t1 = time.time()
    failed = 0
    passed = 0
    sources = [
        'cpp.c',
Ejemplo n.º 8
0
def analyze_sources(source_dir, defines):
    """ Analyze a directory with sourcecode """

    # Phase 1: acquire ast's:
    arch_info = get_arch('x86_64').info
    coptions = COptions()
    # TODO: infer defines from code:
    coptions.add_define('FPM_DEFAULT')
    coptions.add_include_path(source_dir)
    coptions.add_include_path(LIBC_INCLUDES)
    coptions.add_include_path('/usr/include')
    asts = []
    for source_filename in glob.iglob(os.path.join(source_dir, '*.c')):
        logger.info('Processing %s', source_filename)
        with open(source_filename, 'r') as f:
            source_code = f.read()
        f = io.StringIO(source_code)
        # ast = parse_text(source_code)
        try:
            ast = create_ast(
                f, arch_info, filename=source_filename, coptions=coptions)
            asts.append((source_filename, source_code, ast))
            # break
        except CompilerError as ex:
            print('Compiler error:', ex)
    logger.info("Got %s ast's", len(asts))

    # Phase 2: do some bad-ass analysis:
    global_variables = []
    functions = []
    for source_filename, source_code, ast in asts:
        for decl in ast.declarations:
            if isinstance(decl, declarations.VariableDeclaration):
                global_variables.append(decl)
            elif isinstance(decl, declarations.FunctionDeclaration):
                if decl.body is not None and decl.storage_class != 'static':
                    functions.append(decl)

    functions.sort(key=lambda d: d.name)

    # Phase 3: generate html report?
    with open('analyze_report.html', 'w') as f:
        c_lexer = CLexer()
        formatter = HtmlFormatter(lineanchors='fubar', linenos='inline')
        print('''<html>
        <head>
        <style>
        {}
        </style>
        </head>
        <body>
        '''.format(formatter.get_style_defs()), file=f)

        print('<h1>Overview</h1>', file=f)
        print('<table>', file=f)
        print(
            '<tr><th>Name</th><th>Location</th><th>typ</th></tr>',
            file=f)
        for func in functions:
            tagname = get_tag(func.location.filename)
            name = '<a href="#{2}-{1}">{0}</a>'.format(func.name, func.location.row, tagname)
            print(
                '<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>'.format(
                    name, '', ''),
                file=f)

        print('</table>', file=f)
        print('<h1>Files</h1>', file=f)

        for source_filename, source_code, ast in asts:
            tagname = get_tag(source_filename)
            formatter = HtmlFormatter(lineanchors=tagname, linenos='inline')
            print('<h2>{}</h2>'.format(source_filename), file=f)
            print('<table>', file=f)
            print(
                '<tr><th>Name</th><th>Location</th><th>typ</th></tr>',
                file=f)
            for decl in ast.declarations:
                if isinstance(decl, declarations.VariableDeclaration):
                    tp = 'var'
                elif isinstance(decl, declarations.FunctionDeclaration):
                    tp = 'func'
                else:
                    tp = 'other'

                tp += str(decl.storage_class)

                if source_filename == decl.location.filename:
                    name = '<a href="#{2}-{1}">{0}</a>'.format(decl.name, decl.location.row, tagname)
                else:
                    name = decl.name

                print(
                    '<tr><td>{}</td><td>{}</td><td>{}</td></tr>'.format(
                        name, decl.location, tp),
                    file=f)
            print('</table>', file=f)
            print('''  <div>''', file=f)
            print(highlight(source_code, c_lexer, formatter), file=f)
            print('''  </div>''', file=f)

        print('''</body>
        </html>
        ''', file=f)