Services and Modeling for Embedded Software Development
Embecosm divider strip
Prev  Next

3.5. Parsing Registers and Immediates

The rest of the functions within archASMParser handle parsing particular operand types.

To parse the name of a register, a ParseRegister function is created. The lexer reading the file is asked for which type of token the function has been given. A register is an identifier (i.e. short string, e.g. r0). To firstly filter out incorrect token types, if it is not an identifier, the function simply returns 0 to indicate that it was unable to parse it.

If the token is an identifier, it is then given to the TableGen generated MatchRegisterName function. This returns a value corresponding to the register if it is valid, otherwise 0 is returned.

For example if MatchRegisterName is given an instruction mnemonic, which is obviously incorrect but also an identifier, 0 will be returned, so checking for an error is key.

[Note]Note

Register values start with a value of 1, so in the case of an architecture like OpenRISC 1000  which uses r0, r1, etc. to name registers, r0 will be represented by 1, r1 by 2, etc.

In addition, this numbering convention is specified by TableGen so it is not guaranteed that register numbers are the same between different compiled versions of LLVM.

If a match was made, the lexer then consumes the token that was used in the match, preparing it for the next operand or instruction. Finally an archOperand is created for the register using the CreateReg function defined above and then returned.

OR1KOperand *OR1KAsmParser::ParseRegister(unsigned &RegNo) {
  SMLoc S = Parser.getTok().getLoc();
  SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() -1);

  switch(getLexer().getKind()) {
    default: return 0;
    case AsmToken::Identifier:
      RegNo = MatchRegisterName(getLexer().getTok().getIdentifier());
      if (RegNo == 0)
        return 0;
      getLexer().Lex();
      return OR1KOperand::CreateReg(RegNo, S, E);
  }
  return 0;
}
        

The same method is applied when parsing immediates. In this case any integer, plus or minus is accepted as a valid token type for the operand. If the type matches, then a MCParser is used to calculate the immediate via the ParseExpression function.

If the expression returned is valid it is then evaluated as an absolute value, with an archOperand being created and returned as before.

Embecosm divider strip