86 lines
3.1 KiB
Python
86 lines
3.1 KiB
Python
from collections import defaultdict
|
|
|
|
from ScEpTIC.AST.transformations.virtual_memory.parsers.ast_parser import ASTParser
|
|
from ScEpTIC.AST.transformations.virtual_memory.parsers.memory_tags_parser import MemoryTagsParser
|
|
from ScEpTIC.AST.elements.instructions.memory_operations import LoadOperation, StoreOperation
|
|
from ScEpTIC.AST.elements.instructions.other_operations import CallOperation
|
|
|
|
def apply_transformation(functions, vmstate, f_declarations):
|
|
"""
|
|
:param functions: ScEpTIC AST functions
|
|
:param vmstate: vmstate
|
|
"""
|
|
config = vmstate.config.ast_transformations.static_dvfs
|
|
|
|
parser = ASTParser(functions)
|
|
parser.parse(False)
|
|
|
|
mem_parser = MemoryTagsParser(parser)
|
|
mem_parser.resolve_memory_tags()
|
|
|
|
functions_dependencies = {}
|
|
# traccia variabili globali in ogni funzione
|
|
# global_vars[function_name] = {'loads': [], 'stores': []}
|
|
# pre-processing dove riempi global_vars[function_name]
|
|
# for function in functions:
|
|
# for instruction in function.body
|
|
|
|
for function_name in parser.get_function_names():
|
|
print(function_name)
|
|
print(f"Function: {function_name}")
|
|
|
|
functions_dependencies[function_name] = {}
|
|
|
|
for bb_id, basic_block in parser.get_ast(function_name).items():
|
|
dependencies = defaultdict(list)
|
|
definitions = {}
|
|
loads = defaultdict(list)
|
|
stores = defaultdict(list)
|
|
|
|
print(f" BasicBlock: {bb_id}")
|
|
|
|
for instr_id, instruction in enumerate(basic_block.instructions):
|
|
memory_tag = instruction.memory_tag
|
|
print(f" {instr_id}: {instruction}")
|
|
|
|
if isinstance(instruction, LoadOperation):
|
|
if memory_tag[0] == '@':
|
|
for store in stores[memory_tag]:
|
|
dependencies[store].append(instr_id)
|
|
|
|
loads[memory_tag].append(instr_id)
|
|
|
|
if isinstance(instruction, StoreOperation):
|
|
if memory_tag[0] == '@':
|
|
for store in stores[memory_tag]:
|
|
dependencies[store].append(instr_id)
|
|
|
|
for load in loads[memory_tag]:
|
|
dependencies[load].append(instr_id)
|
|
|
|
stores[memory_tag].append(instr_id)
|
|
|
|
if isinstance(instruction, CallOperation):
|
|
# fill me
|
|
pass
|
|
|
|
for reg in instruction.get_defs():
|
|
definitions[reg] = instr_id
|
|
|
|
for reg in instruction.get_uses():
|
|
if reg not in definitions:
|
|
print(f"[Debug] {reg} not defined")
|
|
continue
|
|
|
|
def_instr = definitions[reg]
|
|
dependencies[def_instr].append(instr_id)
|
|
|
|
dependency_list = []
|
|
|
|
for id_def, id_uses in dependencies.items():
|
|
for id_use in id_uses:
|
|
dependency_list.append([id_def, id_use])
|
|
|
|
functions_dependencies[function_name][bb_id] = dependency_list
|
|
print(dependency_list)
|