def codegen(self, builder: IRBuilder, ctx: Context): self.check_children() body = ctx.function.append_basic_block() condition = ctx.function.append_basic_block() end = ctx.function.append_basic_block() ctx.start_scope() builder.branch(body) builder.position_at_end(body) ctx.label = body body_node = self.children[1] body_node.codegen(builder, ctx) # Alla fine faccio il collect collect_fun = ctx.get_function(fn.collect_function) builder.call(collect_fun, []) builder.branch(condition) ctx.end_scope() builder.position_at_end(condition) ctx.label = condition condition_node = self.children[0] last = condition_node.codegen(builder, ctx) cond_res = builder.icmp_unsigned("!=", last, ir.Constant(ir.IntType(8), 0)) builder.cbranch(cond_res, body, end) builder.position_at_end(end) ctx.label = end
def builder_icmp_unsigned( builder: ir.IRBuilder, operator: str, left: ir.Value, right: ir.Value ) -> R[Pair[BlockBuilder, Value]]: builder_ = BlockBuilder.box(builder) return ( builder_.icmp_signed(operator, Value.box(left), Value.box(right)), lambda: Pair[BlockBuilder, Value].create( builder_, Value.box(builder.icmp_unsigned(operator, left, right)) ), )
def string_to_volpe(b: ir.IRBuilder, string: ir.Value): with options(b, int64) as (ret, phi): ret(int64(0)) character = b.load(b.gep(string, [phi])) with b.if_then(b.icmp_unsigned("!=", character, char(0))): ret(b.add(phi, int64(1))) new_string = string_type.unwrap()(ir.Undefined) new_string = b.insert_value(new_string, string, 0) new_string = b.insert_value(new_string, phi, 1) return new_string
def codegen(self, builder: IRBuilder, ctx: Context): self.check_children() l = len(self.children) condition = ctx.function.append_basic_block() true = ctx.function.append_basic_block() false = None if l == 3: false = ctx.function.append_basic_block() end = ctx.function.append_basic_block() if false is None: false = end builder.branch(condition) builder.position_at_end(condition) condition_node = self.children[0] last = condition_node.codegen(builder, ctx) cond_res = builder.icmp_unsigned("!=", last, ir.Constant(ir.IntType(8), 0)) builder.cbranch(cond_res, true, false) ctx.start_scope() builder.position_at_end(true) ctx.label = true true_node = self.children[1] true_node.codegen(builder, ctx) builder.branch(end) ctx.end_scope() if l == 3: ctx.start_scope() builder.position_at_end(false) ctx.label = false false_node = self.children[2] false_node.codegen(builder, ctx) builder.branch(end) ctx.end_scope() builder.position_at_end(end) ctx.label = end # Alla fine faccio il collect collect_fun = ctx.get_function(fn.collect_function) builder.call(collect_fun, [])