-
Notifications
You must be signed in to change notification settings - Fork 36
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
Python and C3 #91
Comments
Thanks for your interest in this project! There is several options here. First of, you could compile python to IR-code, and C3 to IR-code and then link this IR-code, using Another option would be to compile the C3 code to machine code and dynamically link it into the running python process. This allows to specify python callbacks as well. Things to note here, are the usage of I hope this helps a bit? |
I see, I didn't know the |
There are several issues with
A good place to start might be this part of the code --> https://github.com/windelbouwman/ppci/blob/master/ppci/utils/codepage.py#L108 |
I'd suggest to skip C3 altogether and use C instead. As @windelbouwman suggests, sticking to I myself interested to do more hacking on Python subset compiler, and what I would do is: implement Sadly, I'm too full of ideas and too short of time. So, @darleybarreto, please see if ideas above resonate with you, and if so, feel free to beat be on that.
Manipulating string generally means GC. Surely, trivial operations on static strings (like passing them to |
I was playing with @pfalcon's picompile for a couple of hours and I managed to get some basic |
Hey, cool! Except it's not mine, but a humble fork of https://github.com/sdiehl/numpile, to which I didn't yet even apply any interesting changes. First would be getting rid of intermediate AST, and instead do type inferencing and other processing on real Python AST, because this intermediate AST makes toy processing, like done by numpile, easier, but only complicates further extension. But if you think about type inference, you immediately think about hilarious case of Shedskin, which can't grok the following:
Obviously, there's nothing wrong with the above, and type of
Well, cool, you've got some experience with LLVM API. But you can't get around need for garbage collection when dealing with strings, it's not a value type. So, as soon as you get to:
"Fast way" in which sense? It won't lead to fast or unbloated code, and would be a chore to code up, given that it's largely a throw-away (YMMV) exercise (at least for this usecase). Unless it's already coded up, and it seems that PPCI already supports it, even without ctypes/cffi being exposed (I guess ppci/utils/codepage.py#L108 quoted by @windelbouwman is the underlying impl): https://ppci.readthedocs.io/en/latest/howto/jitting.html#calling-python-functions-from-native-code ;-) |
Interesting, although I have to say I don't know much of SSA stuff.
I thought on loading and linking
In the sense of getting something working. |
Makes sense, please keep us posted of your progress! |
So, instead of dealing with ctypes, what if we use an independent rust shared lib with from cffi import FFI
ffi = FFI()
ffi.cdef("""
char * load_str(char *);
void free_str(char *);
""")
p_str = "I will go down to the rabbit hole!".encode("utf-8")
size = len(p_str)
lib = ffi.dlopen("path/to/rustlib.so")
pointer = lib.load_str(p)
p_str_2 = ffi.buffer(pointer,size)[:].decode("utf-8") # a perfect string
lib.free_str(pointer) # freeing In this example, the use core::{ptr,str};
use ustr::Ustr;
use cstr_core::{CString, CStr, c_char};
#[no_mangle]
pub extern "C" fn load_str(d_ptr: *mut c_char) -> *mut c_char {
// Based on https://bheisler.github.io/post/calling-rust-in-python/
if d_ptr.is_null() {
return ptr::null_mut();
}
let data = unsafe { CStr::from_ptr(d_ptr).to_bytes() };
match str::from_utf8(data){
Ok(data_str) => {
let gc_string = Ustr::from(data_str);
CString::new(gc_string.as_str()).unwrap().into_raw()
},
Err(e) => {
println!("Error while converting raw pointer back to str: {}", e);
ptr::null_mut()
},
}
} The lib in question would do the whole work as a simple wrapper to rust's |
First I would like to thank everyone who has contributed to this work, it is really impressive. I wonder if there's a way of linking a python function in a c3 program. I was trying to do this on IR level, but it seems it doesn't work. For example:
I want to play around making subset of python code and compiling it to interact with the system, which C3 makes possible.
The text was updated successfully, but these errors were encountered: