asm_01_grammar.c (1415B)
1 /* Track A — frontend parser for GNU inline-asm statements. 2 * 3 * Exercises every limb of the asm-stmt grammar supported in v1: 4 * - both keyword spellings: `asm` and `__asm__` 5 * - `volatile` and `__volatile__` qualifiers (informational, dropped) 6 * - the three colon-separated lists: outputs, inputs, clobbers 7 * - `[name]` symbolic operands 8 * - adjacent string-literal fusion in the template 9 * - empty intermediate sections (`asm("..." : : "r"(x))`) 10 * 11 * `asm goto` is not supported in v1 and is deliberately omitted here. */ 12 13 int test_main(void) { 14 int rc = 42; 15 int a = 1, b = 2, c = 0; 16 17 /* Smallest form. */ 18 asm("nop"); 19 20 /* `__asm__` spelling, `volatile` qualifier. */ 21 __asm__ volatile("nop"); 22 23 /* `__volatile__` alias. */ 24 asm __volatile__("nop"); 25 26 /* Adjacent string-literal fusion in the template. */ 27 asm("nop\n\t" 28 "nop"); 29 30 /* Inputs only. */ 31 __asm__ volatile("mov w0, %w0" : : "r"(rc) : "x0"); 32 33 /* Outputs + inputs + clobbers. */ 34 asm("add %w0, %w1, %w2" : "=r"(c) : "r"(a), "r"(b) : "cc"); 35 36 /* Symbolic operand names ([sum], [x], [y]). */ 37 asm("add %w[sum], %w[x], %w[y]" : [sum] "=r"(c) : [x] "r"(a), [y] "r"(b)); 38 39 /* In-out (`+r`). Decomposes to `=r` + a matching "0" input. */ 40 asm("add %w0, %w0, #1" : "+r"(rc)); 41 42 /* Memory clobber. */ 43 asm volatile("dmb sy" : : : "memory"); 44 45 /* Empty middle sections. */ 46 asm("nop" : : :); 47 48 return rc; 49 }