def setUp(self): super(TestRedirectionToDeclaration, self).setUp() name = type(self).__name__ private_h = Header("private.h") private_h.add_type(Structure("Private")) public_h = Header("public.h") public_h.add_types([ Type["Private"].gen_forward_declaration(), Function("public_func") ]) private_c = Source("private.c") public_func_impl = Type["public_func"].gen_definition() private_c.add_type(public_func_impl) src = Source(name.lower() + ".c").add_global_variable( # It must internally re-direct pointer from type "Private" # to "Private.declaration", its forward declaration. Pointer(Type["Private"])("handler")) src.add_type(Pointer(public_func_impl, name="cb_ptr")) src_content = """\ /* %s */ #include "public.h" typedef void (*cb_ptr)(void); Private *handler __attribute__((unused)); """ % (name.lower() + ".c") self.files = [(src, src_content)]
def setUp(self): super(TestOpaqueCode, self).setUp() name = type(self).__name__ hdr = Header(name.lower() + ".h", protection=False) test_var = Type["int"]("test_var") test_func = Function(name="test_func") opaque_top = OpaqueCode("""\ /* Generic comment above macros */ """, weight=0) opaque_bottom = OpaqueCode(""" /* A comment at bottom of file */ """, weight=10) opaque_middle = OpaqueCode(""" /* How to use test_var and test_func. */ """, used_variables=[test_var], used_types=[test_func] # Default weight (5) ) hdr.add_types([ # Yields #define statement with weight 1 Macro("TEST_MACRO"), # Yields function declaration with weight 6 Function(name="another_test_func"), opaque_middle, opaque_bottom, opaque_top ]) hdr_content = """\ /* {path} */ /* Generic comment above macros */ #define TEST_MACRO extern int test_var; void test_func(void); /* How to use test_var and test_func. */ void another_test_func(void); /* A comment at bottom of file */ """.format(path=hdr.path) self.files = [(hdr, hdr_content)]
def fill_source(self): """ Adds a hook callback in MMIO read handler. """ super(CustomSBDType, self).fill_source() # Now, code of device's module is generated. It's time to patch it! # Assuming, your code uses functions and types from your header. custom_h = Header("custom-api.h", is_global=True) # If your header is inside Qemu source tree and under Git, then you # likely should write above statement as follows: # # custom_h = Header["custom/custom-api.h"] # # Because the header is already registered. # To use custom functions (and other things) you must declare them. # Note that, macros are added automatically if header is inside Qemu # source tree. # Only name is sufficient because no code will be generated for them. custom_h.add_types([Function("custom_callback")]) read_func = self.find_mmio_read_helper(0) # We are going to pass second and following argument values of the # read helper to the custom callback. # Of course, you can made different choice in your code. args2callback = read_func.args[1:] # Insert function call statement before 3-nd statement. # Note, first statements are likely variable declarations. # This is for prettiness only. read_func.body.children.insert(2, Call("custom_callback", *args2callback))