예제 #1
0
파일: nodes.py 프로젝트: pyprogrammer/ctree
    def _compile(self, program_text, compilation_dir):
        import ctree
        from ctree.util import truncate

        c_src_file = os.path.join(compilation_dir, self.get_filename())
        ll_bc_file = os.path.join(compilation_dir, self.get_bc_filename())
        log.info("file for generated C: %s", c_src_file)
        log.info("file for generated LLVM: %s", ll_bc_file)

        # syntax-highlight and print C program
        highlighted = highlight(program_text, 'c')
        log.info("generated C program: (((\n%s\n)))", highlighted)

        # write program text to C file
        with open(c_src_file, 'w') as c_file:
            c_file.write(program_text)

        # call clang to generate LLVM bitcode file
        CC = ctree.CONFIG.get(self.config_target, 'CC')
        CFLAGS = ctree.CONFIG.get(self.config_target, 'CFLAGS')
        compile_cmd = "%s -emit-llvm %s -o %s -c %s" % (CC, CFLAGS, ll_bc_file, c_src_file)
        log.info("compilation command: %s", compile_cmd)
        subprocess.check_call(compile_cmd, shell=True)

        # load llvm bitcode
        import llvm.core

        with open(ll_bc_file, 'rb') as bc:
            ll_module = llvm.core.Module.from_bitcode(bc)

        # syntax-highlight and print LLVM program
        highlighted = highlight(str(ll_module), 'llvm')
        log.debug("generated LLVM Program: (((\n%s\n)))", highlighted)

        return ll_module
예제 #2
0
파일: nodes.py 프로젝트: i-Zaak/ctree
    def _compile(self, program_text):
        # print(repr(self.path), repr(self.get_filename()))
        c_src_file = os.path.join(self.path, self.get_filename())
        so_file = os.path.join(self.path, self.get_so_filename())
        program_hash = hashlib.sha512(program_text.strip().encode()).hexdigest()
        so_file_exists = os.path.exists(so_file)
        old_hash = self.program_hash
        hash_match = old_hash == program_hash
        log.debug("Old hash: %s \n New hash: %s", old_hash, program_hash)
        recreate_c_src = program_text and program_text != self.empty and not hash_match
        recreate_so = recreate_c_src or not so_file_exists

        log.debug("RECREATE_C_SRC: %s \t RECREATE_so: %s \t HASH_MATCH: %s",
                 recreate_c_src, recreate_so, hash_match)

        if not program_text:
            log.debug("Program not found. Attempting to use cached version")

        #create c_src
        if recreate_c_src:
            with open(c_src_file, 'w') as c_file:
                c_file.write(program_text)
            log.info("file for generated C: %s", c_src_file)
            # syntax-highlight and print C program
            highlighted = highlight(program_text, 'c')
            log.info("generated C program: (((\n%s\n)))", highlighted)
            self.program_hash = program_hash


        #create ll_bc_file
        if recreate_so:
            # call clang to generate LLVM bitcode file
            log.debug('Regenerating so.')
            CC = ctree.CONFIG.get(self.config_target, 'CC')
            CFLAGS = ctree.CONFIG.get(self.config_target, 'CFLAGS')
            LDFLAGS = ctree.CONFIG.get(self.config_target, 'LDFLAGS')
            compile_cmd = "%s -shared %s -o %s %s %s" % (CC, CFLAGS, so_file,
                    c_src_file, LDFLAGS)
            log.info("compilation command: %s", compile_cmd)
            subprocess.check_call(compile_cmd, shell=True)
            # log.info("file for generated so: %s", so_file)

        #use cached version otherwise
        if not (so_file_exists or recreate_so):
            raise NotImplementedError('No Cached version found')


        # load llvm bitcode
        # import llvm.core
        # import llvmlite.binding as llvm

        # with open(ll_bc_file, 'rb') as bc:
        #     ll_module = llvm.module.parse_bitcode(bc.read())

        # syntax-highlight and print LLVM program
        #preserve_src_drhighlighted = highlight(str(ll_module), 'llvm')
        #log.debug("generated LLVM Program: (((\n%s\n)))", highlighted)

        return so_file
