boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

007-loop-tag-scoping.P1pp (1650B)


      1 # tests/p1/loop-tag-scoping.P1pp — sibling-function scoped-loop hygiene.
      2 #
      3 # Two %fn blocks each contain a %loop_scoped whose body uses %break and
      4 # %continue. Each %fn opens its own hex2++ .scope, and each
      5 # %loop_scoped opens a further nested .scope defining :.top / :.end, so
      6 # the two loops' labels can never collide. This test exercises both
      7 # loops to verify that hygiene end-to-end.
      8 #
      9 # Reproducer setup:
     10 #   - Global byte slot starting at '0'.
     11 #   - `bumper` is the first function: it loops 3 times, incrementing
     12 #     the slot ('0' -> '3') on each iteration, exiting via %break.
     13 #   - `p1_main` is the second function. It calls bumper, then enters
     14 #     its own %loop_scoped that immediately %breaks. After the loop it
     15 #     writes the slot to stdout and exits 0.
     16 #
     17 # Expected stdout: "3" (single byte).
     18 
     19 :counter_buf $(0)
     20 
     21 %fn(bumper, 8, {
     22     %li(a0, 0)
     23     %st(a0, sp, 0)
     24     %loop_scoped({
     25         %la(t0, &counter_buf)
     26         %lb(t1, t0, 0)
     27         %addi(t1, t1, 1)
     28         %sb(t1, t0, 0)              # counter_buf[0]++
     29         %ld(t0, sp, 0)
     30         %addi(t0, t0, 1)
     31         %st(t0, sp, 0)              # iter++
     32         %ld(a0, sp, 0)
     33         %li(a1, 3)
     34         %if_lt(a0, a1, { %continue })
     35         %break
     36     })
     37 })
     38 
     39 %fn(p1_main, 0, {
     40     %li(a0, 48)                     # '0'
     41     %la(t0, &counter_buf)
     42     %sb(a0, t0, 0)                  # counter_buf[0] = '0'
     43     %call(&bumper)
     44     %loop_scoped({ %break })        # sibling loop — own .scope, no collision
     45     %li(a0, 1)                      # fd stdout
     46     %la(a1, &counter_buf)
     47     %li(a2, 1)
     48     %call(&sys_write)
     49     %li(a0, 0)                      # exit 0
     50 })
     51 
     52 :ELF_end