Ejemplo n.º 1
0
    def _run_llvm(self, filename, silence_output=True):
        file_name = self.path + filename
        ast = main.create_ast(file_name)
        if ast.semantic_analysis():
            code = ast.generate_llvm()
            result_file = self.result_path + filename[:-2] + ".ll"
            with open(result_file, "w+") as file:
                file.write(code)

            if silence_output:
                ret = subprocess.call(["clang", result_file],
                                      stderr=subprocess.DEVNULL,
                                      stdout=subprocess.DEVNULL)

            else:
                ret = subprocess.call(["clang", result_file])

            if ret == 1:
                main.generate_ast_visuals(ast,
                                          self.result_path + filename[:-2])

            # We cannot define function's at this stage of the project.
            # 1561 is the error thrown that we lack an entry point, witch is okay
            try:
                self.assertEqual(ret, 1561)

            except Exception:
                subprocess.call(["clang", "-S", "-emit-llvm", file_name])
Ejemplo n.º 2
0
    def _run_analysis(self, filename, errors=0, warnings=0):

        if not os.path.exists(self.result_path):
            os.makedirs(self.result_path)

        f = open(self.result_path + filename[:-2] + "_error.log", "w+")
        file_name = self.path + filename
        ast = main.create_ast(file_name, f)
        ast.semantic_analysis()
        f.close()
        try:
            self.assertEqual(ast.error_count(), errors)
            self.assertEqual(ast.warning_count(), warnings)
        except Exception:
            main.generate_ast_visuals(ast, self.result_path + filename[:-2])
            raise
Ejemplo n.º 3
0
    def _compile_llvm(self, filename):

        if not os.path.exists(self.result_path):
            os.makedirs(self.result_path)

        slug = filename[:-2]  # - .c
        file_name = self.path + filename
        ast = main.create_ast(file_name)
        if ast.semantic_analysis():
            code = ast.generate_llvm()
            result_file = self.result_path + filename[:-2] + ".ll"
            with open(result_file, "w+") as file:
                file.write(code)

            return True
        else:
            self.assertTrue(False, 'Semantics are wrong in an excecution test')
            return False
Ejemplo n.º 4
0
    def _build_and_run_mips(self, filename, exit_code):
        if not os.path.exists(self.result_path):
            os.makedirs(self.result_path)

        slug = filename[:-2]  # -c
        file_name = self.path + filename
        ast = main.create_ast(file_name)
        if ast.semantic_analysis():
            code = ast.generate_llvm()
            result_file = self.result_path + filename[:-2] + ".asm"
            with open(result_file, "w+") as file:
                file.write(code)

            main.generate_mips(ast, self.result_path + slug)
            code = subprocess.call([
                'java', '-jar', "Mars.jar", self.result_path + slug + ".asm",
                "nc"
            ])
            self.assertEqual(code, exit_code, "Wrong exit code")

        else:
            self.assertTrue(False, 'Semantics are wrong in an excecution test')
            return False
Ejemplo n.º 5
0
    if args.input_file is -1:
        print("Please specify input file")
        sys.exit(1)

    # Slug is useful for naming consistency of output files
    slug = os.path.basename(args.input_file)[:-2]

    # Setting up target directory
    path = "result/" + slug + "/"
    if not os.path.exists(path):
        os.makedirs(path)

    # AST generation
    # ==================================================================================================================
    # Creating AST
    ast = main.create_ast(args.input_file)

    if ast == -1:
        print("Syntax errors")
        sys.exit(1)

    if args.visual_ast:
        os.makedirs(path + "AST")
        main.generate_ast_visuals(ast, path + "AST/" + slug + "_pre_folding")

    ast.constant_folding()

    if args.visual_ast:
        main.generate_ast_visuals(ast, path + "AST/" + slug + "_pre_analysis")

    # If the semantic analysis fails