-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Opcode metadata auto generation #6174
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
21d9632
f6ab8d2
b8ebe9b
8510815
bcea0bf
c7ffa2a
f1948c6
84c7f45
9adf908
99ed360
56c0dc8
3fdc479
5cf1888
eaee21e
acd74c7
f71ad1b
b860f65
ff4fb09
493da32
657660f
eacab03
7b36086
dd761d0
aeedbdc
33a90f3
4a11f38
029e534
11369f8
a5813ea
6e6accd
f8a887a
a2014b5
6c48b49
46f3e29
742b709
2a8fb1e
465528e
e437232
1ae0c7e
28bab70
fe3f94a
aca4508
3daec5a
9436354
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
OpcodeId in _opcode
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,36 +5,10 @@ mod opcode { | |||||||||
| use crate::vm::{ | ||||||||||
| AsObject, PyObjectRef, PyResult, VirtualMachine, | ||||||||||
| builtins::{PyBool, PyInt, PyIntRef, PyNone}, | ||||||||||
| instruction::Instruction, | ||||||||||
| bytecode::Instruction, | ||||||||||
| match_class, | ||||||||||
| opcode::OpcodeId, | ||||||||||
| }; | ||||||||||
| use std::ops::Deref; | ||||||||||
|
|
||||||||||
| struct Opcode(Instruction); | ||||||||||
|
|
||||||||||
| impl Deref for Opcode { | ||||||||||
| type Target = Instruction; | ||||||||||
|
|
||||||||||
| fn deref(&self) -> &Self::Target { | ||||||||||
| &self.0 | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| impl Opcode { | ||||||||||
| #[must_use] | ||||||||||
| fn try_from_pyint(raw: PyIntRef, vm: &VirtualMachine) -> PyResult<Self> { | ||||||||||
| let instruction = raw | ||||||||||
| .try_to_primitive::<u16>(vm) | ||||||||||
| .and_then(|v| { | ||||||||||
| Instruction::try_from(v).map_err(|_| { | ||||||||||
| vm.new_exception_empty(vm.ctx.exceptions.value_error.to_owned()) | ||||||||||
| }) | ||||||||||
| }) | ||||||||||
| .map_err(|_| vm.new_value_error("invalid opcode or oparg"))?; | ||||||||||
|
|
||||||||||
| Ok(Self(instruction)) | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| #[pyattr] | ||||||||||
| const ENABLE_SPECIALIZATION: u8 = 1; | ||||||||||
|
|
@@ -49,82 +23,87 @@ mod opcode { | |||||||||
| jump: Option<PyObjectRef>, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /* | ||||||||||
| #[pyfunction] | ||||||||||
| fn stack_effect(args: StackEffectArgs, vm: &VirtualMachine) -> PyResult<i32> { | ||||||||||
| let oparg = args | ||||||||||
| .oparg | ||||||||||
| .map(|v| { | ||||||||||
| if !v.fast_isinstance(vm.ctx.types.int_type) { | ||||||||||
| return Err(vm.new_type_error(format!( | ||||||||||
| "'{}' object cannot be interpreted as an integer", | ||||||||||
| v.class().name() | ||||||||||
| ))); | ||||||||||
| // TODO: Make use of the auto generated `Instruction` enum (when we will have it) | ||||||||||
| #[pyfunction] | ||||||||||
| fn stack_effect(args: StackEffectArgs, vm: &VirtualMachine) -> PyResult<i32> { | ||||||||||
| let oparg = args | ||||||||||
| .oparg | ||||||||||
| .map(|v| { | ||||||||||
| if !v.fast_isinstance(vm.ctx.types.int_type) { | ||||||||||
| return Err(vm.new_type_error(format!( | ||||||||||
| "'{}' object cannot be interpreted as an integer", | ||||||||||
| v.class().name() | ||||||||||
| ))); | ||||||||||
| } | ||||||||||
| v.downcast_ref::<PyInt>() | ||||||||||
| .ok_or_else(|| vm.new_type_error(""))? | ||||||||||
| .try_to_primitive::<u32>(vm) | ||||||||||
| }) | ||||||||||
| .unwrap_or(Ok(0))?; | ||||||||||
|
|
||||||||||
| let jump = args | ||||||||||
| .jump | ||||||||||
| .map(|v| { | ||||||||||
| match_class!(match v { | ||||||||||
| b @ PyBool => Ok(b.is(&vm.ctx.true_value)), | ||||||||||
| _n @ PyNone => Ok(false), | ||||||||||
| _ => { | ||||||||||
| Err(vm.new_value_error("stack_effect: jump must be False, True or None")) | ||||||||||
| } | ||||||||||
| v.downcast_ref::<PyInt>() | ||||||||||
| .ok_or_else(|| vm.new_type_error(""))? | ||||||||||
| .try_to_primitive::<u32>(vm) | ||||||||||
| }) | ||||||||||
| .unwrap_or(Ok(0))?; | ||||||||||
|
|
||||||||||
| let jump = args | ||||||||||
| .jump | ||||||||||
| .map(|v| { | ||||||||||
| match_class!(match v { | ||||||||||
| b @ PyBool => Ok(b.is(&vm.ctx.true_value)), | ||||||||||
| _n @ PyNone => Ok(false), | ||||||||||
| _ => { | ||||||||||
| Err(vm.new_value_error("stack_effect: jump must be False, True or None")) | ||||||||||
| } | ||||||||||
| }) | ||||||||||
| }) | ||||||||||
| .unwrap_or(Ok(false))?; | ||||||||||
| }) | ||||||||||
| .unwrap_or(Ok(false))?; | ||||||||||
|
|
||||||||||
| let opcode = Opcode::try_from_pyint(args.opcode, vm)?; | ||||||||||
| let instruction = args | ||||||||||
| .opcode | ||||||||||
| .try_to_primitive::<u8>(vm) | ||||||||||
| .and_then(|v| { | ||||||||||
| Instruction::try_from(v) | ||||||||||
| .map_err(|_| vm.new_exception_empty(vm.ctx.exceptions.value_error.to_owned())) | ||||||||||
| }) | ||||||||||
| .map_err(|_| vm.new_value_error("invalid opcode or oparg"))?; | ||||||||||
|
|
||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||||||||||
| Ok(opcode.stack_effect(oparg.into(), jump)) | ||||||||||
| } | ||||||||||
| */ | ||||||||||
| Ok(instruction.stack_effect(oparg.into(), jump)) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /* | ||||||||||
| #[pyfunction] | ||||||||||
| fn is_valid(opcode: i32) -> bool { | ||||||||||
| Opcode::is_valid(opcode) | ||||||||||
| OpcodeId::try_from(opcode).is_ok() | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider filtering pseudo-opcodes in All the Apply this diff to filter pseudo-opcodes: fn is_valid(opcode: i32) -> bool {
- OpcodeId::try_from(opcode).is_ok()
+ OpcodeId::try_from(opcode).is_ok_and(|oid| !oid.is_pseudo())
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
| } | ||||||||||
|
|
||||||||||
| #[pyfunction] | ||||||||||
| fn has_arg(opcode: i32) -> bool { | ||||||||||
| Opcode::has_arg(opcode) | ||||||||||
| OpcodeId::try_from(opcode).map_or(false, |oid| !oid.is_pseudo() && oid.has_arg()) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| #[pyfunction] | ||||||||||
| fn has_const(opcode: i32) -> bool { | ||||||||||
| Opcode::has_const(opcode) | ||||||||||
| OpcodeId::try_from(opcode).map_or(false, |oid| !oid.is_pseudo() && oid.has_const()) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| #[pyfunction] | ||||||||||
| fn has_name(opcode: i32) -> bool { | ||||||||||
| Opcode::has_name(opcode) | ||||||||||
| OpcodeId::try_from(opcode).map_or(false, |oid| !oid.is_pseudo() && oid.has_name()) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| #[pyfunction] | ||||||||||
| fn has_jump(opcode: i32) -> bool { | ||||||||||
| Opcode::has_jump(opcode) | ||||||||||
| OpcodeId::try_from(opcode).map_or(false, |oid| !oid.is_pseudo() && oid.has_jump()) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| #[pyfunction] | ||||||||||
| fn has_free(opcode: i32) -> bool { | ||||||||||
| Opcode::has_free(opcode) | ||||||||||
| OpcodeId::try_from(opcode).map_or(false, |oid| !oid.is_pseudo() && oid.has_free()) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| #[pyfunction] | ||||||||||
| fn has_local(opcode: i32) -> bool { | ||||||||||
| Opcode::has_local(opcode) | ||||||||||
| OpcodeId::try_from(opcode).map_or(false, |oid| !oid.is_pseudo() && oid.has_local()) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| #[pyfunction] | ||||||||||
| fn has_exc(opcode: i32) -> bool { | ||||||||||
| Opcode::has_exc(opcode) | ||||||||||
| OpcodeId::try_from(opcode).map_or(false, |oid| !oid.is_pseudo() && oid.has_exc()) | ||||||||||
| } | ||||||||||
|
ShaharNaveh marked this conversation as resolved.
|
||||||||||
|
|
||||||||||
| #[pyfunction] | ||||||||||
|
|
@@ -212,5 +191,4 @@ mod opcode { | |||||||||
| fn get_specialization_stats(vm: &VirtualMachine) -> PyObjectRef { | ||||||||||
| vm.ctx.none() | ||||||||||
| } | ||||||||||
| */ | ||||||||||
| } | ||||||||||
Uh oh!
There was an error while loading. Please reload this page.