/ test / external / riscof / spike_simple / riscof_spike_simple.py
riscof_spike_simple.py
 1  import os
 2  import re
 3  import shutil
 4  import subprocess
 5  import shlex
 6  import logging
 7  import random
 8  import string
 9  from string import Template
10  import sys
11  
12  import riscof.utils as utils
13  from riscof.pluginTemplate import pluginTemplate
14  import riscof.constants as constants
15  
16  logger = logging.getLogger()
17  
18  class spike_simple(pluginTemplate):
19      __model__ = "Spike"
20      __version__ = "0.5.0"
21  
22      def __init__(self, *args, **kwargs):
23          sclass = super().__init__(*args, **kwargs)
24  
25          config = kwargs.get('config')
26          self.spike_exe = os.path.join(config['PATH'] if 'PATH' in config else "","spike")
27          if config is None:
28              print("Please enter input file paths in configuration.")
29              raise SystemExit
30          try:
31              self.isa_spec = os.path.abspath(config['ispec'])
32              self.platform_spec = os.path.abspath(config['pspec'])
33              self.pluginpath = os.path.abspath(config['pluginpath'])
34          except KeyError as e:
35              logger.error("Please check the spike_simple section in config for missing values.")
36              logger.error(e)
37              raise SystemExit
38          logger.debug("SPIKE Simple plugin initialised using the following configuration.")
39          for entry in config:
40              logger.debug(entry+' : '+config[entry])
41  
42          self.compare_run = not ("compare_run" in config and config["compare_run"] == "0")
43  
44          return sclass
45  
46      def initialise(self, suite, work_dir, compliance_env):
47          self.work_dir = work_dir
48          self.compile_cmd = 'riscv64-unknown-elf-gcc -march={0} \
49           -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles\
50           -T '+self.pluginpath+'/env/link.ld\
51           -I '+self.pluginpath+'/env/\
52           -I ' + compliance_env
53  
54      def build(self, isa_yaml, platform_yaml):
55          ispec = utils.load_yaml(isa_yaml)['hart0']
56          self.xlen = ('64' if 64 in ispec['supported_xlen'] else '32')
57          if "64I" in ispec["ISA"]:
58              self.compile_cmd = self.compile_cmd+' -mabi='+'lp64 '
59          elif "32I" in ispec["ISA"]:
60              self.compile_cmd = self.compile_cmd+' -mabi='+'ilp32 '
61          elif "32E" in ispec["ISA"]:
62              self.compile_cmd = self.compile_cmd+' -mabi='+'ilp32e '
63          self.isa = ispec["ISA"].lower()
64  
65          compiler = "riscv64-unknown-elf-gcc".format(self.xlen)
66          if shutil.which(compiler) is None:
67              logger.error(compiler+": executable not found. Please check environment setup.")
68              raise SystemExit
69          if shutil.which(self.spike_exe) is None:
70              logger.error(self.spike_exe+ ": executable not found. Please check environment setup.")
71              raise SystemExit
72  
73      def runTests(self, testList):
74          for file in testList:
75              testentry = testList[file]
76              test = testentry['test_path']
77              test_dir = testentry['work_dir']
78  
79              elf = 'my.elf'
80              sig_file = os.path.join(test_dir, self.name[:-1] + ".signature")
81  
82              cmd = self.compile_cmd.format(testentry['isa'].lower(), self.xlen) + ' ' + test + ' -o ' + elf
83              compile_cmd = cmd + ' -D' + " -D".join(testentry['macros'])
84              logger.debug('Compiling test: ' + test)
85              utils.shellCommand(compile_cmd).run(cwd=test_dir)
86  
87              execute = self.spike_exe + ' --isa={0} +signature={1} +signature-granularity=4 {2}'.format(self.isa, sig_file, elf)
88              logger.debug('Executing on Spike ' + execute)
89              utils.shellCommand(execute).run(cwd=test_dir)
90  
91          if not self.compare_run:
92              # exit now if we don't want to run compare of signatures
93              raise SystemExit(0)