Skip to content

Commit

Permalink
l2s: implemented br
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugobros3 committed Oct 24, 2023
1 parent 1df2d43 commit f2cfb4a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
15 changes: 9 additions & 6 deletions src/frontends/llvm/l2s.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ static bool cmp_opaque_ptr(OpaqueRef* a, OpaqueRef* b) {
return *a == *b;
}

static const Node* write_bb_tail(Parser* p, BodyBuilder* b, LLVMBasicBlockRef bb, LLVMValueRef first_instr) {
static const Node* write_bb_tail(Parser* p, Node* fn, BodyBuilder* b, LLVMBasicBlockRef bb, LLVMValueRef first_instr) {
LLVMValueRef instr;
for (instr = first_instr; instr; instr = LLVMGetNextInstruction(instr)) {
bool last = instr == LLVMGetLastInstruction(bb);
if (last)
assert(LLVMGetBasicBlockTerminator(bb) == instr);
LLVMDumpValue(instr);
printf("\n");
EmittedInstr emitted = emit_instruction(p, b, instr);
EmittedInstr emitted = convert_instruction(p, fn, b, instr);
if (emitted.terminator)
return finish_body(b, emitted.terminator);
if (!emitted.instruction)
Expand All @@ -50,7 +50,7 @@ static const Node* write_bb_tail(Parser* p, BodyBuilder* b, LLVMBasicBlockRef bb
assert(false);
}

static const Node* emit_bb(Parser* p, Node* fn, LLVMBasicBlockRef bb) {
const Node* convert_basic_block(Parser* p, Node* fn, LLVMBasicBlockRef bb) {
const Node** found = find_value_dict(LLVMValueRef, const Node*, p->map, bb);
if (found) return *found;
IrArena* a = get_module_arena(p->dst);
Expand All @@ -69,10 +69,13 @@ static const Node* emit_bb(Parser* p, Node* fn, LLVMBasicBlockRef bb) {
}
after_phis:
{
Node* nbb = basic_block(a, fn, params, LLVMGetBasicBlockName(bb));
String name = LLVMGetBasicBlockName(bb);
if (!name || strlen(name) == 0)
name = unique_name(a, "bb");
Node* nbb = basic_block(a, fn, params, name);
insert_dict(LLVMValueRef, const Node*, p->map, bb, nbb);
BodyBuilder* b = begin_body(a);
write_bb_tail(p, b, bb, instr);
nbb->payload.basic_block.body = write_bb_tail(p, fn, b, bb, instr);
return nbb;
}
}
Expand Down Expand Up @@ -111,7 +114,7 @@ const Node* convert_function(Parser* p, LLVMValueRef fn) {
if (first_bb) {
BodyBuilder* b = begin_body(a);
insert_dict(LLVMValueRef, const Node*, p->map, first_bb, f);
f->payload.fun.body = write_bb_tail(p, b, first_bb, LLVMGetFirstInstruction(first_bb));
f->payload.fun.body = write_bb_tail(p, f, b, first_bb, LLVMGetFirstInstruction(first_bb));
}

return r;
Expand Down
26 changes: 23 additions & 3 deletions src/frontends/llvm/l2s_instr.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static Nodes reinterpret_operands(BodyBuilder* b, Nodes ops, const Type* dst_t)
}

/// instr may be an instruction or a constantexpr
EmittedInstr emit_instruction(Parser* p, BodyBuilder* b, LLVMValueRef instr) {
EmittedInstr convert_instruction(Parser* p, Node* fn, BodyBuilder* b, LLVMValueRef instr) {
IrArena* a = get_module_arena(p->dst);
int num_ops = LLVMGetNumOperands(instr);
size_t num_results = 1;
Expand All @@ -62,8 +62,28 @@ EmittedInstr emit_instruction(Parser* p, BodyBuilder* b, LLVMValueRef instr) {
.args = num_ops == 0 ? empty(a) : convert_operands(p, num_ops, instr)
})
};
case LLVMBr:
goto unimplemented;
case LLVMBr: {
unsigned n_successors = LLVMGetNumSuccessors(instr);
LARRAY(const Node*, targets, n_successors);
for (size_t i = 0; i < n_successors; i++)
targets[i] = convert_basic_block(p, fn, LLVMGetSuccessor(instr, i));
if (LLVMIsConditional(instr)) {
assert(n_successors == 2);
const Node* condition = convert_value(p, LLVMGetCondition(instr));
return (EmittedInstr) {
.terminator = branch(a, (Branch) {
.branch_condition = condition,
.true_jump = jump_helper(a, targets[0], empty(a)),
.false_jump = jump_helper(a, targets[1], empty(a)),
})
};
} else {
assert(n_successors == 1);
return (EmittedInstr) {
.terminator = jump_helper(a, targets[0], empty(a))
};
}
}
case LLVMSwitch:
goto unimplemented;
case LLVMIndirectBr:
Expand Down
16 changes: 9 additions & 7 deletions src/frontends/llvm/l2s_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,27 @@ typedef struct ParsedAnnotationContents_ {
struct ParsedAnnotationContents_* next;
} ParsedAnnotation;

typedef struct {
const Node* terminator;
const Node* instruction;
Nodes result_types;
} EmittedInstr;

ParsedAnnotation* find_annotation(Parser*, const Node*);
ParsedAnnotation* next_annotation(ParsedAnnotation*);
void add_annotation(Parser*, const Node*, ParsedAnnotation);

void process_llvm_annotations(Parser* p, LLVMValueRef global);

EmittedInstr emit_instruction(Parser* p, BodyBuilder* b, LLVMValueRef instr);
const Node* convert_value(Parser* p, LLVMValueRef v);
const Node* convert_function(Parser* p, LLVMValueRef fn);
const Type* convert_type(Parser* p, LLVMTypeRef t);
const Node* convert_metadata(Parser* p, LLVMMetadataRef meta);
const Node* convert_global(Parser* p, LLVMValueRef global);
const Node* convert_function(Parser* p, LLVMValueRef fn);
const Node* convert_basic_block(Parser* p, Node* fn, LLVMBasicBlockRef bb);

typedef struct {
const Node* terminator;
const Node* instruction;
Nodes result_types;
} EmittedInstr;

EmittedInstr convert_instruction(Parser* p, Node* fn, BodyBuilder* b, LLVMValueRef instr);

void postprocess(Parser*, Module* src, Module* dst);

Expand Down
2 changes: 1 addition & 1 deletion src/frontends/llvm/l2s_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const Node* convert_value(Parser* p, LLVMValueRef v) {
break;
case LLVMConstantExprValueKind: {
BodyBuilder* bb = begin_body(a);
EmittedInstr emitted = emit_instruction(p, bb, v);
EmittedInstr emitted = convert_instruction(p, NULL, bb, v);
r = anti_quote_helper(a, emitted.instruction);
break;
}
Expand Down

0 comments on commit f2cfb4a

Please sign in to comment.