Bladeren bron

Improve bytecode-interpreter debug info accuracy

jonathanvdc 8 jaren geleden
bovenliggende
commit
074a6bdbf1
2 gewijzigde bestanden met toevoegingen van 26 en 3 verwijderingen
  1. 13 3
      kernel/modelverse_jit/bytecode_interpreter.py
  2. 13 0
      kernel/modelverse_jit/source_map.py

+ 13 - 3
kernel/modelverse_jit/bytecode_interpreter.py

@@ -2,6 +2,7 @@
 
 import modelverse_jit.bytecode_ir as bytecode_ir
 import modelverse_jit.runtime as jit_runtime
+import modelverse_jit.source_map as source_map
 import modelverse_kernel.primitives as primitive_functions
 
 class BreakException(Exception):
@@ -22,10 +23,11 @@ class ContinueException(Exception):
 
 class InterpreterState(object):
     """The state of the bytecode interpreter."""
-    def __init__(self, gc_root_node, keyword_arg_dict, nop_period=10):
+    def __init__(self, gc_root_node, keyword_arg_dict, src_map, nop_period=10):
         self.gc_root_node = gc_root_node
         self.nop_period = nop_period
         self.keyword_arg_dict = keyword_arg_dict
+        self.src_map = src_map
         self.current_result = None
         self.nop_phase = 0
         self.local_vars = {}
@@ -66,10 +68,17 @@ class InterpreterState(object):
         """Interprets the given instruction and returns the current result."""
         instruction_type = type(instruction)
         if instruction_type in InterpreterState.INTERPRETERS:
+            old_debug_info = self.src_map.debug_information
+            debug_info = instruction.debug_information
+            if debug_info is not None:
+                self.src_map.debug_information = debug_info
+
             # Interpret the instruction.
             yield [("CALL_ARGS",
                     [InterpreterState.INTERPRETERS[instruction_type], (self, instruction)])]
 
+            self.src_map.debug_information = old_debug_info
+
             # Maybe perform a nop.
             if self.schedule_nop():
                 yield None
@@ -279,11 +288,12 @@ class InterpreterState(object):
 def interpret_bytecode_function(function_name, body_bytecode, local_arguments, keyword_arguments):
     """Interprets the bytecode function with the given name, body, named arguments and
        keyword arguments."""
-    yield [("DEBUG_INFO", [function_name, None, jit_runtime.BYTECODE_INTERPRETER_ORIGIN_NAME])]
+    src_map = source_map.ManualSourceMap()
+    yield [("DEBUG_INFO", [function_name, src_map, jit_runtime.BYTECODE_INTERPRETER_ORIGIN_NAME])]
     task_root = keyword_arguments['task_root']
     gc_root_node, = yield [("CN", [])]
     gc_root_edge, = yield [("CE", [task_root, gc_root_node])]
-    interpreter = InterpreterState(gc_root_node, keyword_arguments)
+    interpreter = InterpreterState(gc_root_node, keyword_arguments, src_map)
     for param_id, arg_node in local_arguments.items():
         yield [("CALL_ARGS", [interpreter.import_local, (param_id, arg_node)])]
 

+ 13 - 0
kernel/modelverse_jit/source_map.py

@@ -50,3 +50,16 @@ class SourceMapBuilder(object):
     def __str__(self):
         return str(self.source_map)
 
+class ManualSourceMap(object):
+    """A source map whose debug information can be set and reset. Line numbers are ignored."""
+    def __init__(self):
+        self.debug_information = None
+
+    def get_debug_info(self, _):
+        """Gets the debug information for the given line number, or None if no debug info was
+           found."""
+        return self.debug_information
+
+    def __str__(self):
+        return str(self.debug_information)
+