def cpu_reset(self): cpu_reset = Type["QEMUResetHandler"].use_as_prototype( self.qtn.for_id_name + "_cpu_reset", body=BodyTree(), static=True) var_cpu = Pointer(Type["CPUState"])("cpu") cpu_reset.body = BodyTree()(Declare( OpDeclareAssign(var_cpu, MCall("CPU", cpu_reset.args[0]))), NewLine(), Call("cpu_reset", var_cpu)) self.source.add_type(cpu_reset) return cpu_reset
def setUp(self): super(TestLabelAndGotoGeneration, self).setUp() name = type(self).__name__ src = Source(name.lower() + ".c") lbl = Label("begin") i = Type["int"]("i") src.add_type( Function(name="main", body=BodyTree()(Declare(i), lbl, OpAssign(i, OpAdd(i, 1)), Goto(lbl)))) src_content = """\ /* {} */ void main(void) {{ int i; begin: i = i + 1; goto begin; }} """.format(src.path) self.files = [(src, src_content)]
def setUp(self): super(TestSeparateCases, self).setUp() src = Source(type(self).__name__.lower() + ".c") i = Type["int"]("i") src.add_type( Function(name="func_a", body=BodyTree()(Declare(OpDeclareAssign(i, 0)), BranchSwitch(i, separate_cases=True)( SwitchCase(1), SwitchCase(2))))) src_content = """\ /* {} */ void func_a(void) {{ int i = 0; switch (i) {{ case 1: break; case 2: break; default: break; }} }} """.format(src.path) self.files = [(src, src_content)]
def setUp(self): super(TestHeaderInclusion, self).setUp() name = type(self).__name__ f = Function(name="test_f") f_def = f.gen_definition() hdr = Header(name.lower() + ".h").add_type(f) hdr_content = """\ /* {path} */ #ifndef INCLUDE_{fname_upper}_H #define INCLUDE_{fname_upper}_H void test_f(void); #endif /* INCLUDE_{fname_upper}_H */ """.format(path=hdr.path, fname_upper=name.upper()) src1 = Source(name.lower() + ".c").add_type(f_def) src1_content = """\ /* {} */ void test_f(void) {{}} """.format(src1.path) src2 = Source(name.lower() + "2.c").add_type( Function( name="func_a", body=BodyTree()( Call(f_def) # use function definition to Call ))) src2_content = """\ /* {} */ #include "{}" void func_a(void) {{ test_f(); }} """.format(src2.path, hdr.path) self.files = [(hdr, hdr_content), (src1, src1_content), (src2, src2_content)]
def setUp(self): super(TestEnumerations, self).setUp() name = type(self).__name__ try: h = Header["enums.h"] except: h = Header("enums.h") h.add_type(Enumeration("A", [("one", 1), ("two", 2)])) a = Type["int"]("a") b = Type["int"]("b") c = Type["int"]("c") src = Source(name.lower() + ".c").add_types([ Enumeration("B", [("three", 3), ("four", 4)], "B"), Function(name="main", body=BodyTree()(Declare(a, b, c), OpAssign(a, Type["A"].one), OpAssign(b, Type["B"].three), OpAssign(c, Type["four"]))) ]) src_content = """\ /* {} */ #include "enums.h" enum B {{ three = 3, four = 4 }}; void main(void) {{ int a, b, c; a = one; b = three; c = four; }} """.format(src.path) self.files = [(src, src_content)]
def setUp(self): super(TestEnumerations, self).setUp() name = type(self).__name__ try: h = Header["enums.h"] except: h = Header("enums.h") h.add_type(Enumeration([("EXT", 1)])) src = Source(name.lower() + ".c").add_types([ Enumeration([("ONE", 1)]), Enumeration([("TWO", 2)], enum_name="A"), Enumeration([("THREE", 3)], typedef_name="B"), Enumeration([("FOUR", 4)], enum_name="C", typedef_name="D") ]) a = Type["int"]("a") b = Type["int"]("b") c = Type["A"]("c") d = Type["B"]("d") e = Type["D"]("e") src.add_types([ Function(name="main", body=BodyTree()(Declare(a, b), OpAssign(a, Type["EXT"]), OpAssign(b, Type["ONE"]), Declare(c), OpAssign(c, Type["TWO"]), Declare(d), OpAssign(d, Type["THREE"]), Declare(e), OpAssign(e, Type["FOUR"]))) ]) src_content = """\ /* {} */ #include "enums.h" enum A {{ TWO = 2 }}; typedef enum {{ THREE = 3 }} B; typedef enum C {{ FOUR = 4 }} D; enum {{ ONE = 1 }}; void main(void) {{ int a, b; a = EXT; b = ONE; enum A c; c = TWO; B d; d = THREE; D e; e = FOUR; }} """.format(src.path) self.files = [(src, src_content)]
def machine_register_2_6(mach): # machine class definition function class_init = Function( name = "machine_%s_class_init" % mach.qtn.for_id_name, static = True, ret_type = Type["void"], args = [ Pointer(Type["ObjectClass"])("oc"), Pointer(Type["void"])("opaque") ] ) mc = Pointer(Type["MachineClass"])("mc") class_init.body = BodyTree()( Declare( OpDeclareAssign( mc, MCall( "MACHINE_CLASS", class_init.args[0] ) ) ), NewLine(), OpAssign( OpSDeref(mc, "desc"), mach.desc ), OpAssign( OpSDeref(mc, "init"), mach.instance_init ) ) mach.class_init = class_init mach.source.add_type(class_init) # machine type definition structure type_machine_macro = Type["TYPE_MACHINE"] type_machine_type_name_macro = Type["MACHINE_TYPE_NAME"] mach.type_info = Type["TypeInfo"]( name = "machine_type_%s" % mach.qtn.for_id_name, static = True, initializer = Initializer({ "name" : type_machine_type_name_macro.gen_usage_string( Initializer({ "machinename" : '"%s"' % mach.qtn.for_id_name }) ), "parent" : type_machine_macro, "class_init" : mach.class_init }, used_types = [ type_machine_type_name_macro ] ) ) mach.source.add_global_variable(mach.type_info) # machine type registration function mach.type_reg_func = mach.gen_register_types_fn(mach.type_info) mach.source.add_type(mach.type_reg_func) # Main machine registration macro machine_init_def_args = Initializer({ "function": mach.type_reg_func }) mach.source.add_type( Type["type_init"].gen_type(initializer = machine_init_def_args) )
def machine_register_2_5(mach): # machine class definition function class_init = Function( name = "machine_%s_class_init" % mach.qtn.for_id_name, static = True, ret_type = Type["void"], args = [ Pointer(Type["ObjectClass"])("oc"), Pointer(Type["void"])("opaque") ] ) mc = Pointer(Type["MachineClass"])("mc") class_init.body = BodyTree()( Declare( OpDeclareAssign( mc, MCall( "MACHINE_CLASS", class_init.args[0] ) ) ), NewLine(), OpAssign( OpSDeref(mc, "name"), mach.qtn.for_id_name ), OpAssign( OpSDeref(mc, "desc"), mach.desc ), OpAssign( OpSDeref(mc, "init"), mach.instance_init ) ) mach.class_init = class_init mach.source.add_type(class_init) # machine type definition structure type_machine_macro = Type["TYPE_MACHINE"] type_machine_suf_macro = Type["TYPE_MACHINE_SUFFIX"] mach.type_info = Type["TypeInfo"]( name = "machine_type_%s" % mach.qtn.for_id_name, static = True, initializer = Initializer({ "name" : '"%s" %s' % (mach.qtn.for_id_name, type_machine_suf_macro.name ), "parent" : type_machine_macro, "class_init" : mach.class_init }, used_types = [ type_machine_suf_macro ] ) ) mach.source.add_global_variable(mach.type_info) # machine type registration function mach.type_reg_func = Function( name = "machine_init_%s" % mach.qtn.for_id_name, body = BodyTree()( Call( "type_register", OpAddr(mach.type_info) ) ), static = True ) mach.source.add_type(mach.type_reg_func) # Main machine registration macro def_type = get_vp("machine initialization function register type name") machine_init_def_args = Initializer( code = { "function": mach.type_reg_func } ) mach.source.add_type( Type[def_type].gen_type(initializer = machine_init_def_args) )