예제 #3
0
    def _compile(self, program_text):
        # print(repr(self.path), repr(self.get_filename()))
        c_src_file = os.path.join(self.path, self.get_filename())
        so_file = os.path.join(self.path, self.get_so_filename())
        program_hash = hashlib.sha512(
            program_text.strip().encode()).hexdigest()
        so_file_exists = os.path.exists(so_file)
        old_hash = self.program_hash
        hash_match = old_hash == program_hash
        log.debug("Old hash: %s \n New hash: %s", old_hash, program_hash)
        recreate_c_src = program_text and program_text != self.empty and not hash_match
        recreate_so = recreate_c_src or not so_file_exists

        log.debug("RECREATE_C_SRC: %s \t RECREATE_so: %s \t HASH_MATCH: %s",
                  recreate_c_src, recreate_so, hash_match)

        if not program_text:
            log.debug("Program not found. Attempting to use cached version")

        #create c_src
        if recreate_c_src:
            with open(c_src_file, 'w') as c_file:
                c_file.write(program_text)
            log.info("file for generated C: %s", c_src_file)
            # syntax-highlight and print C program
            highlighted = highlight(program_text, 'c')
            log.info("generated C program: (((\n%s\n)))", highlighted)
            self.program_hash = program_hash

        #create ll_bc_file
        if recreate_so:
            # call clang to generate LLVM bitcode file
            log.debug('Regenerating so.')
            CC = ctree.CONFIG.get(self.config_target, 'CC')
            CFLAGS = ctree.CONFIG.get(self.config_target, 'CFLAGS')
            LDFLAGS = ctree.CONFIG.get(self.config_target, 'LDFLAGS')
            compile_cmd = "%s -shared %s -o %s %s %s" % (CC, CFLAGS, so_file,
                                                         c_src_file, LDFLAGS)
            log.info("compilation command: %s", compile_cmd)
            subprocess.check_call(compile_cmd, shell=True)
            # log.info("file for generated so: %s", so_file)

        #use cached version otherwise
        if not (so_file_exists or recreate_so):
            raise NotImplementedError('No Cached version found')

        # load llvm bitcode
        # import llvm.core
        # import llvmlite.binding as llvm

        # with open(ll_bc_file, 'rb') as bc:
        #     ll_module = llvm.module.parse_bitcode(bc.read())

        # syntax-highlight and print LLVM program
        #preserve_src_drhighlighted = highlight(str(ll_module), 'llvm')
        #log.debug("generated LLVM Program: (((\n%s\n)))", highlighted)

        return so_file
예제 #4
0
파일: jit.py 프로젝트: leonardt/ctree
    def __init__(self, entry_point_name, project, entry_point_typesig, extra_args=tuple()):
        assert isinstance(project, Project), \
            "Expected a Project but it got a %s." % type(project)
        assert project.parent is None, \
            "Expected null project.parent, but got: %s." % type(project.parent)

        VerifyOnlyCtreeNodes().visit(project)

        self.module = project.codegen()
        highlighted = highlight(str(self.module.ll_module), 'llvm')
        log.debug("full LLVM program is: <<<\n%s\n>>>" % highlighted)
        self.fn = self.module.get_callable(entry_point_name,
                                           entry_point_typesig)
        self._extra_args = extra_args
