Lance Pollard
Lance Pollard

Reputation: 79440

How to convert LLVM IR br back to a while loop

I am wondering how to do sort of transpilation of source to source using LLVM at a high level. Given that LLVM converts while loops to using br and the like, I am wondering how to then take that specific IR chunk and convert it back to a while loop in a language such as JavaScript.

C while loop -> LLVM IR -> JavaScript while loop

This article suggests that Emscripten converts LLVM code to JavaScript, so it probably handles this somewhere.

I'm wondering just the general strategy for converting it, if there is one. It seems a bit tricky from a distance, figuring out the statements to piece together a while loop from IR.

Upvotes: 1

Views: 363

Answers (2)

JKRT
JKRT

Reputation: 1207

During the translation from C to LLVM IR, instructions that are deemed necessary can be decorated with Metadata, this metadata can then be used to convert LLVM IR to JavaScript, e.g indicating if the circular branching between basic blocks is a while loop or not (This information is present in the C context). See Intrinsics & Metadata Attributes.

For more information regarding LLVM Metadata see LLVM-Metadata.

Upvotes: 1

Anatoly Trosinenko
Anatoly Trosinenko

Reputation: 754

In Emscripten the algorithm of recreating high-level language structures is called Relooping and is described in this parer. I'm not sure, though, that it is up-to-date information, but it probably answers your question.

Upvotes: 1

Related Questions