From 578531395ecbabd8179e31520c2832ac7d6d3765 Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Thu, 27 Jul 2023 12:06:17 -0700 Subject: add dynamic typing example --- python/examples/dynamic_typing/dietpass | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 python/examples/dynamic_typing/dietpass (limited to 'python/examples/dynamic_typing/dietpass') diff --git a/python/examples/dynamic_typing/dietpass b/python/examples/dynamic_typing/dietpass new file mode 100755 index 0000000..5d15e19 --- /dev/null +++ b/python/examples/dynamic_typing/dietpass @@ -0,0 +1,50 @@ +#!/bin/python3 +import os +import sys +from dietc import * + +def main(): + prog = Program(open(sys.argv[1], "rb").read()) + for function in prog.functions: + fn_locals = function.locals() + start_i = function.code_start() + new_body = [] + for op in function.body: + new_body.append(op) + # only look for casts to void + if " = ( " not in op.line: continue + if op.lhs_type(prog) != PointerType(BasicType("void")): continue + # if one is found, construct & annotate its type + rhs_type = prog.object_types[op[-2]] + type_ptr = describe_type(prog, rhs_type, new_body) + new_body.append(Instruction( + f"annotate_type({op[0]}, {type_ptr});")) + function.body = new_body + prog.preamble.append("#include ") + prog.print() + +def describe_type(prog, type_, new_body): + name = f"type_{count()}" + if isinstance(type_, BasicType): + new_body.append(Instruction( + f"void *{name} = make_basic(\"{type_.name}\", sizeof({type_.name}));")) + elif isinstance(type_, ArrayType): + base = describe_type(prog, type_.base, new_body) + new_body.append(Instruction( + f"void *{name} = make_array({base}, {type_.length});")) + elif isinstance(type_, PointerType): + base = describe_type(prog, type_.base, new_body) + new_body.append(Instruction( + f"void *{name} = make_pointer({base});")) + elif isinstance(type_, AggregateType): + tname = prog.type_to_name[type_] + new_body.append(Instruction( + f"void *{name} = make_struct(sizeof({tname}));")) + for fn, ft in reversed(type_.fields): + field = describe_type(prog, ft, new_body) + new_body.append(Instruction( + f"prepend_member({name}, {field}, \"{fn}\", offsetof({tname}, {fn}));")) + else: raise NotImplementedError + return name + +main() -- cgit v1.2.3