-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from cdm-processors/assembler-big-refactor
Refactor assembler
- Loading branch information
Showing
88 changed files
with
2,912 additions
and
2,453 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import assembler, linker, object_file, object_module |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
antlr4 -Dlanguage=Python3 -visitor -no-listener -Xexact-output-dir -o generated \ | ||
./grammar/AsmLexer.g4 ./grammar/AsmParser.g4 ./grammar/Macro.g4 ./grammar/ObjectFileParser.g4 ./grammar/ObjectFileLexer.g4 | ||
antlr4 -Dlanguage=Python3 -visitor -no-listener -Xexact-output-dir -o object_file/generated \ | ||
object_file/grammar/ObjectFileParser.g4 object_file/grammar/ObjectFileLexer.g4 | ||
antlr4 -Dlanguage=Python3 -visitor -no-listener -Xexact-output-dir -o assembler/generated \ | ||
assembler/grammar/AsmLexer.g4 assembler/grammar/AsmParser.g4 assembler/grammar/Macro.g4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""Assembler module. Converts source files into object module structures""" | ||
|
||
from .assembler import assemble_files, assemble_module | ||
from .exceptions import AssemblerException, AssemblerExceptionTag | ||
from .targets import list_assembler_targets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import codecs | ||
from itertools import chain | ||
from pathlib import Path | ||
from typing import Any, Optional | ||
|
||
import antlr4 | ||
|
||
from cocas.object_module import ObjectModule | ||
|
||
from .ast_builder import build_ast | ||
from .macro_processor import ExpandMacrosVisitor, process_macros, read_mlb | ||
from .object_generator import generate_object_module | ||
from .targets import TargetInstructions, import_target, mlb_path | ||
|
||
|
||
def assemble_module(input_stream: antlr4.InputStream, | ||
target_instructions: TargetInstructions, | ||
macros_library: ExpandMacrosVisitor, | ||
filepath: Path) -> ObjectModule: | ||
""" | ||
Convert lines of an assembler file to object code | ||
:param input_stream: contents of file | ||
:param target_instructions: information how to convert mnemonics to code segments | ||
:param macros_library: standard macros of assembler | ||
:param filepath: path of the file to use in error handling | ||
""" | ||
macro_expanded_input_stream = process_macros(input_stream, macros_library, filepath) | ||
r = build_ast(macro_expanded_input_stream, filepath) | ||
return generate_object_module(r, target_instructions) | ||
|
||
|
||
def get_debug_info_path(filepath: Path, | ||
debug: Optional[Any], | ||
relative_path: Optional[Path], | ||
realpath: bool) -> Optional[Path]: | ||
if debug: | ||
debug_info_path = filepath.expanduser().absolute() | ||
if realpath: | ||
debug_info_path = debug_info_path.resolve() | ||
if relative_path: | ||
debug_info_path = debug_info_path.relative_to(relative_path) | ||
else: | ||
debug_info_path = None | ||
return debug_info_path | ||
|
||
|
||
def assemble_files(target: str, | ||
files: list[Path], | ||
debug: bool, | ||
relative_path: Optional[Path], | ||
absolute_path: Optional[Path], | ||
realpath: bool) -> list[tuple[Path, ObjectModule]]: | ||
""" | ||
Open and assemble multiple files into object modules | ||
:param target: name of processor, should be valid | ||
:param files: list of assembler files' paths to process | ||
:param debug: if debug information should be collected | ||
:param relative_path: if debug paths should be relative to some path | ||
:param absolute_path: if relative paths should be converted to absolute | ||
:param realpath: if paths should be converted to canonical | ||
:return: list of pairs [source file path, object module] | ||
""" | ||
_ = absolute_path | ||
target_instructions = import_target(target) | ||
macros_library = read_mlb(mlb_path(target)) | ||
objects = [] | ||
|
||
for filepath in files: | ||
with filepath.open('rb') as file: | ||
data = file.read() | ||
data = codecs.decode(data, 'utf8', 'strict') | ||
if not data.endswith('\n'): | ||
data += '\n' | ||
input_stream = antlr4.InputStream(data) | ||
obj = assemble_module(input_stream, target_instructions, macros_library, filepath) | ||
|
||
debug_info_path = get_debug_info_path(filepath, debug, relative_path, realpath) | ||
if debug_info_path: | ||
obj.source_file_path = debug_info_path | ||
fp = filepath.absolute().as_posix() | ||
dip = debug_info_path.as_posix() | ||
for i in chain(obj.asects, obj.rsects): | ||
for j in i.code_locations.values(): | ||
if j.file == fp: | ||
j.file = dip | ||
else: | ||
j.file = get_debug_info_path(Path(j.file), debug, relative_path, realpath) | ||
objects.append((filepath, obj)) | ||
return objects |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.