Skip to content

Mod dis #512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/snippets/dismod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import dis

dis.disassemble(compile("5 + x + 5 or 2", "", "eval"))
print("\n")
dis.disassemble(compile("def f(x):\n return 1", "", "exec"))
print("\n")
dis.disassemble(compile("if a:\n 1 or 2\nelif x == 'hello':\n 3\nelse:\n 4", "", "exec"))
print("\n")
dis.disassemble(compile("f(x=1, y=2)", "", "eval"))
print("\n")
130 changes: 119 additions & 11 deletions vm/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use num_bigint::BigInt;
use num_complex::Complex64;
use rustpython_parser::ast;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::fmt;

/// Primary container of a single code object. Each python function has
Expand Down Expand Up @@ -173,6 +173,8 @@ pub enum Instruction {
},
}

use self::Instruction::*;

#[derive(Debug, Clone, PartialEq)]
pub enum CallType {
Positional(usize),
Expand Down Expand Up @@ -277,17 +279,123 @@ impl CodeObject {
}
}

impl fmt::Display for CodeObject {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let label_targets: HashSet<&usize> = self.label_map.values().collect();
for (offset, instruction) in self.instructions.iter().enumerate() {
let arrow = if label_targets.contains(&offset) {
">>"
} else {
" "
};
write!(f, " {} {:5} ", arrow, offset)?;
instruction.fmt_dis(f, &self.label_map)?;
}
Ok(())
}
}

impl Instruction {
fn fmt_dis(&self, f: &mut fmt::Formatter, label_map: &HashMap<Label, usize>) -> fmt::Result {
macro_rules! w {
($variant:ident) => {
write!(f, "{:20}\n", stringify!($variant))
};
($variant:ident, $var:expr) => {
write!(f, "{:20} ({})\n", stringify!($variant), $var)
};
($variant:ident, $var1:expr, $var2:expr) => {
write!(f, "{:20} ({}, {})\n", stringify!($variant), $var1, $var2)
};
}

match self {
Import { name, symbol } => w!(Import, name, format!("{:?}", symbol)),
ImportStar { name } => w!(ImportStar, name),
LoadName { name } => w!(LoadName, name),
StoreName { name } => w!(StoreName, name),
DeleteName { name } => w!(DeleteName, name),
StoreSubscript => w!(StoreSubscript),
DeleteSubscript => w!(DeleteSubscript),
StoreAttr { name } => w!(StoreAttr, name),
DeleteAttr { name } => w!(DeleteAttr, name),
LoadConst { value } => w!(LoadConst, value),
UnaryOperation { op } => w!(UnaryOperation, format!("{:?}", op)),
BinaryOperation { op, inplace } => w!(BinaryOperation, format!("{:?}", op), inplace),
LoadAttr { name } => w!(LoadAttr, name),
CompareOperation { op } => w!(CompareOperation, format!("{:?}", op)),
Pop => w!(Pop),
Rotate { amount } => w!(Rotate, amount),
Duplicate => w!(Duplicate),
GetIter => w!(GetIter),
Pass => w!(Pass),
Continue => w!(Continue),
Break => w!(Break),
Jump { target } => w!(Jump, label_map[target]),
JumpIf { target } => w!(JumpIf, label_map[target]),
JumpIfFalse { target } => w!(JumpIfFalse, label_map[target]),
MakeFunction { flags } => w!(MakeFunction, format!("{:?}", flags)),
CallFunction { typ } => w!(CallFunction, format!("{:?}", typ)),
ForIter { target } => w!(ForIter, label_map[target]),
ReturnValue => w!(ReturnValue),
YieldValue => w!(YieldValue),
YieldFrom => w!(YieldFrom),
SetupLoop { start, end } => w!(SetupLoop, label_map[start], label_map[end]),
SetupExcept { handler } => w!(SetupExcept, handler),
SetupWith { end } => w!(SetupWith, end),
CleanupWith { end } => w!(CleanupWith, end),
PopBlock => w!(PopBlock),
Raise { argc } => w!(Raise, argc),
BuildString { size } => w!(BuildString, size),
BuildTuple { size, unpack } => w!(BuildTuple, size, unpack),
BuildList { size, unpack } => w!(BuildList, size, unpack),
BuildSet { size, unpack } => w!(BuildSet, size, unpack),
BuildMap { size, unpack } => w!(BuildMap, size, unpack),
BuildSlice { size } => w!(BuildSlice, size),
ListAppend { i } => w!(ListAppend, i),
SetAdd { i } => w!(SetAdd, i),
MapAdd { i } => w!(MapAdd, i),
PrintExpr => w!(PrintExpr),
LoadBuildClass => w!(LoadBuildClass),
StoreLocals => w!(StoreLocals),
UnpackSequence { size } => w!(UnpackSequence, size),
UnpackEx { before, after } => w!(UnpackEx, before, after),
Unpack => w!(Unpack),
FormatValue { spec } => w!(FormatValue, spec),
}
}
}

impl fmt::Display for Constant {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Constant::Integer { value } => write!(f, "{}", value),
Constant::Float { value } => write!(f, "{}", value),
Constant::Complex { value } => write!(f, "{}", value),
Constant::Boolean { value } => write!(f, "{}", value),
Constant::String { value } => write!(f, "{:?}", value),
Constant::Bytes { value } => write!(f, "{:?}", value),
Constant::Code { code } => write!(f, "{:?}", code),
Constant::Tuple { elements } => write!(
f,
"({})",
elements
.iter()
.map(|e| format!("{}", e))
.collect::<Vec<_>>()
.join(", ")
),
Constant::None => write!(f, "None"),
}
}
}

impl fmt::Debug for CodeObject {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let inst_str = self
.instructions
.iter()
.zip(self.locations.iter())
.enumerate()
.map(|(i, inst)| format!("Inst {}: {:?}", i, inst))
.collect::<Vec<_>>()
.join("\n");
let labelmap_str = format!("label_map: {:?}", self.label_map);
write!(f, "Code Object {{ \n{}\n{} }}", inst_str, labelmap_str)
write!(
f,
"<code object {} at ??? file {:?}, line {}>",
self.obj_name, self.source_path, self.first_line_number
)
}
}
18 changes: 18 additions & 0 deletions vm/src/stdlib/dis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::super::obj::objcode;
use super::super::obj::objtype;
use super::super::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
use super::super::vm::VirtualMachine;

fn dis_disassemble(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(co, Some(vm.ctx.code_type()))]);

let code = objcode::get_value(co);
print!("{}", code);
Ok(vm.get_none())
}

pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let py_mod = ctx.new_module("dis", ctx.new_scope(None));
ctx.set_attr(&py_mod, "disassemble", ctx.new_rustfunc(dis_disassemble));
py_mod
}
2 changes: 2 additions & 0 deletions vm/src/stdlib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod ast;
mod dis;
pub mod io;
mod json;
mod keyword;
Expand All @@ -21,6 +22,7 @@ pub type StdlibInitFunc = fn(&PyContext) -> PyObjectRef;
pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
let mut modules = HashMap::new();
modules.insert("ast".to_string(), ast::mk_module as StdlibInitFunc);
modules.insert("dis".to_string(), dis::mk_module as StdlibInitFunc);
modules.insert("io".to_string(), io::mk_module as StdlibInitFunc);
modules.insert("json".to_string(), json::mk_module as StdlibInitFunc);
modules.insert("keyword".to_string(), keyword::mk_module as StdlibInitFunc);
Expand Down