예제 #5
0
    def _compile(self, program_text, compilation_dir):
        import ctree
        from ctree.util import truncate

        c_src_file = os.path.join(compilation_dir, self.get_filename())
        ll_bc_file = os.path.join(compilation_dir, self.get_bc_filename())
        log.info("file for generated C: %s", c_src_file)
        log.info("file for generated LLVM: %s", ll_bc_file)

        # syntax-highlight and print C program
        highlighted = highlight(program_text, 'c')
        log.info("generated C program: (((\n%s\n)))", highlighted)

        # write program text to C file
        with open(c_src_file, 'w') as c_file:
            c_file.write(program_text)

        # call clang to generate LLVM bitcode file
        CC = ctree.CONFIG.get(self.config_target, 'CC')
        CFLAGS = ctree.CONFIG.get(self.config_target, 'CFLAGS')
        compile_cmd = "%s -emit-llvm %s -o %s -c %s" % (CC, CFLAGS, ll_bc_file,
                                                        c_src_file)
        log.info("compilation command: %s", compile_cmd)
        subprocess.check_call(compile_cmd, shell=True)

        # load llvm bitcode
        import llvm.core

        with open(ll_bc_file, 'rb') as bc:
            ll_module = llvm.core.Module.from_bitcode(bc)

        # syntax-highlight and print LLVM program
        highlighted = highlight(str(ll_module), 'llvm')
        log.debug("generated LLVM Program: (((\n%s\n)))", highlighted)

        return ll_module
예제 #6
0
    def _compile(self, entry_point_name, project_node, entry_point_typesig,
                 **kwargs):
        """
        Returns a python callable.
        """
        assert isinstance(project_node, Project), \
            "Expected a Project but it got a %s." % type(project_node)

        VerifyOnlyCtreeNodes().visit(project_node)

        self._module = project_node.codegen(**kwargs)

        highlighted = highlight(str(self._module.ll_module), 'llvm')
        log.debug("full LLVM program is: <<<\n%s\n>>>" % highlighted)

        return self._module.get_callable(entry_point_name, entry_point_typesig)
예제 #7
0
파일: jit.py 프로젝트: pyprogrammer/ctree
    def _compile(self, entry_point_name, project_node, entry_point_typesig,
                 **kwargs):
        """
        Returns a python callable.
        """
        assert isinstance(project_node, Project), \
            "Expected a Project but it got a %s." % type(project_node)

        VerifyOnlyCtreeNodes().visit(project_node)

        self._module = project_node.codegen(**kwargs)

        highlighted = highlight(str(self._module.ll_module), 'llvm')
        log.debug("full LLVM program is: <<<\n%s\n>>>" % highlighted)

        return self._module.get_callable(entry_point_name, entry_point_typesig)
예제 #8
0
파일: jit.py 프로젝트: lowks/ctree
    def __init__(self,
                 entry_point_name,
                 project,
                 entry_point_typesig,
                 extra_args=tuple()):
        assert isinstance(project, Project), \
            "Expected a Project but it got a %s." % type(project)
        assert project.parent is None, \
            "Expected null project.parent, but got: %s." % type(project.parent)

        VerifyOnlyCtreeNodes().visit(project)

        self.module = project.codegen()
        highlighted = highlight(str(self.module.ll_module), 'llvm')
        log.debug("full LLVM program is: <<<\n%s\n>>>" % highlighted)
        self.fn = self.module.get_callable(entry_point_name,
                                           entry_point_typesig)
        self._extra_args = extra_args
예제 #9
0
파일: util.py 프로젝트: pyprogrammer/ctree
    def _check_code(self, actual="", expected=""):
        if not isinstance(actual, str):
            actual = textwrap.dedent( str(actual) )
        if not isinstance(expected, str):
            expected = textwrap.dedent( str(expected) )

        actual   = textwrap.dedent(str(actual))
        expected = textwrap.dedent(str(expected))

        if actual != expected:
            actual_display = (actual + ("\n" if actual[-1] != "\n" else "")).splitlines(True)
            expected_display = expected.splitlines(True)
            diff_gen = difflib.unified_diff(
                actual_display, expected_display,
                "<actual>", "<expected>")
            diff = "".join(diff_gen)
            print(highlight(diff, language='diff'))

        self.assertEqual(actual, expected)
