cengal.entities.asm_functions.versions.v_0.asm_functions
1#!/usr/bin/env python 2# coding=utf-8 3 4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space> 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17 18 19__all__ = [ 20 'ASM', 21 'c_uint8', 22 'c_uint16', 23 'c_uint32', 24 'c_uint64', 25 'c_float', 26 'c_double', 27 'asm_func', 28 'asm_func_guard', 29 'asm_funcs_guard', 30 'run_asm', 31 'run_asm__c_uint8', 32 'run_asm__c_uint16', 33 'run_asm__c_uint32', 34 'run_asm__c_uint64', 35 'run_asm__c_float', 36 'run_asm__c_double', 37 'run_asm__no_result', 38 'asm_func_declaration', 39 'declare_asm_function', 40 'daf', 41 'compile_asm_function', 42 'caf', 43 'run_asm_func', 44] 45 46 47""" 48Module Docstring 49Docstrings: http://www.python.org/dev/peps/pep-0257/ 50""" 51 52__author__ = "ButenkoMS <gtalk@butenkoms.space>" 53__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>" 54__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ] 55__license__ = "Apache License, Version 2.0" 56__version__ = "4.4.1" 57__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>" 58__email__ = "gtalk@butenkoms.space" 59# __status__ = "Prototype" 60__status__ = "Development" 61# __status__ = "Production" 62 63 64from cengal.modules_management.alternative_import import alt_import 65with alt_import('cpuinfo') as cpuinfo: 66 if cpuinfo is None: 67 CPUINFO_PRESENT: bool = False 68 else: 69 CPUINFO_PRESENT = True 70 71if not CPUINFO_PRESENT: 72 raise RuntimeError('Package "py-cpuinfo" is not present. Please, install it. Version 7.0.0 or higher is required.') 73 74if not hasattr(cpuinfo, 'ASM'): 75 raise RuntimeError('Package "py-cpuinfo" is not the latest version. Please, update it. Version 7.0.0 or higher is required.') 76 77import ctypes 78from collections import namedtuple 79from typing import Callable, Generator, Sequence, NamedTuple 80 81 82class ASM(cpuinfo.ASM): 83 def __init__(self, restype = None, argtypes = None, machine_code = None): 84 # cpuinfo.ASM.__init__ has mutable default parameter which is not safe. So we fixing it here. 85 argtypes = argtypes if argtypes is not None else () 86 machine_code = machine_code if machine_code is not None else [] 87 self.destroyed: bool = True 88 super().__init__(restype, argtypes, machine_code) 89 90 def compile(self): 91 try: 92 super().compile() 93 finally: 94 self.destroyed = False 95 96 def __call__(self, *args): 97 return self.func(*args) 98 99 def free(self): 100 if self.destroyed: 101 return 102 103 try: 104 super().free() 105 finally: 106 self.destroyed = True 107 108 def __del__(self): 109 self.free() 110 111 112c_uint8 = ctypes.c_uint8 113c_uint16 = ctypes.c_uint16 114c_uint32 = ctypes.c_uint32 115c_uint64 = ctypes.c_uint64 116c_float = ctypes.c_float 117c_double = ctypes.c_double 118 119 120def asm_func(restype = None, argtypes = None, machine_code = None) -> ASM: 121 asm = ASM(restype, argtypes, machine_code) 122 asm.compile() 123 return asm 124 125 126def asm_func_guard(asm: ASM) -> Generator[Callable, None, None]: 127 try: 128 yield asm.func 129 finally: 130 asm.free() 131 132 133def asm_funcs_guard(asm_functions: Sequence[ASM]) -> Generator[Sequence[Callable], None, None]: 134 try: 135 yield [asm.func for asm in asm_functions] 136 finally: 137 for asm in asm_functions: 138 asm.free() 139 140 141def run_asm(restype, *machine_code): 142 asm = ASM(restype, (), machine_code) 143 asm.compile() 144 try: 145 retval = asm.func() 146 finally: 147 asm.free() 148 149 return retval 150 151 152def run_asm__c_uint8(*machine_code): 153 return run_asm(c_uint8, *machine_code) 154 155 156def run_asm__c_uint16(*machine_code): 157 return run_asm(c_uint16, *machine_code) 158 159 160def run_asm__c_uint32(*machine_code): 161 return run_asm(c_uint32, *machine_code) 162 163 164def run_asm__c_uint64(*machine_code): 165 return run_asm(c_uint64, *machine_code) 166 167 168def run_asm__c_float(*machine_code): 169 return run_asm(c_float, *machine_code) 170 171 172def run_asm__c_double(*machine_code): 173 return run_asm(c_double, *machine_code) 174 175 176def run_asm__no_result(*machine_code): 177 return run_asm(None, *machine_code) 178 179 180asm_func_declaration: NamedTuple = namedtuple('asm_func_declaration', ['restype', 'argtypes', 'machine_code']) 181 182 183def declare_asm_function(restype, argtypes, *machine_code) -> NamedTuple: 184 return asm_func_declaration(restype, argtypes, machine_code) 185 186 187daf = declare_asm_function 188 189 190def compile_asm_function(declaration: NamedTuple) -> ASM: 191 asm = ASM(declaration.restype, declaration.argtypes, declaration.machine_code) 192 asm.compile() 193 return asm 194 195 196caf = compile_asm_function 197 198 199def run_asm_func(declaration: NamedTuple, *args): 200 asm = ASM(declaration.restype, declaration.argtypes, declaration.machine_code) 201 asm.compile() 202 try: 203 retval = asm.func(*args) 204 finally: 205 asm.free() 206 207 return retval
class
ASM(cpuinfo.cpuinfo.ASM):
83class ASM(cpuinfo.ASM): 84 def __init__(self, restype = None, argtypes = None, machine_code = None): 85 # cpuinfo.ASM.__init__ has mutable default parameter which is not safe. So we fixing it here. 86 argtypes = argtypes if argtypes is not None else () 87 machine_code = machine_code if machine_code is not None else [] 88 self.destroyed: bool = True 89 super().__init__(restype, argtypes, machine_code) 90 91 def compile(self): 92 try: 93 super().compile() 94 finally: 95 self.destroyed = False 96 97 def __call__(self, *args): 98 return self.func(*args) 99 100 def free(self): 101 if self.destroyed: 102 return 103 104 try: 105 super().free() 106 finally: 107 self.destroyed = True 108 109 def __del__(self): 110 self.free()
ASM(restype=None, argtypes=None, machine_code=None)
84 def __init__(self, restype = None, argtypes = None, machine_code = None): 85 # cpuinfo.ASM.__init__ has mutable default parameter which is not safe. So we fixing it here. 86 argtypes = argtypes if argtypes is not None else () 87 machine_code = machine_code if machine_code is not None else [] 88 self.destroyed: bool = True 89 super().__init__(restype, argtypes, machine_code)
Inherited Members
- cpuinfo.cpuinfo.ASM
- restype
- argtypes
- machine_code
- prochandle
- mm
- func
- address
- size
- run
c_uint8 =
<class 'ctypes.c_ubyte'>
c_uint16 =
<class 'ctypes.c_ushort'>
c_uint32 =
<class 'ctypes.c_uint'>
c_uint64 =
<class 'ctypes.c_ulong'>
class
c_float(_ctypes._SimpleCData):
XXX to be provided
Inherited Members
- _ctypes._SimpleCData
- _SimpleCData
- value
class
c_double(_ctypes._SimpleCData):
XXX to be provided
Inherited Members
- _ctypes._SimpleCData
- _SimpleCData
- value
def
asm_funcs_guard( asm_functions: Sequence[ASM]) -> Generator[Sequence[Callable], NoneType, NoneType]:
def
run_asm(restype, *machine_code):
def
run_asm__c_uint8(*machine_code):
def
run_asm__c_uint16(*machine_code):
def
run_asm__c_uint32(*machine_code):
def
run_asm__c_uint64(*machine_code):
def
run_asm__c_float(*machine_code):
def
run_asm__c_double(*machine_code):
def
run_asm__no_result(*machine_code):
class
asm_func_declaration(builtins.tuple):
asm_func_declaration(restype, argtypes, machine_code)
asm_func_declaration(restype, argtypes, machine_code)
Create new instance of asm_func_declaration(restype, argtypes, machine_code)
Inherited Members
- builtins.tuple
- index
- count
def
declare_asm_function(restype, argtypes, *machine_code) -> <class 'NamedTuple'>:
def
daf(restype, argtypes, *machine_code) -> <class 'NamedTuple'>:
def
run_asm_func(declaration: <class 'NamedTuple'>, *args):