Skip to content

Commit

Permalink
Implemented sources compilation method
Browse files Browse the repository at this point in the history
  • Loading branch information
NikosDelijohn committed Aug 27, 2024
1 parent 2714bbe commit f75472b
Showing 1 changed file with 49 additions and 30 deletions.
79 changes: 49 additions & 30 deletions src/zoix.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import sys
import asm
import subprocess
import logging
import re
import subprocess
import logging
import re
import enum

log = logging.getLogger("testcrush logger")
Expand Down Expand Up @@ -39,96 +39,115 @@ def __init__(self, fsim_threads : int) -> 'ZoixInvoker':

self.coverage : float = 0.0
self.fsim_threads : int = fsim_threads

@staticmethod
def execute(instruction : str, timeout : float = None) -> tuple[str, str]:
"""Executes a bash command-string instruction and returns
the `stdout` and `stderr` responses as a tuple.
- Parameters:
- instruction (str): The bash instruction to be executed.
- Returns:
- tuple(str, str): The stdout (index 0) and the stderr (index 1) string."""
- tuple(str, str): The stdout (index 0) and the stderr (index 1) as strings."""

log.debug(f"Executing {instruction}...")
try:
with subprocess.Popen(
["/bin/bash", "-c", instruction],
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
stderr = subprocess.PIPE,
text = True) as process:

stdout, stderr = process.communicate(timeout = timeout)

return stdout, stderr

except subprocess.TimeoutExpired:

log.debug(f"TIMEOUT during the execution of:\n\t{instruction}.")
process.kill()
return "TimeoutExpired", "TimeoutExpired"

def compile_sources(self, *instructions : str, **kwargs) -> Compilation:
... #TODO
def compile_sources(self, *instructions : str) -> Compilation:
"""Takes a number of bash instructions to perform the compilation of
an assembly file using VCS.
- Parameters:
- None
Returns:
- Compilation: A status Enum to signify the success or failure
of the compilation.
"""

compilation_status = Compilation.SUCCESS

for cmd in instructions:

stdout, stderr = self.execute(cmd)

if stderr:

compilation_status = Compilation.ZOIX_ERROR
break

return compilation_status

def logic_simulate(self, *instructions : str, **kwargs) -> LogicSimulation:
"""Takes a number of bash instructions which are expected to be either
bash scripts, or build instructions (e.g., make) to perform a
logic simulation of an assembly file using VCS in shell mode
- Parameters:
bash scripts, or build instructions (e.g., make) to perform a
logic simulation of an assembly file using VCS (simv) in shell mode
- Parameters:
- *instructions (str): A series of bash shell instructions
- **kwargs: User-defined options needed for the
evaluation of the result of the logic simulation. These are:
- timeout (float): A global timeout to be used for **each**
of the executed lsim instruction.
- timeout (float): A global timeout in **seconds** to be
used for **each** of the executed lsim instruction.
- success_regexp (re.regexp): A regular expression which is
used for matching in every line of the `stdout` stream to
signify the sucessfull completion of the logic simulation.
- Returns:
- LogicSimulation (enum):
- Returns:
- LogicSimulation (enum):
- TIMEOUT: if user defined timeout has been triggered.
- SIM_ERROR: if any text was found in the `stderr` stream
during the execution of an instruction.
- SUCCESS: if the halting regexp matched text from the
- SUCCESS: if the halting regexp matched text from the
`stdout` stream."""

timeout : float = kwargs.get("timeout", None)

# The default regexp catches the line:
# $finish at simulation time XXXXXXYs
# where X = a digit and Y = time unit.
# Capturing of the simulation duration
# done for possible TaT purposes. TODO
success : re.regexp = kwargs.get("success_regexp",
success : re.regexp = kwargs.get("success_regexp",
re.compile(r"\$finish[^0-9]+([0-9]+)[m|u|n|p]s", re.DOTALL))

# TODO: Handle all OK/NO_OK scenarios for compilation statuses.

simulation_status = None

for cmd in instructions:

stdout, stderr = self.execute(cmd, timeout = timeout)

if stderr and stderr != "TimeoutExpired":

log.debug(f"Error during execution of {cmd}\n\
---------[MESSAGE]---------\n\
{'-'.join(stderr.splitlines(keepends=True))}\n\
---------------------------\n")

simulation_status = LogicSimulation.SIM_ERROR
break
simulation_status = LogicSimulation.SIM_ERROR
break

elif stderr == stdout == "TimeoutExpired":
simulation_status = LogicSimulation.TIMEOUT
break


for line in stdout.splitlines():
log.debug(f"{cmd}: {line.rstrip()}")

if re.match(success, line):
log.debug(f"SIMULATION SUCCESSFULL: {re.match(success, line).groups()}")
simulation_status = LogicSimulation.SUCCESS
Expand All @@ -137,7 +156,7 @@ def logic_simulate(self, *instructions : str, **kwargs) -> LogicSimulation:
if not simulation_status:
raise LogicSimulationException(f"Simulation status was not set during\
the execution of {instructions}. Check the debug log for more information!")

return simulation_status

def main():
Expand All @@ -148,4 +167,4 @@ def main():
print(res)
if __name__ == "__main__":

main()
main()

0 comments on commit f75472b

Please sign in to comment.