Implementing the assembler parser
The assembler parser is easy to implement, since LLVM provides a framework for it, and large parts are generated from the target description.
The ParseInstruction()
method in our class is called when the framework detects that an instruction needs to be parsed. That method parses in input via the provided lexer and constructs a so-called operand vector. An operand can be a token such as an instruction mnemonic, a register name, or an immediate, or it can be category-specific to the target. For example, two operands are constructed from the jmp %r2
input: a token operand for the mnemonic, and a register operand.
Then a generated matcher tries to match the operand vector against the instructions. If a match is found, then an instance of the MCInst
class is created, which holds the parsed instruction. Otherwise, an error message is emitted. The advantage of this approach is that it automatically derives the matcher from the target description, without...