title: RISCV Inline Assembly in GCC: Atomic Load/Store
body: <p>Edit:</p>
  <p>I believe that the clue here was &quot;Illegal *operand&quot;. The problem was with operands. I changed some operand ATTRIBUTES from &quot;m&quot; (or whatever) to &quot;A&quot;, and I made progress. For RISC-V, if you are passing an address/pointer, you must use &quot;A&quot; attribute!</p>
  <hr />
  <p>Colleagues!</p>
  <p>Please consider this block of inline gcc assembly (excerpted from <code>foo.c</code>):</p>
  <pre><code>  asm volatile(
      &quot;als_%=:\n\t&quot;
      &quot;  lr.w.aq  t1, %[src]\n\t&quot;
      &quot;  sc.w.rl  t0, t1, %[dst]\n\t&quot;
      &quot;  bnez     t0, als_%=&quot;
      : [dst] &quot;=&amp;m&quot; (*((uint32_t*)(0x27402448ULL)))
      : [src] &quot;m&quot;   (*((uint32_t*)(0x000a0018ULL))));  
  </code></pre>
  <ul>
  <li>It intends to, first, load the contents of the source address to t1.</li>
  <li>Then to, second, store the contents of t1 to the destination address.</li>
  <li>Finally, if the store fails (looking at status in t0), then the process will be attempted again.</li>
  </ul>
  <p>Note that there is no &quot;symbol&quot; at these addresses.  We're running bare-metal code, creating pathological coherency scenarios among &quot;interesting&quot; random memory locations.</p>
  <p>I get this error using SiFive's pre-built gcc-8.3.0:</p>
  <pre><code>foo.c: Assembler messages:
  foo.c:1106: Error: illegal operands `lr.w t1,8(a4)`
  foo.c:1107: Error: illegal operands `sc.w t0,t1,1048(s0)`
  </code></pre>
  <p>I've tried to <code>la</code> from those locations into temporary registers and then to use the registers in the ld/st instructions, but I get similar errors on the <code>la</code>.</p>
  <p>My command line is:</p>
  <pre><code>/path/to/riscv64-unknown-elf-gcc-8.3.0-2019.08.0-x86_64-linux-centos6/bin/riscv64-unknown-elf-gcc \
  -ggdb3 \
  -std=gnu99 \
  -Wall -Wextra -Werror -Wshadow -Wstack-usage=1024 -Wno-implicit-fallthrough -Wno-unused-parameter \
  -ffunction-sections \
  -fdata-sections \
  -I/path/to/target/include \
  --specs=nano.specs \
  -Os \
  -nostartfiles \
  -nostdlib \
  -Xlinker -Map=foo.map \
  -march=rv64imafdc \
  -mabi=lp64d \
  -mcmodel=medany \
  -malign-data=natural \
  -mriscv-attribute \
  -I/path/to/freedom-e-sdk/bsp/qemu-sifive-u54mc/install/include \
  -L/path/to/freedom-e-sdk/bsp/qemu-sifive-u54mc/install/lib/debug \
  foo.c \
  -T../riscv64/link.ld \
  -Wl,--start-group \
    -lc -lgcc -lm -lmetal -lmetal-gloss \
  -Wl,--end-group \
  -Wl,--gc-sections \
  -o foo.elf;
  </code></pre>