目录 ← 首页
CS61C

U-Type

Learning Outcomes

  • Explain why U-Type is needed for building wide immediates.
  • Translate the pseudoinstruction li (Load Immediate) into machine code.

Recall that I-Type arithmetic instructions encode an sign extension 12-bit immediate as a two’s complement integer, which is then sign-extended to a 32-bit value. While constants are frequently short and fit into the 12-bit I-Type fields, we inevitably need to encode wide immediates to build numberic constants larger than the I-type’s 12-bit imm.

RISC-V achieves wide immediates as follows:

  • Specify 32-bit constants using two instructions: an I-Type arithmetic instruction for the 12-bit lower immediate, i.e., bits 0 to 11; and and a different instruction for the 20-bit upper immediate, i.e., bits 12 to 31. (Again, reduced instruction set!)
  • Define an instruction format, U-Type (the “U” is for Upper immediate), that can be used for instructions that specify upper immediates.

There are two U-Type instructions: lui and auipc. These are located in the “Other” section of the RISC-V green card.

Load Upper Immediate (lui)

From the P&H textbook (Chapter 2.10), one such instruction, lui, is Load Upper Immediate:

…load a 20-bit constant into bits 12 through 31 of a register. The rightmost 12 bits are filled with zeros.

In #tab-lui, we call this upper immediate immu and the 32-bit immediate imm.

As shown in #fig-u-type-immu, imm = immu << 12; this 32-bit numeric constant imm is then written to register rd.

32-bit U-Type instruction format: the 20-bit field immu sits in bits 31–12, bits 11–0 are zero, and the full immediate imm is formed by concatenating immu with twelve zero in the least significant bit positions.

For U-Type instructions, immu is the top 20 bits of a 32-bit-wide numeric constant imm imm.

Load Immediate Pseudoinstruction

With this new instruction, we can now return to translating the li pseudoinstruction, Load Immediate. When we introduced load immediate, you may have noticed a footnote in #tab-mv-li:

This description is incomplete given the range of imm in addi. See the green-card and a later section for the full translation of load immediates.

In other words, when li must use a numeric constant wider than the 12 bits accommodated by the I-Type addi’s imm field it translates to two instructions: lui and addi.

PseudoinstructionNameDescriptionTranslation
li rd immLoad ImmediateR[rd] = immlui (if needed), addi

In other words, li translates to addi when the immediate is between -2048 and 2047. For anything larger, li translates to two instructions: lui and addi.

In general, the compiler or assembler will break large constants across the lui and addi instructions, as needed. After all, there are some important caveats to this process. Hover over the two footnotes below before continuing.

  • lui to set the upper 20 bits1
  • addi to “set” the lower 12 bits2

Add Upper Immediate to PC

Coming soon. Used for J-Type instructions.

U-Type: Fields

In order to translate lui and auipc to machine code, we need an instruction format that supports 20-bit immediates. The U-Type instruction format is only used by lui and auipc and is the one of the last rows of the instruction format table of the RISC-V green card.

U-type instruction format layout with syntax opname rd immu and fields imm[31:12] (bits 31–12), rd (bits 11–7), and opcode (bits 6–0); additional notes specify that immu is the upper 20 bits of a 32-bit immediate produced by shifting that field left by 12.

The U-Type Instruction Format.

U-Type has only three fields (@fig-u-type):

  • Register operand: The U-Type field rd is the destination register to put the immediate.

  • Opcode: The U-Type field opcode specifies the operation: auipc or lui (@tab-u-type).

  • Constant operand: The upper immediate field (we’ll call it immu) specifies a 20-bit immediate value. immu is left-shifted by 12 to form the upper 20 bits of a 32-bit numeric constant imm (@fig-u-type-immu).

Footnotes

  1. And zero out lower 12 bits

  2. “Set” as in, add a sign-extended 12-bit immediate

  3. Wikipedia: Straw Man

  4. For some definition of “cute”; here, “cool math”