예제 #10
0
파일: util.py 프로젝트: ucb-sejits/ctree
    def _check_code(self, actual="", expected=""):
        if not isinstance(actual, str):
            actual = textwrap.dedent(str(actual))
        if not isinstance(expected, str):
            expected = textwrap.dedent(str(expected))

        actual = textwrap.dedent(str(actual))
        expected = textwrap.dedent(str(expected))

        if actual != expected:
            actual_display = (
                actual + ("\n" if actual[-1] != "\n" else "")).splitlines(True)
            expected_display = expected.splitlines(True)
            diff_gen = difflib.unified_diff(actual_display, expected_display,
                                            "<actual>", "<expected>")
            diff = "".join(diff_gen)
            print(highlight(diff, language='diff'))

        self.assertEqual(actual, expected)
예제 #11
0
]
LOG.info("checking for config files at: %s", CFG_PATHS)

LOG.info("found config files: %s", CONFIG.read(CFG_PATHS))

if sys.version_info.major == 2:
    from io import BytesIO as Memfile
else:
    from io import StringIO as Memfile

from ctree.util import highlight

CONFIGFILE = Memfile()
CONFIG.write(CONFIGFILE)
CONFIG_TXT = CONFIGFILE.getvalue()
LOG.info("using configuration:\n%s", highlight(CONFIG_TXT, language='ini'))
CONFIGFILE.close()
if CONFIG.has_option('log', 'level'):
    logging.basicConfig(level=getattr(logging, CONFIG.get('log', 'level')))

# ---------------------------------------------------------------------------
# stats

import atexit
import collections


class LogInfo(object):
    """Tracks events, reports counts upon garbage collections."""
    def __init__(self):
        self._counter = collections.Counter()
예제 #12
0
 def test_highlight_c(self):
     highlighted = highlight("int a = 0;", "c")
     assert len(highlighted) > 0
예제 #13
0
 def test_highlight_llvm(self):
     highlighted = highlight("%add.i.1 = fadd double %14, %mul.i.1", "llvm")
     assert len(highlighted) > 0
예제 #14
0
 def test_no_pygments(self):
     """ No pygments => no syntax highlighting. """
     with PreventImport('pygments'):
         code = "int a = 0;"
         highlighted = highlight(code, "c")
         self.assertEqual(code, highlighted)
예제 #15
0
 def test_bad_lang(self):
     with self.assertRaises(ValueError):
         highlight("int a = 0;", "not-a-language")
예제 #16
0
 def test_highlight_llvm(self):
     highlighted = highlight("%add.i.1 = fadd double %14, %mul.i.1", "llvm")
     assert len(highlighted) > 0
예제 #17
0
 def test_highlight_c(self):
     highlighted = highlight("int a = 0;", "c")
     assert len(highlighted) > 0
예제 #18
0
파일: __init__.py 프로젝트: i-Zaak/ctree
]
LOG.info("checking for config files at: %s", CFG_PATHS)

LOG.info("found config files: %s", CONFIG.read(CFG_PATHS))

if sys.version_info.major == 2:
    from io import BytesIO as Memfile
else:
    from io import StringIO as Memfile

from ctree.util import highlight

CONFIGFILE = Memfile()
CONFIG.write(CONFIGFILE)
CONFIG_TXT = CONFIGFILE.getvalue()
LOG.info("using configuration:\n%s", highlight(CONFIG_TXT, language='ini'))
CONFIGFILE.close()
if CONFIG.has_option('log','level'):
    logging.basicConfig(level=getattr(logging,CONFIG.get('log','level')))


# ---------------------------------------------------------------------------
# stats

import atexit
import collections


class LogInfo(object):
    """Tracks events, reports counts upon garbage collections."""
예제 #19
0
 def test_no_pygments(self):
     """ No pygments => no syntax highlighting. """
     with PreventImport('pygments'):
         code = "int a = 0;"
         highlighted = highlight(code, "c")
         self.assertEqual(code, highlighted)
예제 #20
0
 def test_bad_lang(self):
     with self.assertRaises(ValueError):
         highlight("int a = 0;", "not-a-language")