class VM: def __init__(self): self.assemblies = [] self.methods = [] self.currentMethod = None self.stack = Stack(8) self.hooks = [None, None, None, None] self.instructionPointer = 0 self.protected_blocks = [] def start(self): self.add_builtins() md = self.find_method_by_signature(None, "Main", None, None) method = md.get_method() self.execute_method(method) pass def add_builtins(self): m = MethodDefinition() m.instructions.append(Ret()) m.name = "ctor" m.namespace = "[mscorlib]System.Object" m.returnType = Types.Void self.methods.append(m) def load(self, fileName): f = open(fileName, "r") s = f.read() p = ParserContext() p.parse(s) self.methods = p.methods def find_instruction_pointer_by_label(self, label): for i in range(len(self.current_method().instructions)): instruction = self.current_method().instructions[i] if instruction.label == label: return i return -1 # fixme - check for instance/static def find_method_by_signature(self, namespace, name, returnType, params): for m in self.methods: # fixme - namexpaces not parsed if namespace != None and m.namespace != namespace: continue if returnType != None and m.returnType != returnType: continue if name != None and m.name != name: continue if params == None: return m # fixme - shoudl always do checks if len(m.parameters) == len(params): equal = True for i in range(len(params)): # support both variables and types methodParameterType = m.parameters[i] if isinstance(m.parameters[i], Variable): methodParameterType = m.parameters[i].type parameterType = params[i] if isinstance(params[i], Variable): parameterType = params[i].type if ( parameterType != methodParameterType and parameterType.arrayType != methodParameterType.arrayType ): equal = False break if equal: return m return None # raise Exception("method not found: " + name) def current_method(self): return self.current_stack_frame().method def current_stack_frame(self): return self.stack.currentFrame def set_current_method(self, method): self.stack.beginFrame(method.maxStack, method) self.currentMethod = method def execute_method(self, method): self.set_current_method(method) if self.hooks[DebugHooks.PreMethod] is not None: self.hooks[DebugHooks.PreMethod](method) frame = self.current_stack_frame() frame.instructionPointer = 0 while frame == self.current_stack_frame() and frame.instructionPointer < len(method.instructions): instruction = method.instructions[self.current_stack_frame().instructionPointer] self.current_stack_frame().instructionPointer += 1 if self.hooks[DebugHooks.PreInstruction] is not None: self.hooks[DebugHooks.PreInstruction](instruction) instruction.execute(self) if self.hooks[DebugHooks.PostInstruction] is not None: self.hooks[DebugHooks.PostInstruction](instruction) if self.hooks[DebugHooks.PostMethod] is not None: self.hooks[DebugHooks.PostMethod](method) def add_hook(self, hookType, method): self.hooks[hookType] = method def remove_hook(self, hookType, method): self.hooks[hookType] = None def get_instruction_pointer(self): return self.current_stack_frame().instructionPointer def get_protected_blocks(self): return self.protected_blocks
class VM: def __init__(self): self.assemblies = [] self.methods = [] self.currentMethod = None self.stack = Stack(8) self.hooks = [None, None, None, None] self.instructionPointer = 0 self.protected_blocks = [] def start(self): self.add_builtins() md = self.find_method_by_signature(None, 'Main', None, None) method = md.get_method() self.execute_method(method) pass def add_builtins(self): m = MethodDefinition() m.instructions.append(Ret()) m.name = 'ctor' m.namespace = '[mscorlib]System.Object' m.returnType = Types.Void self.methods.append(m) def load(self, fileName): f = open(fileName, 'r') s = f.read() p = ParserContext() p.parse(s) self.methods = p.methods def find_instruction_pointer_by_label(self, label): for i in range(len(self.current_method().instructions)): instruction = self.current_method().instructions[i] if instruction.label == label: return i return -1 #fixme - check for instance/static def find_method_by_signature(self, namespace, name, returnType, params): for m in self.methods: # fixme - namexpaces not parsed if namespace != None and m.namespace != namespace: continue if returnType != None and m.returnType != returnType: continue if name != None and m.name != name: continue if params == None: return m # fixme - shoudl always do checks if len(m.parameters) == len(params): equal = True for i in range(len(params)): # support both variables and types methodParameterType = m.parameters[i] if isinstance(m.parameters[i], Variable): methodParameterType = m.parameters[i].type parameterType = params[i] if isinstance(params[i], Variable): parameterType = params[i].type if parameterType != methodParameterType and parameterType.arrayType != methodParameterType.arrayType: equal = False break if equal: return m return None #raise Exception("method not found: " + name) def current_method(self): return self.current_stack_frame().method def current_stack_frame(self): return self.stack.currentFrame; def set_current_method(self, method): self.stack.beginFrame(method.maxStack, method) self.currentMethod = method def execute_method(self, method): self.set_current_method(method) if self.hooks[DebugHooks.PreMethod] is not None: self.hooks[DebugHooks.PreMethod](method) frame = self.current_stack_frame() frame.instructionPointer = 0 while frame == self.current_stack_frame() and frame.instructionPointer < len(method.instructions): instruction = method.instructions[self.current_stack_frame().instructionPointer] self.current_stack_frame().instructionPointer += 1 if self.hooks[DebugHooks.PreInstruction] is not None: self.hooks[DebugHooks.PreInstruction](instruction) instruction.execute(self) if self.hooks[DebugHooks.PostInstruction] is not None: self.hooks[DebugHooks.PostInstruction](instruction) if self.hooks[DebugHooks.PostMethod] is not None: self.hooks[DebugHooks.PostMethod](method) def add_hook(self, hookType, method): self.hooks[hookType] = method def remove_hook(self, hookType, method): self.hooks[hookType] = None def get_instruction_pointer(self): return self.current_stack_frame().instructionPointer def get_protected_blocks(self): return self.protected_blocks