mirror of
https://github.com/tinygrad/tinygrad.git
synced 2026-01-09 15:08:02 -05:00
elf loader (#5508)
* elf loader * cleanup * cleaner * cleaner * fixes * revert this * fix div 0 * fix nv * amd fix * fix mockgpu * amd better? * restore relocs for <12.4 * linter * this is fixed now * revert this * process cdefines as function * cleaner * align * save lines * revert this change
This commit is contained in:
@@ -39,6 +39,56 @@ def _try_dlopen_$name():
|
||||
EOF
|
||||
}
|
||||
|
||||
process_cdefines() {
|
||||
local input_file="$1"
|
||||
local output_file="$2"
|
||||
|
||||
sed -E '
|
||||
# Remove single-line comments
|
||||
s/[[:space:]]*\/\*.*\*\///g
|
||||
|
||||
# Remove multi-line comments
|
||||
/\/\*/,/\*\//d
|
||||
|
||||
/.*DT_MIPS_NUM.*/d
|
||||
|
||||
# Remove lines ending with backslash (multi-line macros)
|
||||
/\\$/d
|
||||
|
||||
# Convert C integer literals (remove U suffix)
|
||||
s/\b([0-9]+)U\b/\1/g
|
||||
|
||||
# Convert C types to Python ctypes
|
||||
s/\bunsigned char\b/ctypes.c_ubyte/g
|
||||
s/\bsigned char\b/ctypes.c_byte/g
|
||||
s/\bunsigned short\b/ctypes.c_ushort/g
|
||||
s/\bshort\b/ctypes.c_short/g
|
||||
s/\bunsigned int\b/ctypes.c_uint/g
|
||||
s/\bint\b/ctypes.c_int/g
|
||||
s/\bunsigned long\b/ctypes.c_ulong/g
|
||||
s/\blong\b/ctypes.c_long/g
|
||||
s/\bfloat\b/ctypes.c_float/g
|
||||
s/\bdouble\b/ctypes.c_double/g
|
||||
|
||||
# Function-like macros with parameters
|
||||
/^#define[[:space:]]+([[:alnum:]_]+)[[:space:]]*\(([^)]*)\)[[:space:]]+(.+)/ {
|
||||
s//def \1(\2): return \3/
|
||||
p
|
||||
d
|
||||
}
|
||||
|
||||
# Simple #define statements (including those with parentheses)
|
||||
/^#define[[:space:]]+([[:alnum:]_]+)[[:space:]]+(.+)/ {
|
||||
s//\1 = \2/
|
||||
p
|
||||
d
|
||||
}
|
||||
|
||||
# Drop all other lines
|
||||
d
|
||||
' "$input_file" >> "$output_file"
|
||||
}
|
||||
|
||||
generate_opencl() {
|
||||
clang2py /usr/include/CL/cl.h -o $BASE/opencl.py -l /usr/lib/x86_64-linux-gnu/libOpenCL.so.1 -k cdefstum
|
||||
fixup $BASE/opencl.py
|
||||
@@ -210,9 +260,12 @@ generate_libc() {
|
||||
clang2py \
|
||||
$(dpkg -L libc6-dev | grep sys/mman.h) \
|
||||
$(dpkg -L libc6-dev | grep sys/syscall.h) \
|
||||
/usr/include/elf.h \
|
||||
/usr/include/unistd.h \
|
||||
-o $BASE/libc.py
|
||||
|
||||
process_cdefines "/usr/include/elf.h" "$BASE/libc.py"
|
||||
|
||||
sed -i "s\import ctypes\import ctypes, ctypes.util, os\g" $BASE/libc.py
|
||||
sed -i "s\FIXME_STUB\libc\g" $BASE/libc.py
|
||||
sed -i "s\FunctionFactoryStub()\ctypes.CDLL(ctypes.util.find_library('c'))\g" $BASE/libc.py
|
||||
|
||||
Reference in New Issue
Block a user