Skip to content

Commit 676cc1c

Browse files
committed
l2s: parsing improvements
1 parent 971bc83 commit 676cc1c

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/frontends/llvm/l2s_instr.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,19 @@ EmittedInstr convert_instruction(Parser* p, Node* fn_or_bb, BodyBuilder* b, LLVM
272272
} case LLVMSExt: {
273273
// reinterpret as signed, convert to change size, reinterpret back to target T
274274
const Type* src_t = convert_type(p, LLVMTypeOf(LLVMGetOperand(instr, 0)));
275-
const Type* signed_src_t = change_int_t_sign(src_t, true);
276-
const Type* signed_dst_t = change_int_t_sign(t, true);
277-
r = prim_op_helper(a, convert_op, singleton(signed_dst_t), reinterpret_operands(b, convert_operands(p, num_ops, instr), signed_src_t));
278-
r = prim_op_helper(a, reinterpret_op, singleton(t), BIND_PREV_R(signed_dst_t));
275+
Nodes ops = convert_operands(p, num_ops, instr);
276+
if (src_t->tag == Bool_TAG) {
277+
assert(t->tag == Int_TAG);
278+
const Node* zero = int_literal(a, (IntLiteral) { .value = 0, .width = t->payload.int_type.width, .is_signed = t->payload.int_type.is_signed });
279+
uint64_t i = UINT64_MAX >> (64 - int_size_in_bytes(t->payload.int_type.width) * 8);
280+
const Node* ones = int_literal(a, (IntLiteral) { .value = i, .width = t->payload.int_type.width, .is_signed = t->payload.int_type.is_signed });
281+
r = prim_op_helper(a, select_op, empty(a), mk_nodes(a, first(ops), ones, zero));
282+
} else {
283+
const Type* signed_src_t = change_int_t_sign(src_t, true);
284+
const Type* signed_dst_t = change_int_t_sign(t, true);
285+
r = prim_op_helper(a, convert_op, singleton(signed_dst_t), reinterpret_operands(b, ops, signed_src_t));
286+
r = prim_op_helper(a, reinterpret_op, singleton(t), BIND_PREV_R(signed_dst_t));
287+
}
279288
break;
280289
} case LLVMFPToUI:
281290
case LLVMFPToSI:
@@ -439,6 +448,14 @@ EmittedInstr convert_instruction(Parser* p, Node* fn_or_bb, BodyBuilder* b, LLVM
439448
// TODO
440449
return (EmittedInstr) { 0 };
441450
}
451+
if (strcmp(intrinsic, "llvm.dbg.value") == 0) {
452+
// TODO
453+
return (EmittedInstr) { 0 };
454+
}
455+
if (string_starts_with(intrinsic, "llvm.lifetime")) {
456+
// don't care
457+
return (EmittedInstr) { 0 };
458+
}
442459
if (string_starts_with(intrinsic, "llvm.memcpy")) {
443460
Nodes ops = convert_operands(p, num_ops, instr);
444461
num_results = 0;

src/frontends/llvm/l2s_postprocess.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ bool lexical_scope_is_nested(Nodes scope, Nodes parentMaybe) {
6565

6666
bool compare_nodes(Nodes* a, Nodes* b);
6767

68+
static Nodes remake_variables(Context* ctx, Nodes old) {
69+
IrArena* a = ctx->rewriter.dst_arena;
70+
LARRAY(const Node*, nvars, old.count);
71+
for (size_t i = 0; i < old.count; i++) {
72+
const Node* node = old.nodes[i];
73+
nvars[i] = var(a, node->payload.var.type ? qualified_type_helper(rewrite_node(&ctx->rewriter, node->payload.var.type), false) : NULL, node->payload.var.name);
74+
assert(nvars[i]->tag == Variable_TAG);
75+
}
76+
return nodes(a, old.count, nvars);
77+
}
78+
6879
static const Node* process_op(Context* ctx, NodeClass op_class, String op_name, const Node* node) {
6980
IrArena* a = ctx->rewriter.dst_arena;
7081
switch (node->tag) {
@@ -156,7 +167,6 @@ static const Node* process_op(Context* ctx, NodeClass op_class, String op_name,
156167
} else if (compare_nodes(dom_lexical_scope, dst_lexical_scope)) {
157168
debug_print("We need to introduce a control() block at %s, pointing at %s\n.", get_abstraction_name(dom->node), get_abstraction_name(dst));
158169
Controls** found = find_value_dict(const Node, Controls*, ctx->controls, dom->node);
159-
assert(found);
160170
if (found) {
161171
Controls* controls = *found;
162172
const Node* join_token = NULL;
@@ -174,15 +184,15 @@ static const Node* process_op(Context* ctx, NodeClass op_class, String op_name,
174184
controls->tokens = append_nodes(a, controls->tokens, join_token);
175185
controls->destinations = append_nodes(a, controls->destinations, dst);
176186
}
177-
Nodes nargs = recreate_variables(&ctx->rewriter, get_abstraction_params(dst));
178-
187+
Nodes nparams = remake_variables(ctx, get_abstraction_params(dst));
188+
//register_processed_list(&ctx->rewriter, get_abstraction_params(dst), nparams);
179189
Node* fn = src;
180190
if (fn->tag == BasicBlock_TAG)
181191
fn = (Node*) fn->payload.basic_block.fn;
182192
assert(fn->tag == Function_TAG);
183-
Node* wrapper = basic_block(a, fn, nargs, format_string_arena(a->arena, "wrapper_to_%s", get_abstraction_name(dst)));
193+
Node* wrapper = basic_block(a, fn, nparams, format_string_arena(a->arena, "wrapper_to_%s", get_abstraction_name(dst)));
184194
wrapper->payload.basic_block.body = join(a, (Join) {
185-
.args = nargs,
195+
.args = nparams,
186196
.join_point = join_token
187197
});
188198
return jump_helper(a, wrapper, rewrite_nodes(&ctx->rewriter, node->payload.jump.args));
@@ -247,7 +257,7 @@ void postprocess(Parser* p, Module* src, Module* dst) {
247257

248258
ctx.rewriter.rewrite_op_fn = (RewriteOpFn) process_op;
249259
ctx.rewriter.config.process_variables = true;
250-
// ctx.rewriter.config.search_map = false;
260+
ctx.rewriter.config.search_map = true;
251261
// ctx.rewriter.config.write_map = false;
252262

253263
rewrite_module(&ctx.rewriter);

0 commit comments

Comments
 (0)