Skip to content

Commit

Permalink
[cocas] Move writing image and debug information to linker module
Browse files Browse the repository at this point in the history
  • Loading branch information
cjvth committed Jan 30, 2024
1 parent ea16c44 commit e6c9661
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions cocas/linker/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .debug_export import debug_export, write_debug_export
from .image import write_image
from .linker import link
21 changes: 17 additions & 4 deletions cocas/debug_export.py → cocas/linker/debug_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,28 @@ def code_location_json(files, cl: CodeLocation):


def debug_export(code_locations: dict[int, CodeLocation]) -> str:
"""
Convert debug information objects to json format
:param code_locations: mapping from address in binary image to location in source code
:return: string with json representation of debug information, code locations are sorted
"""
files = sorted(set(map(lambda x: x.file, code_locations.values())))
dump = json.dumps({"files": files, "codeLocations": code_locations},
sorted_cl = {key: value for (key, value) in sorted(code_locations.items())}
dump = json.dumps({"files": files, "codeLocations": sorted_cl},
default=default_json(files), indent=4, ensure_ascii=False)
pattern = re.compile(r"{\n\s+\"__no_breaks_begin\": \[],\n\s+([\S\s]+?),\n\s+\"__no_breaks_end\": \[]\s+}")
dump = re.sub(pattern, lambda m: "{" + re.sub(r"\n\s+", " ", m.group(1)) + "}", dump)
return dump


def write_debug_export(filepath: Union[Path, str], sorted_cl: dict[int, CodeLocation]):
sorted_cl = {key: value for (key, value) in sorted(sorted_cl.items())}
def write_debug_export(filepath: Union[Path, str], code_locations: dict[int, CodeLocation]):
"""
Convert debug information objects to json format and write to file
:param filepath: path to file to write
:param code_locations: mapping from address in binary image to location in source code
:return: json file with debug information, code locations are sorted
"""
with open(filepath, 'w') as f:
f.write(debug_export(sorted_cl))
f.write(debug_export(code_locations))
File renamed without changes.
4 changes: 1 addition & 3 deletions cocas/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import colorama

from cocas.assembler import assemble_files, list_assembler_targets
from cocas.debug_export import write_debug_export
from cocas.error import CdmException, log_error
from cocas.image import write_image
from cocas.linker import link
from cocas.linker import link, write_debug_export, write_image
from cocas.object_file import list_object_targets, read_object_files, write_object_file


Expand Down

0 comments on commit e6c9661

Please sign in to comment.